From 93f438f55ebd8d1f558f2e73b64b3f87e928bf02 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 11 Sep 2019 08:00:55 -0400 Subject: [PATCH] Add advanced setting for extension reload on update Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/717 Related feedback: - https://github.com/uBlockOrigin/uBlock-issues/issues/717#issuecomment-530275655 New advanced setting: `extensionUpdateForceReload` Default value: `false` If set to `true`, the extension will unconditionally reload when an update is available; otherwise the extension will reload only when being explicitly disabled then enabled, or when the browser is restarted. --- platform/chromium/vapi-background.js | 8 ---- src/js/background.js | 1 + src/js/start.js | 55 +++++++++++++++------------- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index c2f36952567b5..a805c35f017eb 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -104,14 +104,6 @@ vAPI.app = { }, }; -// https://github.com/uBlockOrigin/uBlock-issues/issues/717 -// Prevent the extensions from being restarted mid-session. -browser.runtime.onUpdateAvailable.addListener(details => { - const toInt = vAPI.app.intFromVersion; - if ( toInt(details.version) > toInt(vAPI.app.version) ) { return; } - browser.runtime.reload(); -}); - /******************************************************************************/ /******************************************************************************/ diff --git a/src/js/background.js b/src/js/background.js index 172abec108572..2f00d34468237 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -50,6 +50,7 @@ const µBlock = (function() { // jshint ignore:line debugScriptlets: false, debugScriptletInjector: false, disableWebAssembly: false, + extensionUpdateForceReload: false, ignoreRedirectFilters: false, ignoreScriptInjectFilters: false, loggerPopupType: 'popup', diff --git a/src/js/start.js b/src/js/start.js index d5804ccc9fe16..561da8959ee84 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -25,15 +25,15 @@ // Load all: executed once. -µBlock.restart = (function() { - -/******************************************************************************/ +{ +// >>>>> start of local scope const µb = µBlock; /******************************************************************************/ vAPI.app.onShutdown = function() { + const µb = µBlock; µb.staticFilteringReverseLookup.shutdown(); µb.assets.updateStop(); µb.staticNetFilteringEngine.reset(); @@ -52,7 +52,7 @@ vAPI.app.onShutdown = function() { // - Initialize internal state with maybe already existing tabs. // - Schedule next update operation. -var onAllReady = function() { +const onAllReady = function() { µb.webRequest.start(); // Ensure that the resources allocated for decompression purpose (likely @@ -86,6 +86,18 @@ var onAllReady = function() { µb.contextMenu.update(null); µb.firstInstall = false; + // https://github.com/uBlockOrigin/uBlock-issues/issues/717 + // Prevent the extensions from being restarted mid-session. + browser.runtime.onUpdateAvailable.addListener(details => { + const toInt = vAPI.app.intFromVersion; + if ( + µBlock.hiddenSettings.extensionUpdateForceReload === true || + toInt(details.version) <= toInt(vAPI.app.version) + ) { + vAPI.app.restart(); + } + }); + log.info(`All ready ${Date.now()-vAPI.T0} ms after launch`); }; @@ -96,8 +108,8 @@ var onAllReady = function() { // in already opened web pages, to remove whatever nuisance could make it to // the web pages before uBlock was ready. -let initializeTabs = function() { - let handleScriptResponse = function(tabId, results) { +const initializeTabs = function() { + const handleScriptResponse = function(tabId, results) { if ( Array.isArray(results) === false || results.length === 0 || @@ -106,10 +118,10 @@ let initializeTabs = function() { return; } // Inject dclarative content scripts programmatically. - let manifest = chrome.runtime.getManifest(); + const manifest = chrome.runtime.getManifest(); if ( manifest instanceof Object === false ) { return; } - for ( let contentScript of manifest.content_scripts ) { - for ( let file of contentScript.js ) { + for ( const contentScript of manifest.content_scripts ) { + for ( const file of contentScript.js ) { vAPI.tabs.injectScript(tabId, { file: file, allFrames: contentScript.all_frames, @@ -118,8 +130,8 @@ let initializeTabs = function() { } } }; - let bindToTabs = function(tabs) { - for ( let tab of tabs ) { + const bindToTabs = function(tabs) { + for ( const tab of tabs ) { µb.tabContextManager.commit(tab.id, tab.url); µb.bindTabToPageStats(tab.id); // https://github.com/chrisaljoudi/uBlock/issues/129 @@ -241,7 +253,7 @@ const onUserSettingsReady = function(fetched) { // Housekeeping, as per system setting changes const onSystemSettingsReady = function(fetched) { - var mustSaveSystemSettings = false; + let mustSaveSystemSettings = false; if ( fetched.compiledMagic !== µb.systemSettings.compiledMagic ) { µb.assets.remove(/^compiled\//); mustSaveSystemSettings = true; @@ -367,17 +379,10 @@ const onAdminSettingsRestored = function() { /******************************************************************************/ -return function() { - // https://github.com/gorhill/uBlock/issues/531 - µb.restoreAdminSettings().then(( ) => { - onAdminSettingsRestored(); - }); -}; - -/******************************************************************************/ - -})(); - -/******************************************************************************/ +// https://github.com/gorhill/uBlock/issues/531 +µb.restoreAdminSettings().then(( ) => { + onAdminSettingsRestored(); +}); -µBlock.restart(); +// <<<<< end of local scope +}