code cosmetics
This commit is contained in:
parent
2e60af40f0
commit
9e9723cfd2
52
apply.js
52
apply.js
|
@ -37,10 +37,7 @@ function requestStyles(options, callback = applyStyles) {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
asHash: true,
|
asHash: true,
|
||||||
}, options);
|
}, options);
|
||||||
// If this is a Stylish page (Edit Style or Manage Styles),
|
// On own pages we request the styles directly to minimize delay and flicker
|
||||||
// we'll request the styles directly to minimize delay and flicker,
|
|
||||||
// unless Chrome is still starting up and the background page isn't fully loaded.
|
|
||||||
// (Note: in this case the function may be invoked again from applyStyles.)
|
|
||||||
if (typeof getStylesSafe !== 'undefined') {
|
if (typeof getStylesSafe !== 'undefined') {
|
||||||
getStylesSafe(request).then(callback);
|
getStylesSafe(request).then(callback);
|
||||||
} else {
|
} else {
|
||||||
|
@ -59,6 +56,7 @@ function applyOnMessage(request, sender, sendResponse) {
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (request.method) {
|
switch (request.method) {
|
||||||
|
|
||||||
case 'styleDeleted':
|
case 'styleDeleted':
|
||||||
|
@ -122,28 +120,20 @@ function doDisableAll(disable) {
|
||||||
function applyStyleState({id, enabled}) {
|
function applyStyleState({id, enabled}) {
|
||||||
const inCache = disabledElements.get(id) || styleElements.get(id);
|
const inCache = disabledElements.get(id) || styleElements.get(id);
|
||||||
const inDoc = document.getElementById(ID_PREFIX + id);
|
const inDoc = document.getElementById(ID_PREFIX + id);
|
||||||
if (enabled && inDoc || !enabled && !inDoc) {
|
if (enabled) {
|
||||||
return;
|
if (inDoc) {
|
||||||
}
|
return;
|
||||||
if (enabled && !inDoc && !inCache) {
|
} else if (inCache) {
|
||||||
requestStyles({id});
|
addStyleElement(inCache);
|
||||||
return;
|
disabledElements.delete(id);
|
||||||
}
|
} else {
|
||||||
if (enabled && inCache) {
|
requestStyles({id});
|
||||||
addStyleElement(inCache);
|
}
|
||||||
disabledElements.delete(id);
|
} else {
|
||||||
return;
|
if (inDoc) {
|
||||||
}
|
disabledElements.set(id, inDoc);
|
||||||
if (!enabled && inDoc) {
|
inDoc.remove();
|
||||||
disabledElements.set(id, inDoc);
|
|
||||||
inDoc.remove();
|
|
||||||
if (document.location.href == 'about:srcdoc') {
|
|
||||||
const original = document.getElementById(ID_PREFIX + id);
|
|
||||||
if (original) {
|
|
||||||
original.remove();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,12 +198,13 @@ function applySections(styleId, sections) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (document.documentElement instanceof SVGSVGElement) {
|
if (document.documentElement instanceof SVGSVGElement) {
|
||||||
// SVG document, make an SVG style element.
|
// SVG document style
|
||||||
el = document.createElementNS('http://www.w3.org/2000/svg', 'style');
|
el = document.createElementNS('http://www.w3.org/2000/svg', 'style');
|
||||||
} else if (document instanceof XMLDocument) {
|
} else if (document instanceof XMLDocument) {
|
||||||
|
// XML document style
|
||||||
el = document.createElementNS('http://www.w3.org/1999/xhtml', 'style');
|
el = document.createElementNS('http://www.w3.org/1999/xhtml', 'style');
|
||||||
} else {
|
} else {
|
||||||
// This will make an HTML style element. If there's SVG embedded in an HTML document, this works on the SVG too.
|
// HTML document style; also works on HTML-embedded SVG
|
||||||
el = document.createElement('style');
|
el = document.createElement('style');
|
||||||
}
|
}
|
||||||
Object.assign(el, {
|
Object.assign(el, {
|
||||||
|
@ -242,6 +233,7 @@ function replaceAll(newStyles) {
|
||||||
oldStyles.forEach(el => (el.id += '-ghost'));
|
oldStyles.forEach(el => (el.id += '-ghost'));
|
||||||
styleElements.clear();
|
styleElements.clear();
|
||||||
disabledElements.clear();
|
disabledElements.clear();
|
||||||
|
[...retiredStyleTimers.values()].forEach(clearTimeout);
|
||||||
retiredStyleTimers.clear();
|
retiredStyleTimers.clear();
|
||||||
applyStyles(newStyles);
|
applyStyles(newStyles);
|
||||||
oldStyles.forEach(el => el.remove());
|
oldStyles.forEach(el => el.remove());
|
||||||
|
@ -273,7 +265,11 @@ function initDocRewriteObserver() {
|
||||||
});
|
});
|
||||||
docRewriteObserver.observe(document, {childList: true});
|
docRewriteObserver.observe(document, {childList: true});
|
||||||
// detect dynamic iframes rewritten after creation by the embedder i.e. externally
|
// detect dynamic iframes rewritten after creation by the embedder i.e. externally
|
||||||
setTimeout(() => document.documentElement != ROOT && reinjectStyles());
|
setTimeout(() => {
|
||||||
|
if (document.documentElement != ROOT) {
|
||||||
|
reinjectStyles();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user