From aa7a0ab961aa53a19f758e50172d04360bef979d Mon Sep 17 00:00:00 2001 From: Simon San <14062932+simonsan@users.noreply.github.com> Date: Fri, 21 Jan 2022 21:41:12 +0100 Subject: [PATCH] Draft of archive.org implementation --- src/js/core/background.js | 547 ++++++++++++++++++++++---------------- 1 file changed, 324 insertions(+), 223 deletions(-) diff --git a/src/js/core/background.js b/src/js/core/background.js index d59fe97..7b12707 100644 --- a/src/js/core/background.js +++ b/src/js/core/background.js @@ -1,10 +1,10 @@ -'use strict'; +"use strict"; /* global STATUS */ const MIN_PROGRESS = 0.01; const TIMEOUT_500_MS = 500; -const UI_PAGE = 'html/ui.html'; +const UI_PAGE = "html/ui.html"; /** * @exports bookmarksorganizer @@ -16,21 +16,21 @@ const bookmarksorganizer = { * * @type {int} */ - LIMIT : 0, + LIMIT: 0, /** * Max attempts to connect to an URL. It's always 2, there is no user setting (yet). * * @type {int} */ - MAX_ATTEMPTS : 2, + MAX_ATTEMPTS: 2, /** * Timeout for reaching a server in milliseconds. It's always 15 seconds, there is no user setting (yet). * * @type {int} */ - TIMEOUT_IN_MS : 15000, + TIMEOUT_IN_MS: 15000, /** * Never process more than QUEUE_SIZE bookmarks at the same time. It's always a size of 10, there is no user @@ -38,98 +38,105 @@ const bookmarksorganizer = { * * @type {int} */ - QUEUE_SIZE : 10, + QUEUE_SIZE: 10, /** * Whether the internal skip list should be used or not. It's always true, there is no user setting (yet). * * @type {boolean} */ - USE_SKIP_LIST : true, + USE_SKIP_LIST: true, /** * Enables or disables the debug mode. It defaults to false and can be enabled in the add-on's settings. * * @type {boolean} */ - debugEnabled : false, + debugEnabled: false, /** * Disables confirmation messages. It defaults to false and can be changed in the add-on's settings. * * @type {boolean} */ - disableConfirmations : false, + 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. * * @type {boolean} */ - inProgress : false, + inProgress: false, /** * Internal variable. The index of the bookmark being checked. * * @type {int} */ - internalCounter : 0, + internalCounter: 0, /** * The number of total bookmarks. * * @type {int} */ - totalBookmarks : 0, + totalBookmarks: 0, /** * The number of already checked bookmarks. * * @type {int} */ - checkedBookmarks : 0, + checkedBookmarks: 0, /** * The number of found errors. * * @type {int} */ - bookmarkErrors : 0, + bookmarkErrors: 0, /** * The number of found warnings (e.g. redirects). * * @type {int} */ - bookmarkWarnings : 0, + bookmarkWarnings: 0, /** * An array containing all bookmarks. * * @type {Array.} */ - collectedBookmarks : [], + collectedBookmarks: [], /** * An array of bookmarks with errors or warnings. * * @type {Array.} */ - bookmarksResult : [], + bookmarksResult: [], /** * Additional data stored for bookmarks. In current version it only contains the full bookmark path. * * @type {Array.} */ - additionalData : [], + additionalData: [], /** * An array with debugging data about bookmark requests. Only used if debug mode is enabled. * * @type {Array.} */ - debug : [], + debug: [], /** * An array of url patterns which should be ignored while checking for broken bookmarks. Please only add patterns @@ -138,21 +145,21 @@ const bookmarksorganizer = { * @type {Array.} * */ - ignoreForBrokenBookmarks : [ + ignoreForBrokenBookmarks: [ /* eslint-disable no-useless-escape, line-comment-position, no-inline-comments */ - '^https?:\/\/groups.google.com/group/', // issue #25 - '^https?:\/\/accounts-static.cdn.mozilla.net', // issue #76 - '^https?:\/\/accounts.firefox.com', // issue #76 - '^https?:\/\/addons.cdn.mozilla.net', // issue #76 - '^https?:\/\/addons.mozilla.org', // issue #76 - '^https?:\/\/api.accounts.firefox.com', // issue #76 - '^https?:\/\/content.cdn.mozilla.net', // issue #76 - '^https?:\/\/discovery.addons.mozilla.org', // issue #76 - '^https?:\/\/install.mozilla.org', // issue #76 - '^https?:\/\/oauth.accounts.firefox.com', // issue #76 - '^https?:\/\/profile.accounts.firefox.com', // issue #76 - '^https?:\/\/support.mozilla.org', // issue #76 - '^https?:\/\/sync.services.mozilla.com' // issue #76 + "^https?://groups.google.com/group/", // issue #25 + "^https?://accounts-static.cdn.mozilla.net", // issue #76 + "^https?://accounts.firefox.com", // issue #76 + "^https?://addons.cdn.mozilla.net", // issue #76 + "^https?://addons.mozilla.org", // issue #76 + "^https?://api.accounts.firefox.com", // issue #76 + "^https?://content.cdn.mozilla.net", // issue #76 + "^https?://discovery.addons.mozilla.org", // issue #76 + "^https?://install.mozilla.org", // issue #76 + "^https?://oauth.accounts.firefox.com", // issue #76 + "^https?://profile.accounts.firefox.com", // issue #76 + "^https?://support.mozilla.org", // issue #76 + "^https?://sync.services.mozilla.com", // issue #76 /* eslint-enable no-useless-escape, line-comment-position, no-inline-comments */ ], @@ -164,7 +171,7 @@ const bookmarksorganizer = { * * @returns {void} */ - onBookmarkCreated (id, bookmark) { + onBookmarkCreated(id, bookmark) { bookmarksorganizer.collectAllBookmarks(bookmark, true); }, @@ -176,20 +183,21 @@ const bookmarksorganizer = { * * @returns {void} */ - onBookmarkChanged (id, bookmark) { + onBookmarkChanged(id, bookmark) { // update bookmark in array with all bookmarks - const idx = bookmarksorganizer.collectedBookmarks.findIndex((obj) => obj.id === id); + const idx = bookmarksorganizer.collectedBookmarks.findIndex( + (obj) => obj.id === id + ); if (idx > -1) { - if (bookmark.title || bookmark.title === '') { + if (bookmark.title || bookmark.title === "") { bookmarksorganizer.collectedBookmarks[idx].title = bookmark.title; } if (bookmark.url) { bookmarksorganizer.collectedBookmarks[idx].url = bookmark.url; } - } - else { + } else { bookmarksorganizer.collectedBookmarks.push(bookmark); } }, @@ -201,14 +209,16 @@ const bookmarksorganizer = { * * @returns {void} */ - onBookmarkRemoved (id) { + onBookmarkRemoved(id) { browser.runtime.sendMessage({ - message : 'total-bookmarks-changed', - total_bookmarks : --bookmarksorganizer.totalBookmarks + message: "total-bookmarks-changed", + total_bookmarks: --bookmarksorganizer.totalBookmarks, }); // remove bookmark from array with all bookmarks - const idx = bookmarksorganizer.collectedBookmarks.findIndex((obj) => obj.id === id); + const idx = bookmarksorganizer.collectedBookmarks.findIndex( + (obj) => obj.id === id + ); bookmarksorganizer.collectedBookmarks.splice(idx, 1); }, @@ -224,22 +234,32 @@ const bookmarksorganizer = { * * @returns {void} */ - showOmniboxSuggestions (input, suggest) { - const availableCommands = ['duplicates', 'empty-names', 'errors', 'organizer', 'redirects']; + showOmniboxSuggestions(input, suggest) { + const availableCommands = [ + "duplicates", + "empty-names", + "errors", + "organizer", + "redirects", + ]; const suggestions = []; for (const command of availableCommands) { if (command.indexOf(input) !== -1) { suggestions.push({ - content : command, - description : browser.i18n.getMessage('omnibox_command_check_' + command.replace('-', '_')) + content: command, + description: browser.i18n.getMessage( + "omnibox_command_check_" + command.replace("-", "_") + ), }); } if (suggestions.length === 0) { suggestions.push({ - content : 'organizer', - description : browser.i18n.getMessage('omnibox_command_check_organizer') + content: "organizer", + description: browser.i18n.getMessage( + "omnibox_command_check_organizer" + ), }); } } @@ -254,25 +274,25 @@ const bookmarksorganizer = { * * @returns {void} */ - callOmniboxAction (input) { + callOmniboxAction(input) { bookmarksorganizer.openUserInterfaceInCurrentTab(); // issue #74: set timeout to prevent "receiving end does not exist" error setTimeout(() => { switch (input) { - case 'errors': - bookmarksorganizer.execute('broken-bookmarks', 'errors'); + case "errors": + bookmarksorganizer.execute("broken-bookmarks", "errors"); break; - case 'redirects': - bookmarksorganizer.execute('broken-bookmarks', 'warnings'); + case "redirects": + bookmarksorganizer.execute("broken-bookmarks", "warnings"); break; - case 'duplicates': - bookmarksorganizer.execute('duplicates', 'all'); + case "duplicates": + bookmarksorganizer.execute("duplicates", "all"); break; - case 'empty-names': - bookmarksorganizer.execute('empty-names', 'all'); + case "empty-names": + bookmarksorganizer.execute("empty-names", "all"); break; - case 'organizer': + case "organizer": default: bookmarksorganizer.openUserInterfaceInCurrentTab(); } @@ -285,7 +305,7 @@ const bookmarksorganizer = { * * @returns {void} */ - openUserInterface () { + openUserInterface() { const url = browser.runtime.getURL(UI_PAGE); browser.tabs.query({}, (tabs) => { @@ -299,9 +319,8 @@ const bookmarksorganizer = { } if (tabId) { - browser.tabs.update(tabId, { active : true }); - } - else { + browser.tabs.update(tabId, { active: true }); + } else { browser.tabs.create({ url }); } }); @@ -312,8 +331,8 @@ const bookmarksorganizer = { * * @returns {void} */ - openUserInterfaceInCurrentTab () { - browser.tabs.update(null, { url : browser.runtime.getURL(UI_PAGE) }); + openUserInterfaceInCurrentTab() { + browser.tabs.update(null, { url: browser.runtime.getURL(UI_PAGE) }); }, /** @@ -323,47 +342,41 @@ const bookmarksorganizer = { * * @returns {void} */ - async handleResponse (response) { - if (response.message === 'count') { + async handleResponse(response) { + if (response.message === "count") { bookmarksorganizer.initBookmarkCount(); - } - else if (response.message === 'execute') { + } else if (response.message === "execute") { if (!bookmarksorganizer.inProgress) { - bookmarksorganizer.execute(response.mode, 'all'); + bookmarksorganizer.execute(response.mode, "all"); } - } - else if (response.message === 'edit') { + } else if (response.message === "edit") { await browser.bookmarks.update(response.bookmarkId, { - title : response.title, - url : response.url + title: response.title, + url: response.url, }); - if (response.mode === 'duplicate') { + if (response.mode === "duplicate") { browser.runtime.sendMessage({ - message : 'update-listitem', - bookmarkId : response.bookmarkId, - title : response.title, - path : bookmarksorganizer.additionalData[response.bookmarkId].path, - mode : response.mode + message: "update-listitem", + bookmarkId: response.bookmarkId, + title: response.title, + path: bookmarksorganizer.additionalData[response.bookmarkId].path, + mode: response.mode, }); - } - else { + } else { const bookmarks = await browser.bookmarks.get(response.bookmarkId); browser.runtime.sendMessage({ - message : 'update-listitem', - bookmarkId : response.bookmarkId, - bookmark : bookmarks[0], - mode : response.mode + message: "update-listitem", + bookmarkId: response.bookmarkId, + bookmark: bookmarks[0], + mode: response.mode, }); } - } - else if (response.message === 'remove') { + } else if (response.message === "remove") { browser.bookmarks.remove(response.bookmarkId); - } - else if (response.message === 'repair-redirect') { - browser.bookmarks.update(response.bookmarkId, { url : response.newUrl }); - } - else if (response.message === 'ignore') { + } else if (response.message === "repair-redirect") { + browser.bookmarks.update(response.bookmarkId, { url: response.newUrl }); + } else if (response.message === "ignore") { bookmarksorganizer.addToWhitelist(response.bookmarkId); } }, @@ -374,7 +387,7 @@ const bookmarksorganizer = { * * @returns {void} */ - async initBookmarkCount () { + async initBookmarkCount() { const bookmarks = await browser.bookmarks.getTree(); bookmarksorganizer.totalBookmarks = 0; @@ -382,8 +395,8 @@ const bookmarksorganizer = { bookmarksorganizer.collectAllBookmarks(bookmarks[0], false); browser.runtime.sendMessage({ - message : 'total-bookmarks', - total_bookmarks : bookmarksorganizer.totalBookmarks + message: "total-bookmarks", + total_bookmarks: bookmarksorganizer.totalBookmarks, }); }, @@ -397,7 +410,7 @@ const bookmarksorganizer = { * * @returns {void} */ - async execute (mode, type) { + async execute(mode, type) { bookmarksorganizer.inProgress = true; bookmarksorganizer.internalCounter = 0; bookmarksorganizer.checkedBookmarks = 0; @@ -407,18 +420,23 @@ const bookmarksorganizer = { bookmarksorganizer.additionalData = []; bookmarksorganizer.debug = []; - browser.runtime.sendMessage({ message : 'started' }); + browser.runtime.sendMessage({ message: "started" }); browser.storage.local.get((options) => { bookmarksorganizer.debugEnabled = options.debugEnabled || false; - bookmarksorganizer.disableConfirmations = options.disableConfirmations || false; + bookmarksorganizer.disableConfirmations = + options.disableConfirmations || false; }); switch (mode) { - case 'broken-bookmarks': - await bookmarksorganizer.processBookmarks(mode, type, bookmarksorganizer.QUEUE_SIZE); + case "broken-bookmarks": + await bookmarksorganizer.processBookmarks( + mode, + type, + bookmarksorganizer.QUEUE_SIZE + ); break; - case 'duplicates': + case "duplicates": const bookmarks = await browser.bookmarks.getTree(); bookmarksorganizer.getBookmarkPath(bookmarks[0], []); @@ -429,13 +447,13 @@ const bookmarksorganizer = { bookmarksorganizer.checkForDuplicates(); break; - case 'empty-names': + case "empty-names": for (const bookmark of bookmarksorganizer.collectedBookmarks) { bookmarksorganizer.checkForEmptyName(bookmark, mode); } break; default: - // do nothing + // do nothing } }, @@ -447,7 +465,7 @@ const bookmarksorganizer = { * * @returns {Array.} - An array with the full path of all bookmarks */ - getBookmarkPath (bookmark, path) { + getBookmarkPath(bookmark, path) { if (bookmark.title) { path.push(bookmark.title); } @@ -456,8 +474,7 @@ const bookmarksorganizer = { for (const childNode of bookmark.children) { bookmarksorganizer.getBookmarkPath(childNode, path); } - } - else { + } else { if (!bookmarksorganizer.additionalData[bookmark.id]) { bookmarksorganizer.additionalData[bookmark.id] = {}; } @@ -481,21 +498,29 @@ const bookmarksorganizer = { * * @returns {void} */ - async checkForBrokenBookmark (bookmark, mode, type) { + async checkForBrokenBookmark(bookmark, mode, type) { if (bookmark.url) { - if (bookmarksorganizer.LIMIT > 0 && bookmarksorganizer.internalCounter === bookmarksorganizer.LIMIT) { + if ( + bookmarksorganizer.LIMIT > 0 && + bookmarksorganizer.internalCounter === bookmarksorganizer.LIMIT + ) { return; } bookmarksorganizer.internalCounter++; - if (bookmarksorganizer.USE_SKIP_LIST && bookmarksorganizer.ignoreForBrokenBookmarks.some((i) => new RegExp('\\b' + i + '\\b').test(bookmark.url))) { + if ( + bookmarksorganizer.USE_SKIP_LIST && + bookmarksorganizer.ignoreForBrokenBookmarks.some((i) => + new RegExp("\\b" + i + "\\b").test(bookmark.url) + ) + ) { bookmarksorganizer.checkedBookmarks++; return; } - const { whitelist } = await browser.storage.local.get({ whitelist : [] }); + const { whitelist } = await browser.storage.local.get({ whitelist: [] }); if (whitelist.includes(bookmark.id)) { bookmarksorganizer.checkedBookmarks++; @@ -503,15 +528,18 @@ const bookmarksorganizer = { return; } - if ((/^https?:\/\//).test(bookmark.url)) { + if (/^https?:\/\//.test(bookmark.url)) { bookmark.attempts = 0; - const checkedBookmark = await bookmarksorganizer.checkHttpResponse(bookmark, 'HEAD'); + const checkedBookmark = await bookmarksorganizer.checkHttpResponse( + bookmark, + "HEAD" + ); bookmarksorganizer.checkedBookmarks++; switch (checkedBookmark.status) { case STATUS.REDIRECT: - if (type === 'all' || type === 'warnings') { + if (type === "all" || type === "warnings") { bookmarksorganizer.bookmarkWarnings++; bookmarksorganizer.bookmarksResult.push(checkedBookmark); } @@ -519,27 +547,76 @@ const bookmarksorganizer = { case STATUS.NOT_FOUND: case STATUS.FETCH_ERROR: case STATUS.TIMEOUT: - if (type === 'all' || type === 'errors') { + if (type === "all" || type === "errors") { bookmarksorganizer.bookmarkErrors++; bookmarksorganizer.bookmarksResult.push(checkedBookmark); } break; default: - // do nothing + // do nothing } bookmarksorganizer.updateProgressUi(mode, true); - } - else { + } else { bookmarksorganizer.checkedBookmarks++; bookmarksorganizer.updateProgressUi(mode, true); } - } - else { + } else { bookmarksorganizer.bookmarksResult.push(bookmark); } }, + /** + * 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, + }); + + let 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(). * @@ -548,7 +625,7 @@ const bookmarksorganizer = { * * @returns {bookmarks.BookmarkTreeNode} - the bookmark object */ - async checkHttpResponse (bookmark, method) { + async checkHttpResponse(bookmark, method) { bookmark.attempts++; try { @@ -558,10 +635,10 @@ const bookmarksorganizer = { setTimeout(() => controller.abort(), bookmarksorganizer.TIMEOUT_IN_MS); const response = await fetch(bookmark.url, { - cache : 'no-store', - method : method, - mode : 'no-cors', - signal : signal + cache: "no-store", + method: method, + mode: "no-cors", + signal: signal, }); if (response.redirected) { @@ -577,17 +654,18 @@ const bookmarksorganizer = { bookmark.newUrl = response.url; // preserve the hash for redirects (issue #24) - if (bookmark.url.indexOf('#') !== -1) { - bookmark.newUrl += bookmark.url.substring(bookmark.url.indexOf('#')); + if (bookmark.url.indexOf("#") !== -1) { + bookmark.newUrl += bookmark.url.substring(bookmark.url.indexOf("#")); } - } - else { + } else { const { headers } = response; - if (headers.has('Content-Length') && headers.get('Content-Length') === '0') { + if ( + headers.has("Content-Length") && + headers.get("Content-Length") === "0" + ) { // eslint-disable-next-line require-atomic-updates bookmark.status = STATUS.EMPTY_BODY; - } - else { + } else { // eslint-disable-next-line require-atomic-updates bookmark.status = response.status; } @@ -595,59 +673,60 @@ const bookmarksorganizer = { if (bookmarksorganizer.debugEnabled) { bookmarksorganizer.debug.push({ - bookmark : { - id : bookmark.id, - parentId : bookmark.parentId, - title : bookmark.title, - url : bookmark.url, - status : bookmark.status + bookmark: { + id: bookmark.id, + parentId: bookmark.parentId, + title: bookmark.title, + url: bookmark.url, + status: bookmark.status, + }, + method: method, + cause: "server-response", + response: { + url: response.url, + redirected: response.redirected, + status: response.status, }, - method : method, - cause : 'server-response', - response : { - url : response.url, - redirected : response.redirected, - status : response.status - } }); } - 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'); + await bookmarksorganizer.checkHttpResponse(bookmark, "GET"); } } - } - catch (error) { - let cause = 'fetch-error'; + } catch (error) { + let cause = "fetch-error"; - if (error.name === 'AbortError') { + if (error.name === "AbortError") { // eslint-disable-next-line require-atomic-updates bookmark.status = STATUS.TIMEOUT; - cause = 'timeout'; - } - else { + cause = "timeout"; + } else { // eslint-disable-next-line require-atomic-updates bookmark.status = STATUS.FETCH_ERROR; } if (bookmarksorganizer.debugEnabled) { bookmarksorganizer.debug.push({ - bookmark : { - id : bookmark.id, - parentId : bookmark.parentId, - title : bookmark.title, - url : bookmark.url, - status : bookmark.status + bookmark: { + id: bookmark.id, + parentId: bookmark.parentId, + title: bookmark.title, + url: bookmark.url, + status: bookmark.status, }, - method : method, - cause : cause, - response : error.message + method: method, + cause: cause, + response: error.message, }); } if (bookmark.attempts < bookmarksorganizer.MAX_ATTEMPTS) { - await bookmarksorganizer.checkHttpResponse(bookmark, 'GET'); + await bookmarksorganizer.checkHttpResponse(bookmark, "GET"); } } @@ -663,12 +742,15 @@ const bookmarksorganizer = { * * @returns {void} */ - collectAllBookmarks (bookmark, updateCounter) { - if (bookmarksorganizer.LIMIT > 0 && bookmarksorganizer.totalBookmarks === bookmarksorganizer.LIMIT) { + collectAllBookmarks(bookmark, updateCounter) { + if ( + bookmarksorganizer.LIMIT > 0 && + bookmarksorganizer.totalBookmarks === bookmarksorganizer.LIMIT + ) { return; } - if (bookmark.type === 'separator') { + if (bookmark.type === "separator") { return; } @@ -680,8 +762,8 @@ const bookmarksorganizer = { if (updateCounter) { browser.runtime.sendMessage({ - message : 'total-bookmarks-changed', - total_bookmarks : bookmarksorganizer.totalBookmarks + message: "total-bookmarks-changed", + total_bookmarks: bookmarksorganizer.totalBookmarks, }); } @@ -704,13 +786,17 @@ const bookmarksorganizer = { * * @returns {Promise.>} - Promise */ - processBookmarks (mode, type, queue_size) { + processBookmarks(mode, type, queue_size) { const limiter = bookmarksorganizer.throttle(queue_size); const tasks = []; const executeTask = function (aBookmark, aMode, aType) { return async function () { - await bookmarksorganizer.checkForBrokenBookmark(aBookmark, aMode, aType); + await bookmarksorganizer.checkForBrokenBookmark( + aBookmark, + aMode, + aType + ); return limiter.give(); }; @@ -731,10 +817,10 @@ const bookmarksorganizer = { * * @returns {Object} - Object */ - throttle (queue_size) { + throttle(queue_size) { const queue = { - available : queue_size, - maximum : queue_size + available: queue_size, + maximum: queue_size, }; const futures = []; @@ -761,12 +847,11 @@ const bookmarksorganizer = { const future = futures.shift(); future(); - } - else { + } else { queue.available += 1; if (queue.available === queue.maximum) { - emptyPromiseResolver('empty queue'); + emptyPromiseResolver("empty queue"); } } }; @@ -787,9 +872,12 @@ const bookmarksorganizer = { * * @returns {void} */ - checkBookmarkAndAssignPath (bookmark, mode) { + checkBookmarkAndAssignPath(bookmark, mode) { if (bookmark.url) { - if (bookmarksorganizer.LIMIT > 0 && bookmarksorganizer.internalCounter === bookmarksorganizer.LIMIT) { + if ( + bookmarksorganizer.LIMIT > 0 && + bookmarksorganizer.internalCounter === bookmarksorganizer.LIMIT + ) { return; } @@ -812,16 +900,19 @@ const bookmarksorganizer = { * * @returns {void} */ - checkForEmptyName (bookmark, mode) { + checkForEmptyName(bookmark, mode) { if (bookmark.url) { - if (bookmarksorganizer.LIMIT > 0 && bookmarksorganizer.internalCounter === bookmarksorganizer.LIMIT) { + if ( + bookmarksorganizer.LIMIT > 0 && + bookmarksorganizer.internalCounter === bookmarksorganizer.LIMIT + ) { return; } bookmarksorganizer.internalCounter++; // skip place:-URIs (issue #3) - if (!bookmark.url.startsWith('place:')) { + if (!bookmark.url.startsWith("place:")) { if (!bookmark.title) { bookmarksorganizer.bookmarkErrors++; bookmarksorganizer.bookmarksResult.push(bookmark); @@ -830,8 +921,7 @@ const bookmarksorganizer = { bookmarksorganizer.checkedBookmarks++; bookmarksorganizer.updateProgressUi(mode, true); - } - else { + } else { bookmarksorganizer.bookmarksResult.push(bookmark); } }, @@ -841,15 +931,14 @@ const bookmarksorganizer = { * * @returns {void} */ - checkForDuplicates () { - const duplicates = { }; + checkForDuplicates() { + const duplicates = {}; bookmarksorganizer.bookmarksResult.forEach((bookmark) => { if (bookmark.url) { if (duplicates[bookmark.url]) { duplicates[bookmark.url].push(bookmark); - } - else { + } else { duplicates[bookmark.url] = [bookmark]; } } @@ -858,17 +947,16 @@ const bookmarksorganizer = { Object.keys(duplicates).forEach((key) => { if (duplicates[key].length < 2) { delete duplicates[key]; - } - else { + } else { bookmarksorganizer.bookmarkErrors++; } }); browser.runtime.sendMessage({ - message : 'show-duplicates-ui', - bookmarks : duplicates, - errors : bookmarksorganizer.bookmarkErrors, - disableConfirmations : bookmarksorganizer.disableConfirmations + message: "show-duplicates-ui", + bookmarks: duplicates, + errors: bookmarksorganizer.bookmarkErrors, + disableConfirmations: bookmarksorganizer.disableConfirmations, }); bookmarksorganizer.inProgress = false; @@ -884,30 +972,36 @@ const bookmarksorganizer = { * * @returns {void} */ - updateProgressUi (mode, checkForFinish) { - let progress = bookmarksorganizer.checkedBookmarks / bookmarksorganizer.totalBookmarks; + updateProgressUi(mode, checkForFinish) { + let progress = + bookmarksorganizer.checkedBookmarks / bookmarksorganizer.totalBookmarks; if (progress < MIN_PROGRESS) { progress = MIN_PROGRESS; } browser.runtime.sendMessage({ - message : 'update-counters', - total_bookmarks : bookmarksorganizer.totalBookmarks, - checked_bookmarks : bookmarksorganizer.checkedBookmarks, - bookmarks_errors : bookmarksorganizer.bookmarkErrors, - bookmarks_warnings : bookmarksorganizer.bookmarkWarnings, - progress : progress + message: "update-counters", + total_bookmarks: bookmarksorganizer.totalBookmarks, + checked_bookmarks: bookmarksorganizer.checkedBookmarks, + bookmarks_errors: bookmarksorganizer.bookmarkErrors, + bookmarks_warnings: bookmarksorganizer.bookmarkWarnings, + progress: progress, }); - if (checkForFinish && bookmarksorganizer.checkedBookmarks === bookmarksorganizer.totalBookmarks) { - const bookmarks = bookmarksorganizer.buildResultArray(bookmarksorganizer.bookmarksResult)[0].children; + if ( + checkForFinish && + bookmarksorganizer.checkedBookmarks === bookmarksorganizer.totalBookmarks + ) { + const bookmarks = bookmarksorganizer.buildResultArray( + bookmarksorganizer.bookmarksResult + )[0].children; browser.runtime.sendMessage({ - message : 'finished', - mode : mode, - bookmarks : bookmarks, - disableConfirmations : bookmarksorganizer.disableConfirmations, - debug : bookmarksorganizer.debug + message: "finished", + mode: mode, + bookmarks: bookmarks, + disableConfirmations: bookmarksorganizer.disableConfirmations, + debug: bookmarksorganizer.debug, }); bookmarksorganizer.inProgress = false; @@ -922,7 +1016,7 @@ const bookmarksorganizer = { * * @returns {Array.} - an array with the result of bookmarks with errors and warnings */ - buildResultArray (bookmarks) { + buildResultArray(bookmarks) { const result = []; const mappedArray = {}; let mappedElement = null; @@ -937,8 +1031,7 @@ const bookmarksorganizer = { mappedElement = mappedArray[id]; if (mappedElement.parentId) { mappedArray[mappedElement.parentId].children.push(mappedElement); - } - else { + } else { result.push(mappedElement); } } @@ -954,27 +1047,35 @@ const bookmarksorganizer = { * * @returns {void} */ - async addToWhitelist (bookmarkId) { - const { whitelist } = await browser.storage.local.get({ whitelist : [] }); + async addToWhitelist(bookmarkId) { + const { whitelist } = await browser.storage.local.get({ whitelist: [] }); if (!whitelist.includes(bookmarkId)) { whitelist.push(bookmarkId); - browser.storage.local.set({ whitelist : whitelist }); + browser.storage.local.set({ whitelist: whitelist }); } - } + }, }; browser.bookmarks.onCreated.addListener(bookmarksorganizer.onBookmarkCreated); browser.bookmarks.onChanged.addListener(bookmarksorganizer.onBookmarkChanged); browser.bookmarks.onRemoved.addListener(bookmarksorganizer.onBookmarkRemoved); -browser.browserAction.onClicked.addListener(bookmarksorganizer.openUserInterface); -browser.omnibox.onInputChanged.addListener(bookmarksorganizer.showOmniboxSuggestions); -browser.omnibox.onInputEntered.addListener(bookmarksorganizer.callOmniboxAction); -browser.omnibox.setDefaultSuggestion({ description : browser.i18n.getMessage('omnibox_default_description') }); +browser.browserAction.onClicked.addListener( + bookmarksorganizer.openUserInterface +); +browser.omnibox.onInputChanged.addListener( + bookmarksorganizer.showOmniboxSuggestions +); +browser.omnibox.onInputEntered.addListener( + bookmarksorganizer.callOmniboxAction +); +browser.omnibox.setDefaultSuggestion({ + description: browser.i18n.getMessage("omnibox_default_description"), +}); browser.runtime.onMessage.addListener(bookmarksorganizer.handleResponse); browser.menus.create({ - title : browser.i18n.getMessage('omnibox_command_check_organizer'), - contexts : ['tools_menu'], - command : '_execute_browser_action' + title: browser.i18n.getMessage("omnibox_command_check_organizer"), + contexts: ["tools_menu"], + command: "_execute_browser_action", });