From f7949e89f6e482369e718534fd430aba94dcbe02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20R=C3=B6bert?= Date: Thu, 20 Feb 2020 14:12:06 +0100 Subject: [PATCH] Added catch statements --- _locales/de/messages.json | 2 +- clearurls.js | 10 +++++----- core_js/context_menu.js | 2 +- core_js/log.js | 8 ++++---- core_js/popup.js | 12 ++++++------ core_js/settings.js | 16 ++++++++-------- core_js/storage.js | 12 ++---------- core_js/tools.js | 24 ++++++++++++++---------- 8 files changed, 41 insertions(+), 45 deletions(-) diff --git a/_locales/de/messages.json b/_locales/de/messages.json index 47eebb6..7941911 100644 --- a/_locales/de/messages.json +++ b/_locales/de/messages.json @@ -1,6 +1,6 @@ { "extension_description": { - "message": "Entfernt tracking Elemente von URLs.", + "message": "Entfernt Tracking-Elemente von URLs.", "description": "Erweiterungsbeschreibung (max. 132 Zeichen)" }, "hash_status_code_1": { diff --git a/clearurls.js b/clearurls.js index 354667b..c5eec5d 100644 --- a/clearurls.js +++ b/clearurls.js @@ -579,7 +579,7 @@ function start() { if (result.redirect) { if (providers[i].shouldForceRedirect() && request.type === 'main_frame') { - browser.tabs.update(request.tabId, {url: result.url}); + browser.tabs.update(request.tabId, {url: result.url}).catch(handleError); return {cancel: true}; } @@ -595,7 +595,7 @@ function start() { if (result.cancel) { if (request.type === 'main_frame') { const blockingPage = browser.extension.getURL("html/siteBlockedAlert.html?source=" + encodeURIComponent(request.url)); - browser.tabs.update(request.tabId, {url: blockingPage}); + browser.tabs.update(request.tabId, {url: blockingPage}).catch(handleError); return {cancel: true}; } else { @@ -654,7 +654,7 @@ function start() { if (!browser.runtime.lastError) { // https://gitlab.com/KevinRoebert/ClearUrls/issues/346 currentURL = tab.url; } - }); + }).catch(handleError); } /** @@ -740,9 +740,9 @@ function increaseBadged(quiet = false) { checkOSAndroid().then((res) => { if (!res) { if (storage.badgedStatus && !quiet) { - browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid}); + browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid}).catch(handleError); } else { - browser.browserAction.setBadgeText({text: "", tabId: tabid}); + browser.browserAction.setBadgeText({text: "", tabId: tabid}).catch(handleError); } } }); diff --git a/core_js/context_menu.js b/core_js/context_menu.js index 2e53796..7626303 100644 --- a/core_js/context_menu.js +++ b/core_js/context_menu.js @@ -42,7 +42,7 @@ function contextMenuStart() { if (!results || results[0] !== true) { return browser.tabs.executeScript(tab.id, { file: "/external_js/clipboard-helper.js", - }); + }).catch(handleError); } }).then(() => { return browser.tabs.executeScript(tab.id, { diff --git a/core_js/log.js b/core_js/log.js index 10aca43..59c48b7 100644 --- a/core_js/log.js +++ b/core_js/log.js @@ -31,7 +31,7 @@ function resetGlobalLog(){ browser.runtime.sendMessage({ function: "setData", params: ['log', JSON.stringify(obj)] - }); + }).catch(handleError); location.reload(); } @@ -72,7 +72,7 @@ function getLog() "url": getDataTableTranslation() } } ).order([3, 'desc']).draw(); - }); + }).catch(handleError); } /** @@ -107,8 +107,8 @@ function exportGlobalLog() { 'url': URL.createObjectURL(blob), 'filename': 'ClearURLsLogExport.json', 'saveAs': true - }); - }); + }).catch(handleError); + }).catch(handleError); } /** diff --git a/core_js/popup.js b/core_js/popup.js index e616275..c0e3685 100644 --- a/core_js/popup.js +++ b/core_js/popup.js @@ -99,15 +99,15 @@ function changeSwitchButton(id, storageID) browser.runtime.sendMessage({ function: "changeIcon", params: [] - }); + }).catch(handleError); } changeVisibility(id, storageID); browser.runtime.sendMessage({ function: "saveOnExit", params: [] - }); - }); + }).catch(handleError); + }).catch(handleError); }); } @@ -162,17 +162,17 @@ function resetGlobalCounter(){ browser.runtime.sendMessage({ function: "setData", params: ['globalCounter', 0] - }); + }).catch(handleError); browser.runtime.sendMessage({ function: "setData", params: ['globalurlcounter', 0] - }); + }).catch(handleError); browser.runtime.sendMessage({ function: "saveOnExit", params: [] - }); + }).catch(handleError); globalCounter = 0; globalurlcounter = 0; diff --git a/core_js/settings.js b/core_js/settings.js index 46fee23..afb6c6d 100644 --- a/core_js/settings.js +++ b/core_js/settings.js @@ -107,7 +107,7 @@ function getData() } else { $('#logLimit_label').text(translate('setting_log_limit_label', logData.response)); } - }); + }).catch(handleError); loadData("contextMenuEnabled") .then(() => loadData("historyListenerEnabled")) @@ -122,7 +122,7 @@ function getData() changeSwitchButton("referralMarketing", "referralMarketing"); changeSwitchButton("domainBlocking", "domainBlocking"); changeSwitchButton("pingBlocking", "pingBlocking"); - }); + }).catch(handleError); } /** @@ -205,8 +205,8 @@ function exportSettings() { 'url': URL.createObjectURL(blob), 'filename': 'ClearURLs.conf', 'saveAs': true - }); - }); + }).catch(handleError); + }).catch(handleError); } /** @@ -262,14 +262,14 @@ function changeSwitchButton(id, storageID) browser.runtime.sendMessage({ function: "changeIcon", params: [] - }); + }).catch(handleError); } browser.runtime.sendMessage({ function: "saveOnExit", params: [] - }); - }); + }).catch(handleError); + }).catch(handleError); }); setSwitchButton(id, storageID); } @@ -279,7 +279,7 @@ function changeSwitchButton(id, storageID) * * @param {string} id ID of the HTML element * @param {string} attribute Name of the attribute used for localization -* @param {boolean} tooltip +* @param {string} tooltip */ function injectText(id, attribute, tooltip = "") { diff --git a/core_js/storage.js b/core_js/storage.js index ed0d0d2..65ad0bd 100644 --- a/core_js/storage.js +++ b/core_js/storage.js @@ -75,7 +75,7 @@ function saveOnDisk(keys) { }); console.log(translate('core_save_on_disk')); - browser.storage.local.set(json); + browser.storage.local.set(json).catch(handleError); } /** @@ -114,7 +114,7 @@ function genesis() { // Start history listener historyListenerStart(); - }, error); + }, handleError); } /** @@ -164,14 +164,6 @@ function setData(key, value) { } } -/** - * Write error on console. - */ -function error(e) { - console.log(translate('core_error')); - console.error(e); -} - /** * Set default values, if the storage is empty. * @param {Object} items diff --git a/core_js/tools.js b/core_js/tools.js index 227859e..8f845a3 100644 --- a/core_js/tools.js +++ b/core_js/tools.js @@ -235,9 +235,9 @@ function changeIcon() { checkOSAndroid().then((res) => { if (!res) { if (storage.globalStatus) { - browser.browserAction.setIcon({path: "img/clearurls_128x128.png"}); + browser.browserAction.setIcon({path: "img/clearurls_128x128.png"}).catch(handleError); } else { - browser.browserAction.setIcon({path: "img/clearurls_gray_128x128.png"}); + browser.browserAction.setIcon({path: "img/clearurls_gray_128x128.png"}).catch(handleError); } } }); @@ -252,11 +252,11 @@ function setBadgedStatus() { checkOSAndroid().then((res) => { if (!res && storage.badgedStatus) { let color = storage.badged_color; - if(storage.badged_color.charAt(0) !== '#') + if (storage.badged_color.charAt(0) !== '#') color = '#' + storage.badged_color; browser.browserAction.setBadgeBackgroundColor({ 'color': color - }); + }).catch(handleError); } }); } @@ -294,11 +294,15 @@ function decodeURL(url) { } /** -* Gets the value of at `key` an object. If the resolved value is `undefined`, the `defaultValue` is returned in its place. -* -* @param {string} key the key of the object -* @param {object} defaultValue the default value -*/ + * Gets the value of at `key` an object. If the resolved value is `undefined`, the `defaultValue` is returned in its place. + * + * @param {string} key the key of the object + * @param {object} defaultValue the default value + */ Object.prototype.getOrDefault = function (key, defaultValue) { return this[key] === undefined ? defaultValue : this[key]; -}; \ No newline at end of file +}; + +function handleError(error) { + console.log(translate('core_error') + ":" + error); +} \ No newline at end of file