disconnect an orphaned chrome.runtime message/port listener

This commit is contained in:
tophf 2018-01-10 16:44:29 +03:00
parent 885919e7d9
commit 9aa382ebf5
3 changed files with 23 additions and 12 deletions

View File

@ -361,6 +361,9 @@
// so we need to detach event listeners // so we need to detach event listeners
[docRewriteObserver, docRootObserver].forEach(ob => ob && ob.takeRecords() && ob.disconnect()); [docRewriteObserver, docRootObserver].forEach(ob => ob && ob.takeRecords() && ob.disconnect());
window.removeEventListener(chrome.runtime.id, orphanCheck, true); window.removeEventListener(chrome.runtime.id, orphanCheck, true);
try {
chrome.runtime.onMessage.removeListener(applyOnMessage);
} catch (e) {}
} }
function initDocRewriteObserver() { function initDocRewriteObserver() {

View File

@ -103,6 +103,9 @@
// In Chrome content script is orphaned on an extension update/reload // In Chrome content script is orphaned on an extension update/reload
// so we need to detach event listeners // so we need to detach event listeners
removeEventListener(eventName, orphanCheckRequest, true); removeEventListener(eventName, orphanCheckRequest, true);
try {
chrome.runtime.onConnect.removeListener(onConnected);
} catch (e) {}
}; };
dispatchEvent(new Event(eventName)); dispatchEvent(new Event(eventName));
addEventListener(eventName, orphanCheckRequest, true); addEventListener(eventName, orphanCheckRequest, true);

View File

@ -13,18 +13,7 @@
['', 'Chrome', 'Opera'].forEach(browser => ['', 'Chrome', 'Opera'].forEach(browser =>
document.addEventListener('stylish' + type + browser, onClick))); document.addEventListener('stylish' + type + browser, onClick)));
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { chrome.runtime.onMessage.addListener(onMessage);
switch (msg.method) {
case 'ping':
// orphaned content script check
sendResponse(true);
break;
case 'openSettings':
openSettings();
sendResponse(true);
break;
}
});
new MutationObserver((mutations, observer) => { new MutationObserver((mutations, observer) => {
if (document.body) { if (document.body) {
@ -38,6 +27,19 @@
} }
}).observe(document.documentElement, {childList: true}); }).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 /* 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, 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 we need to fix this URL using "stylish-update-url" meta key
@ -304,6 +306,9 @@
['Update', 'Install'].forEach(type => ['Update', 'Install'].forEach(type =>
['', 'Chrome', 'Opera'].forEach(browser => ['', 'Chrome', 'Opera'].forEach(browser =>
document.addEventListener('stylish' + type + browser, onClick))); document.addEventListener('stylish' + type + browser, onClick)));
try {
chrome.runtime.onMessage.addListener(onMessage);
} catch (e) {}
} }
})(); })();