Skip to content

Commit

Permalink
Implement draft of archive.org snapshot lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsan committed Jan 21, 2022
1 parent 91d0ea7 commit 1c17162
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/js/core/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ const bookmarksorganizer = {
*/
disableConfirmations : false,

/**
* Disables automatic replacement with archive.org URL. It defaults to false, there is no user setting (yet).
*
* @type {boolean}
*/
disableAutomaticArchiveOrgReplacement : false,

/**
* Internal variable. It's only true while a check is running.
*
Expand Down Expand Up @@ -540,6 +547,60 @@ const bookmarksorganizer = {
}
},

/**
* This method sends a GET request to the archive.org API to check if there is a snapshot available for a given URL.
* It replaces the current broken URL with the archived URL.
*
* @param {bookmarks.BookmarkTreeNode} bookmark
* @returns {bookmarks.BookmarkTreeNode} - the bookmark object
*/
async checkArchiveOrgSnapshotAvailability (bookmark) {
const archive_api = 'https://archive.org/wayback/available?url=';
const snapshot_base_url = 'https://web.archive.org/web/';

try {
const controller = new AbortController();
const { signal } = controller;

setTimeout(() => controller.abort(), bookmarksorganizer.TIMEOUT_IN_MS);

const response = await fetch(`${archive_api}${bookmark.url}`, {
cache : 'no-store',
method : 'GET',
mode : 'no-cors',
signal : signal
});

const archived_snapshots = await response.json();

console.log(archived_snapshots);

// Check for available snapshots
if (archived_snapshots.keys('archived_snapshots').length === 0) {
bookmark.status = STATUS.NOT_FOUND;
}
else {
bookmark.newUrl = `${snapshot_base_url}${bookmark.url}`;
bookmark.status = STATUS.REDIRECT;
}

return bookmark;
}
catch (error) {
let cause = 'snapshot-availability-fetch-error';

if (error.name === 'AbortError') {
// eslint-disable-next-line require-atomic-updates
bookmark.status = STATUS.TIMEOUT;
cause = 'timeout';
}
else {
// eslint-disable-next-line require-atomic-updates
bookmark.status = STATUS.FETCH_ERROR;
}
}
},

/**
* This method sends a fetch request to check if a bookmark is broken or not, called by checkForBrokenBookmark().
*
Expand Down Expand Up @@ -612,7 +673,11 @@ const bookmarksorganizer = {
});
}

if (bookmark.status > STATUS.REDIRECT) {
if (bookmark.status == STATUS.NOT_FOUND) {
console.log('Checking for archive.org availability...');
await bookmarksorganizer.checkArchiveOrgSnapshotAvailability(bookmark);
}
else if (bookmark.status > STATUS.NOT_FOUND) {
if (bookmark.attempts < bookmarksorganizer.MAX_ATTEMPTS) {
await bookmarksorganizer.checkHttpResponse(bookmark, 'GET');
}
Expand Down

0 comments on commit 1c17162

Please sign in to comment.