kick out orphaned script

This commit is contained in:
tophf 2017-11-25 23:29:59 +03:00
parent e4de02995c
commit babeb695c1

View File

@ -1,17 +1,15 @@
'use strict'; 'use strict';
const CHROMIUM = chrome.app && /Chromium/.test(navigator.userAgent); // non-Windows Chromium
const FIREFOX = !chrome.app; const FIREFOX = !chrome.app;
const VIVALDI = chrome.app && /Vivaldi/.test(navigator.userAgent); const VIVALDI = chrome.app && /Vivaldi/.test(navigator.userAgent);
const OPERA = chrome.app && /OPR/.test(navigator.userAgent); const OPERA = chrome.app && /OPR/.test(navigator.userAgent);
document.addEventListener('stylishUpdate', onClick); window.dispatchEvent(new CustomEvent(chrome.runtime.id + '-install'));
document.addEventListener('stylishUpdateChrome', onClick); window.addEventListener(chrome.runtime.id + '-install', orphanCheck, true);
document.addEventListener('stylishUpdateOpera', onClick);
document.addEventListener('stylishInstall', onClick); ['Update', 'Install'].forEach(type =>
document.addEventListener('stylishInstallChrome', onClick); ['', 'Chrome', 'Opera'].forEach(browser =>
document.addEventListener('stylishInstallOpera', onClick); document.addEventListener('stylish' + type + browser, onClick)));
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
// orphaned content script check // orphaned content script check
@ -190,7 +188,7 @@ function sendEvent(type, detail = null) {
function onClick(event) { function onClick(event) {
if (onClick.processing || !orphanCheck || !orphanCheck()) { if (onClick.processing) {
return; return;
} }
onClick.processing = true; onClick.processing = true;
@ -361,27 +359,8 @@ function orphanCheck() {
} }
// we're orphaned due to an extension update // we're orphaned due to an extension update
// we can detach event listeners // we can detach event listeners
document.removeEventListener('stylishUpdate', onClick); window.removeEventListener(chrome.runtime.id + '-install', orphanCheck, true);
document.removeEventListener('stylishUpdateChrome', onClick); ['Update', 'Install'].forEach(type =>
document.removeEventListener('stylishUpdateOpera', onClick); ['', 'Chrome', 'Opera'].forEach(browser =>
document.addEventListener('stylish' + type + browser, onClick)));
document.removeEventListener('stylishInstall', onClick);
document.removeEventListener('stylishInstallChrome', onClick);
document.removeEventListener('stylishInstallOpera', onClick);
// 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
[
'checkUpdatability',
'getMeta',
'getResource',
'onDOMready',
'onClick',
'onInstall',
'onUpdate',
'orphanCheck',
'saveStyleCode',
'sendEvent',
'styleSectionsEqual',
].forEach(fn => (window[fn] = null));
} }