ignore style messages if started in disableAll mode

This commit is contained in:
tophf 2021-02-22 00:32:22 +03:00
parent c5e2baaf87
commit b61cd75b25

View File

@ -6,7 +6,12 @@
(() => { (() => {
if (window.INJECTED === 1) return; if (window.INJECTED === 1) return;
/** true -> when the page styles are received,
* false -> when disableAll mode is on at start, the styles won't be sent
* so while disableAll lasts we can ignore messages about style updates because
* the tab will explicitly ask for all styles in bulk when disableAll mode ends */
let hasStyles = false; let hasStyles = false;
let isDisabled = false;
let isTab = !chrome.tabs || location.pathname !== '/popup.html'; let isTab = !chrome.tabs || location.pathname !== '/popup.html';
const isFrame = window !== parent; const isFrame = window !== parent;
const isFrameAboutBlank = isFrame && location.href === 'about:blank'; const isFrameAboutBlank = isFrame && location.href === 'about:blank';
@ -75,7 +80,8 @@
parentStyles && await new Promise(requestAnimationFrame) && parentStyles || parentStyles && await new Promise(requestAnimationFrame) && parentStyles ||
!isFrameAboutBlank && chrome.app && !chrome.tabs && tryCatch(getStylesViaXhr) || !isFrameAboutBlank && chrome.app && !chrome.tabs && tryCatch(getStylesViaXhr) ||
await API.styles.getSectionsByUrl(matchUrl, null, true); await API.styles.getSectionsByUrl(matchUrl, null, true);
hasStyles = !styles.disableAll; isDisabled = styles.disableAll;
hasStyles = !isDisabled;
if (hasStyles) { if (hasStyles) {
window[SYM] = styles; window[SYM] = styles;
await styleInjector.apply(styles); await styleInjector.apply(styles);
@ -121,6 +127,7 @@
break; break;
case 'styleUpdated': case 'styleUpdated':
if (!hasStyles && isDisabled) break;
if (style.enabled) { if (style.enabled) {
API.styles.getSectionsByUrl(matchUrl, style.id).then(sections => API.styles.getSectionsByUrl(matchUrl, style.id).then(sections =>
sections[style.id] sections[style.id]
@ -132,6 +139,7 @@
break; break;
case 'styleAdded': case 'styleAdded':
if (!hasStyles && isDisabled) break;
if (style.enabled) { if (style.enabled) {
API.styles.getSectionsByUrl(matchUrl, style.id) API.styles.getSectionsByUrl(matchUrl, style.id)
.then(styleInjector.apply); .then(styleInjector.apply);
@ -139,6 +147,7 @@
break; break;
case 'urlChanged': case 'urlChanged':
if (!hasStyles && isDisabled) break;
API.styles.getSectionsByUrl(matchUrl).then(sections => { API.styles.getSectionsByUrl(matchUrl).then(sections => {
hasStyles = true; hasStyles = true;
styleInjector.replace(sections); styleInjector.replace(sections);
@ -159,6 +168,7 @@
} }
function updateDisableAll(key, disableAll) { function updateDisableAll(key, disableAll) {
isDisabled = disableAll;
if (isUnstylable) { if (isUnstylable) {
API.styleViaAPI({method: 'prefChanged', prefs: {disableAll}}); API.styleViaAPI({method: 'prefChanged', prefs: {disableAll}});
} else if (!hasStyles && !disableAll) { } else if (!hasStyles && !disableAll) {