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"] */
/* 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);

View File

@ -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};
}

View File

@ -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;
}
}