From db560f3ede56e8d257e7a7224d09a58e658cd8fc Mon Sep 17 00:00:00 2001 From: tophf Date: Sun, 19 Mar 2017 04:58:16 +0300 Subject: [PATCH] Orphaned content script case self-destruction for install.js --- install.js | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/install.js b/install.js index c0ed018c..cc1ffb7d 100644 --- a/install.js +++ b/install.js @@ -69,7 +69,9 @@ function sendEvent(type, data) { document.dispatchEvent(stylishEvent); } -document.addEventListener("stylishInstallChrome", function() { +document.addEventListener("stylishUpdateChrome", stylishUpdateChrome); +function stylishInstallChrome() { + orphanCheck(); getResource(getMeta("stylish-description"), function(name) { if (confirm(chrome.i18n.getMessage('styleInstall', [name]))) { getResource(getMeta("stylish-code-chrome"), function(code) { @@ -83,9 +85,11 @@ document.addEventListener("stylishInstallChrome", function() { getResource(getMeta("stylish-install-ping-url-chrome")); } }); -}, false); +} -document.addEventListener("stylishUpdateChrome", function() { +document.addEventListener("stylishInstallChrome", stylishInstallChrome); +function stylishUpdateChrome() { + orphanCheck(); chrome.runtime.sendMessage({method: "getStyles", url: getMeta("stylish-id-url") || location.href}, function(response) { var style = response[0]; if (confirm(chrome.i18n.getMessage('styleUpdate', [style.name]))) { @@ -99,8 +103,7 @@ document.addEventListener("stylishUpdateChrome", function() { }); } }); -}, false); - +} function getMeta(name) { var e = document.querySelector("link[rel='" + name + "']"); @@ -146,3 +149,32 @@ function getResource(url, callback) { ...document.querySelectorAll('div[id^="stylish-installed-style-not-installed-"]'), ...document.querySelectorAll('div[id^="stylish-installed-style-needs-update-"]') ]); + +// orphaned content script check + +chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => + msg.method == 'ping' && sendResponse(true)); + +function orphanCheck() { + var port = chrome.runtime.connect(); + if (port) { + port.disconnect(); + return; + } + // we're orphaned due to an extension update + // we can detach event listeners + document.removeEventListener('stylishUpdateChrome', stylishUpdateChrome); + document.removeEventListener('stylishInstallChrome', stylishInstallChrome); + // we can't detach chrome.runtime.onMessage because it's no longer connected internally + // we can destroy global functions in this context to free up memory + [ + 'arraysAreEqual', + 'getMeta', + 'getResource', + 'orphanCheck', + 'sectionsAreEqual', + 'sendEvent', + 'stylishUpdateChrome', + 'stylishInstallChrome' + ].forEach(fn => window[fn] = null); +}