diff --git a/content/apply.js b/content/apply.js index 03df8327..be44759a 100644 --- a/content/apply.js +++ b/content/apply.js @@ -361,6 +361,9 @@ // so we need to detach event listeners [docRewriteObserver, docRootObserver].forEach(ob => ob && ob.takeRecords() && ob.disconnect()); window.removeEventListener(chrome.runtime.id, orphanCheck, true); + try { + chrome.runtime.onMessage.removeListener(applyOnMessage); + } catch (e) {} } function initDocRewriteObserver() { diff --git a/content/install-hook-usercss.js b/content/install-hook-usercss.js index cd2b1c1c..605385d7 100644 --- a/content/install-hook-usercss.js +++ b/content/install-hook-usercss.js @@ -103,6 +103,9 @@ // In Chrome content script is orphaned on an extension update/reload // so we need to detach event listeners removeEventListener(eventName, orphanCheckRequest, true); + try { + chrome.runtime.onConnect.removeListener(onConnected); + } catch (e) {} }; dispatchEvent(new Event(eventName)); addEventListener(eventName, orphanCheckRequest, true); diff --git a/content/install-hook-userstyles.js b/content/install-hook-userstyles.js index 99f9c7b5..d565162d 100644 --- a/content/install-hook-userstyles.js +++ b/content/install-hook-userstyles.js @@ -13,18 +13,7 @@ ['', 'Chrome', 'Opera'].forEach(browser => document.addEventListener('stylish' + type + browser, onClick))); - chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { - switch (msg.method) { - case 'ping': - // orphaned content script check - sendResponse(true); - break; - case 'openSettings': - openSettings(); - sendResponse(true); - break; - } - }); + chrome.runtime.onMessage.addListener(onMessage); new MutationObserver((mutations, observer) => { if (document.body) { @@ -38,6 +27,19 @@ } }).observe(document.documentElement, {childList: true}); + function onMessage(msg, sender, sendResponse) { + switch (msg.method) { + case 'ping': + // orphaned content script check + sendResponse(true); + break; + case 'openSettings': + openSettings(); + sendResponse(true); + break; + } + } + /* since we are using "stylish-code-chrome" meta key on all browsers and US.o does not provide "advanced settings" on this url if browser is not Chrome, we need to fix this URL using "stylish-update-url" meta key @@ -304,6 +306,9 @@ ['Update', 'Install'].forEach(type => ['', 'Chrome', 'Opera'].forEach(browser => document.addEventListener('stylish' + type + browser, onClick))); + try { + chrome.runtime.onMessage.addListener(onMessage); + } catch (e) {} } })();