applyStyleState: use a separate cache for disabled styles
This commit is contained in:
parent
494b23e3fd
commit
071a4d6f26
37
apply.js
37
apply.js
|
@ -6,6 +6,7 @@
|
||||||
var isOwnPage = location.href.startsWith('chrome-extension:');
|
var isOwnPage = location.href.startsWith('chrome-extension:');
|
||||||
var disableAll = false;
|
var disableAll = false;
|
||||||
var styleElements = new Map();
|
var styleElements = new Map();
|
||||||
|
var disabledElements = new Map();
|
||||||
var retiredStyleIds = [];
|
var retiredStyleIds = [];
|
||||||
var iframeObserver;
|
var iframeObserver;
|
||||||
var docRewriteObserver;
|
var docRewriteObserver;
|
||||||
|
@ -126,22 +127,41 @@ function doDisableAll(disable) {
|
||||||
|
|
||||||
|
|
||||||
function applyStyleState(id, enabled, doc) {
|
function applyStyleState(id, enabled, doc) {
|
||||||
const el = doc.getElementById('stylus-' + id);
|
const inCache = disabledElements.get(id);
|
||||||
if (el) {
|
const inDoc = doc.getElementById('stylus-' + id);
|
||||||
el.sheet.disabled = !enabled;
|
if (enabled && inDoc || !enabled && !inDoc) {
|
||||||
processDynamicIFrames(doc, applyStyleState, id, enabled);
|
return;
|
||||||
} else if (enabled) {
|
}
|
||||||
|
if (enabled && !inDoc && !inCache) {
|
||||||
requestStyles({id});
|
requestStyles({id});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (enabled && inCache) {
|
||||||
|
const el = inCache.cloneNode(true);
|
||||||
|
document.documentElement.appendChild(el);
|
||||||
|
el.sheet.disabled = disableAll;
|
||||||
|
processDynamicIFrames(doc, applyStyleState, id, enabled);
|
||||||
|
disabledElements.delete(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!enabled && inDoc) {
|
||||||
|
disabledElements.set(id, inDoc);
|
||||||
|
inDoc.remove();
|
||||||
|
processDynamicIFrames(doc, applyStyleState, id, enabled);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function removeStyle(id, doc) {
|
function removeStyle(id, doc) {
|
||||||
styleElements.delete('stylus-' + id);
|
|
||||||
[doc.getElementById('stylus-' + id)].forEach(e => e && e.remove());
|
[doc.getElementById('stylus-' + id)].forEach(e => e && e.remove());
|
||||||
if (doc == document && !styleElements.size) {
|
if (doc == document) {
|
||||||
|
styleElements.delete('stylus-' + id);
|
||||||
|
disabledElements.delete(id);
|
||||||
|
if (!styleElements.size) {
|
||||||
iframeObserver.disconnect();
|
iframeObserver.disconnect();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
processDynamicIFrames(doc, removeStyle, id);
|
processDynamicIFrames(doc, removeStyle, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,6 +175,7 @@ function retireStyle(id, doc) {
|
||||||
doc = document;
|
doc = document;
|
||||||
retiredStyleIds.push(deadID);
|
retiredStyleIds.push(deadID);
|
||||||
styleElements.delete('stylus-' + id);
|
styleElements.delete('stylus-' + id);
|
||||||
|
disabledElements.delete(id);
|
||||||
// in case something went wrong and new style was never applied
|
// in case something went wrong and new style was never applied
|
||||||
setTimeout(removeStyle, 1000, deadID, doc);
|
setTimeout(removeStyle, 1000, deadID, doc);
|
||||||
}
|
}
|
||||||
|
@ -232,6 +253,7 @@ function applySections(styleId, sections) {
|
||||||
el.appendChild(document.createTextNode(sections.map(section => section.code).join('\n')));
|
el.appendChild(document.createTextNode(sections.map(section => section.code).join('\n')));
|
||||||
addStyleElement(el, document);
|
addStyleElement(el, document);
|
||||||
styleElements.set(el.id, el);
|
styleElements.set(el.id, el);
|
||||||
|
disabledElements.delete(styleId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -322,6 +344,7 @@ function replaceAll(newStyles, doc) {
|
||||||
processDynamicIFrames(doc, replaceAll, newStyles);
|
processDynamicIFrames(doc, replaceAll, newStyles);
|
||||||
if (doc == document) {
|
if (doc == document) {
|
||||||
styleElements.clear();
|
styleElements.clear();
|
||||||
|
disabledElements.clear();
|
||||||
applyStyles(newStyles);
|
applyStyles(newStyles);
|
||||||
replaceAllpass2(newStyles, doc);
|
replaceAllpass2(newStyles, doc);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user