code cosmetics

This commit is contained in:
tophf 2017-04-21 11:59:01 +03:00
parent 2e60af40f0
commit 9e9723cfd2

View File

@ -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,29 +120,21 @@ 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) {
if (inDoc) {
return; return;
} } else if (inCache) {
if (enabled && !inDoc && !inCache) {
requestStyles({id});
return;
}
if (enabled && inCache) {
addStyleElement(inCache); addStyleElement(inCache);
disabledElements.delete(id); disabledElements.delete(id);
return; } else {
requestStyles({id});
} }
if (!enabled && inDoc) { } else {
if (inDoc) {
disabledElements.set(id, inDoc); disabledElements.set(id, inDoc);
inDoc.remove(); 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();
}
});
} }