diff --git a/.eslintrc b/.eslintrc index b72d8204..3d844c56 100644 --- a/.eslintrc +++ b/.eslintrc @@ -17,6 +17,7 @@ globals: URLS: false BG: false notifyAllTabs: false + sendMessage: false queryTabs: false getTab: false getOwnTab: false diff --git a/background/background.js b/background/background.js index 2a76f8ed..7bff2213 100644 --- a/background/background.js +++ b/background/background.js @@ -110,7 +110,7 @@ contextMenus = Object.assign({ contexts: ['editable'], documentUrlPatterns: [URLS.ownOrigin + 'edit*'], click: (info, tab) => { - chrome.tabs.sendMessage(tab.id, {method: 'editDeleteText'}); + sendMessage(tab.id, {method: 'editDeleteText'}); }, } }); @@ -179,15 +179,12 @@ window.addEventListener('storageReady', function _() { }; const pingCS = (cs, {id, url}) => { + const maybeInject = pong => !pong && injectCS(cs, PING.tabId); cs.matches.some(match => { - if ((match === ALL_URLS || url.match(match)) - && (!url.startsWith('chrome') || url === NTP)) { - chrome.tabs.sendMessage(id, PING, pong => { - if (!pong) { - injectCS(cs, id); - } - ignoreChromeError(); - }); + if ((match === ALL_URLS || url.match(match)) && + (!url.startsWith('chrome') || url === NTP)) { + PING.tabId = id; + sendMessage(PING).then(maybeInject); return true; } }); @@ -211,13 +208,13 @@ function webNavigationListener(method, {url, tabId, frameId}) { if (method === 'styleApply') { handleCssTransitionBug({tabId, frameId, url, styles}); } - chrome.tabs.sendMessage(tabId, { + sendMessage({ + tabId, + frameId, method, // ping own page so it retrieves the styles directly styles: url.startsWith(URLS.ownOrigin) ? 'DIY' : styles, - }, { - frameId - }, ignoreChromeError); + }); } // main page frame id is 0 if (frameId === 0) { @@ -303,9 +300,15 @@ function updateIcon(tab, styles) { } -function onRuntimeMessage(request, sender, sendResponse) { - // prevent browser exception bug on sending a response to a closed tab - sendResponse = (send => data => tryCatch(send, data))(sendResponse); +function onRuntimeMessage(request, sender, sendResponseInternal) { + const sendResponse = data => { + // wrap Error object instance as {__ERROR__: message} - will be unwrapped in sendMessage + if (data instanceof Error) { + data = {__ERROR__: data.message}; + } + // prevent browser exception bug on sending a response to a closed tab + tryCatch(sendResponseInternal, data); + }; switch (request.method) { case 'getStyles': getStyles(request).then(sendResponse); @@ -353,6 +356,7 @@ function onRuntimeMessage(request, sender, sendResponse) { } } + function closeTab(tabId, request) { return new Promise(resolve => { if (request.tabId) { diff --git a/background/usercss-helper.js b/background/usercss-helper.js index dccc7a76..7edbeb96 100644 --- a/background/usercss-helper.js +++ b/background/usercss-helper.js @@ -23,11 +23,7 @@ var usercssHelper = (() => { function wrapReject(pending) { return pending - .then(result => ({success: true, result})) - .catch(err => ({ - success: false, - result: Array.isArray(err) ? err.join('\n') : err.message || String(err), - })); + .catch(err => new Error(Array.isArray(err) ? err.join('\n') : err.message || String(err))); } // Parse the source and find the duplication diff --git a/content/install-user-css.js b/content/install-hook-usercss.js similarity index 94% rename from content/install-user-css.js rename to content/install-hook-usercss.js index 150199c2..60156aff 100644 --- a/content/install-user-css.js +++ b/content/install-hook-usercss.js @@ -1,4 +1,3 @@ -/* global runtimeSend */ 'use strict'; function createSourceLoader() { @@ -95,16 +94,16 @@ function initUsercssInstall() { if (history.length > 1) { history.back(); } else { - runtimeSend({method: 'closeTab'}); + chrome.runtime.sendMessage({method: 'closeTab'}); } break; } }); }); - return runtimeSend({ + chrome.runtime.sendMessage({ method: 'openUsercssInstallPage', - updateUrl: location.href - }).catch(alert); + updateUrl: location.href, + }, r => r && r.__ERROR__ && alert(r.__ERROR__)); } function isUsercss() { diff --git a/content/install.js b/content/install-hook-userstyles.js similarity index 100% rename from content/install.js rename to content/install-hook-userstyles.js diff --git a/content/util.js b/content/util.js deleted file mode 100644 index 34160e87..00000000 --- a/content/util.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -function runtimeSend(request) { - return new Promise((resolve, reject) => { - chrome.runtime.sendMessage( - request, - ({success, result}) => (success ? resolve : reject)(result) - ); - }); -} diff --git a/install-usercss.html b/install-usercss.html index 7167462d..73c5cf79 100644 --- a/install-usercss.html +++ b/install-usercss.html @@ -85,7 +85,6 @@
-