skip styleReplaceAll if same; add some safety checks

This commit is contained in:
tophf 2017-12-09 20:34:09 +03:00
parent 491fcef08b
commit cb32b9debf

View File

@ -2,6 +2,10 @@
'use strict'; 'use strict';
(() => { (() => {
if (typeof window.applyOnMessage === 'function') {
// some weird bug in new Chrome: the content script gets injected multiple times
return;
}
var ID_PREFIX = 'stylus-'; var ID_PREFIX = 'stylus-';
var ROOT = document.documentElement; var ROOT = document.documentElement;
var isOwnPage = location.protocol.endsWith('-extension:'); var isOwnPage = location.protocol.endsWith('-extension:');
@ -310,6 +314,14 @@
function replaceAll(newStyles) { function replaceAll(newStyles) {
if ('disableAll' in newStyles &&
disableAll === newStyles.disableAll &&
[...styleElements.values()].every(el =>
el.disabled === disableAll &&
el.parentNode === ROOT &&
el.textContent === (newStyles[getStyleId(el)] || []).map(({code}) => code).join('\n'))) {
return;
}
const oldStyles = Array.prototype.slice.call( const oldStyles = Array.prototype.slice.call(
document.querySelectorAll(`style.stylus[id^="${ID_PREFIX}"]`)); document.querySelectorAll(`style.stylus[id^="${ID_PREFIX}"]`));
oldStyles.forEach(el => (el.id += '-ghost')); oldStyles.forEach(el => (el.id += '-ghost'));
@ -318,7 +330,8 @@
[...retiredStyleTimers.values()].forEach(clearTimeout); [...retiredStyleTimers.values()].forEach(clearTimeout);
retiredStyleTimers.clear(); retiredStyleTimers.clear();
applyStyles(newStyles); applyStyles(newStyles);
oldStyles.forEach(el => el.remove()); docRootObserver.evade(() =>
oldStyles.forEach(el => el.remove()));
} }
@ -439,6 +452,9 @@
} }
} }
function sortStyleElements() { function sortStyleElements() {
if (!observing) {
return;
}
let prevExpected = document.documentElement.lastElementChild; let prevExpected = document.documentElement.lastElementChild;
while (prevExpected && isSkippable(prevExpected, true)) { while (prevExpected && isSkippable(prevExpected, true)) {
prevExpected = prevExpected.previousElementSibling; prevExpected = prevExpected.previousElementSibling;