get disableAll pref earlier, fixes #1074

This commit is contained in:
tophf 2020-10-22 22:58:11 +03:00
parent f9804036b2
commit 6593d5c05a
3 changed files with 27 additions and 16 deletions

View File

@ -1,6 +1,6 @@
/* eslint no-eq-null: 0, eqeqeq: [2, "smart"] */ /* eslint no-eq-null: 0, eqeqeq: [2, "smart"] */
/* global createCache db calcStyleDigest db tryRegExp styleCodeEmpty styleSectionGlobal /* global createCache db calcStyleDigest db tryRegExp styleCodeEmpty styleSectionGlobal
getStyleWithNoCode msg sync uuidv4 URLS */ getStyleWithNoCode msg prefs sync uuidv4 URLS */
/* exported styleManager */ /* exported styleManager */
'use strict'; 'use strict';
@ -479,7 +479,7 @@ const styleManager = (() => {
return result; return result;
} }
function getSectionsByUrl(url, id) { function getSectionsByUrl(url, id, isInitialApply) {
let cache = cachedStyleForUrl.get(url); let cache = cachedStyleForUrl.get(url);
if (!cache) { if (!cache) {
cache = { cache = {
@ -495,13 +495,13 @@ const styleManager = (() => {
.map(i => styles.get(i)) .map(i => styles.get(i))
); );
} }
if (id) { const res = id
if (cache.sections[id]) { ? cache.sections[id] ? {[id]: cache.sections[id]} : {}
return {[id]: cache.sections[id]}; : cache.sections;
} // Avoiding flicker of needlessly applied styles by providing both styles & pref in one API call
return {}; return isInitialApply && prefs.get('disableAll')
} ? Object.assign({disableAll: true}, res)
return cache.sections; : res;
function buildCache(styleList) { function buildCache(styleList) {
const query = createMatchQuery(url); const query = createMatchQuery(url);

View File

@ -71,7 +71,7 @@ CHROME && (async () => {
const {responseHeaders} = req; const {responseHeaders} = req;
responseHeaders.push({ responseHeaders.push({
name: 'Set-Cookie', name: 'Set-Cookie',
value: `${chrome.runtime.id}=${blobId}`, value: `${chrome.runtime.id}=${prefs.get('disableAll') ? 1 : 0}${blobId}`,
}); });
return {responseHeaders}; return {responseHeaders};
} }

View File

@ -59,22 +59,33 @@ self.INJECTED !== 1 && (() => {
if (STYLE_VIA_API) { if (STYLE_VIA_API) {
await API.styleViaAPI({method: 'styleApply'}); await API.styleViaAPI({method: 'styleApply'});
} else { } 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); await styleInjector.apply(styles);
} }
} }
function getStylesViaXhr() { function getStylesViaXhr() {
if (new RegExp(`(^|\\s|;)${chrome.runtime.id}=\\s*([-\\w]+)\\s*(;|$)`).test(document.cookie)) { if (new RegExp(`(^|\\s|;)${chrome.runtime.id}=\\s*([-\\w]+)\\s*(;|$)`).test(document.cookie)) {
const url = 'blob:' + chrome.runtime.getURL(RegExp.$2); const data = RegExp.$2;
const xhr = new XMLHttpRequest(); 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 document.cookie = `${chrome.runtime.id}=1; max-age=0`; // remove our cookie
let res;
try { try {
if (!disableAll) { // will get the styles asynchronously
const xhr = new XMLHttpRequest();
xhr.open('GET', url, false); // synchronous xhr.open('GET', url, false); // synchronous
xhr.send(); xhr.send();
res = JSON.parse(xhr.response);
}
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
return JSON.parse(xhr.response);
} catch (e) {} } catch (e) {}
return res;
} }
} }