diff --git a/background/style-manager.js b/background/style-manager.js index 44c215e0..d772c59b 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -1,6 +1,6 @@ /* eslint no-eq-null: 0, eqeqeq: [2, "smart"] */ /* global createCache db calcStyleDigest db tryRegExp styleCodeEmpty styleSectionGlobal - getStyleWithNoCode msg sync uuidv4 URLS */ + getStyleWithNoCode msg prefs sync uuidv4 URLS */ /* exported styleManager */ 'use strict'; @@ -479,7 +479,7 @@ const styleManager = (() => { return result; } - function getSectionsByUrl(url, id) { + function getSectionsByUrl(url, id, isInitialApply) { let cache = cachedStyleForUrl.get(url); if (!cache) { cache = { @@ -495,13 +495,13 @@ const styleManager = (() => { .map(i => styles.get(i)) ); } - if (id) { - if (cache.sections[id]) { - return {[id]: cache.sections[id]}; - } - return {}; - } - return cache.sections; + const res = id + ? cache.sections[id] ? {[id]: cache.sections[id]} : {} + : cache.sections; + // Avoiding flicker of needlessly applied styles by providing both styles & pref in one API call + return isInitialApply && prefs.get('disableAll') + ? Object.assign({disableAll: true}, res) + : res; function buildCache(styleList) { const query = createMatchQuery(url); diff --git a/background/style-via-xhr.js b/background/style-via-xhr.js index a278bd0a..dc7dc1bc 100644 --- a/background/style-via-xhr.js +++ b/background/style-via-xhr.js @@ -71,7 +71,7 @@ CHROME && (async () => { const {responseHeaders} = req; responseHeaders.push({ name: 'Set-Cookie', - value: `${chrome.runtime.id}=${blobId}`, + value: `${chrome.runtime.id}=${prefs.get('disableAll') ? 1 : 0}${blobId}`, }); return {responseHeaders}; } diff --git a/content/apply.js b/content/apply.js index 1d378347..7f2f5d3a 100644 --- a/content/apply.js +++ b/content/apply.js @@ -59,22 +59,33 @@ self.INJECTED !== 1 && (() => { if (STYLE_VIA_API) { await API.styleViaAPI({method: 'styleApply'}); } else { - const styles = chrome.app && getStylesViaXhr() || await API.getSectionsByUrl(getMatchUrl()); + const styles = chrome.app && getStylesViaXhr() || + await API.getSectionsByUrl(getMatchUrl(), null, true); + if (styles.disableAll) { + delete styles.disableAll; + styleInjector.toggle(false); + } await styleInjector.apply(styles); } } function getStylesViaXhr() { if (new RegExp(`(^|\\s|;)${chrome.runtime.id}=\\s*([-\\w]+)\\s*(;|$)`).test(document.cookie)) { - const url = 'blob:' + chrome.runtime.getURL(RegExp.$2); - const xhr = new XMLHttpRequest(); + const data = RegExp.$2; + const disableAll = data[0] === '1'; + const url = 'blob:' + chrome.runtime.getURL(data.slice(1)); document.cookie = `${chrome.runtime.id}=1; max-age=0`; // remove our cookie + let res; try { - xhr.open('GET', url, false); // synchronous - xhr.send(); + if (!disableAll) { // will get the styles asynchronously + const xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); // synchronous + xhr.send(); + res = JSON.parse(xhr.response); + } URL.revokeObjectURL(url); - return JSON.parse(xhr.response); } catch (e) {} + return res; } }