apply.js: FF forgets the extra methods on MutationObserver

This commit is contained in:
tophf 2018-01-10 16:33:45 +03:00
parent 9aa382ebf5
commit 11dbfea897

View File

@ -177,7 +177,7 @@
// to avoid page flicker when the style is updated // to avoid page flicker when the style is updated
// instead of removing it immediately we rename its ID and queue it // instead of removing it immediately we rename its ID and queue it
// to be deleted in applyStyles after a new version is fetched and applied // to be deleted in applyStyles after a new version is fetched and applied
const deadID = 'ghost-' + id; const deadID = id + '-ghost';
el.id = ID_PREFIX + deadID; el.id = ID_PREFIX + deadID;
// in case something went wrong and new style was never applied // in case something went wrong and new style was never applied
retiredStyleTimers.set(deadID, setTimeout(removeStyle, 1000, {id: deadID})); retiredStyleTimers.set(deadID, setTimeout(removeStyle, 1000, {id: deadID}));
@ -254,7 +254,9 @@
function applySections(styleId, code) { function applySections(styleId, code) {
const id = ID_PREFIX + styleId; const id = ID_PREFIX + styleId;
let el = styleElements.get(id) || document.getElementById(id); let el = styleElements.get(id) || document.getElementById(id);
if (!el) { if (el) {
if (el.textContent !== code) el.textContent = code;
} else {
if (document.documentElement instanceof SVGSVGElement) { if (document.documentElement instanceof SVGSVGElement) {
// SVG document style // SVG document style
el = document.createElementNS('http://www.w3.org/2000/svg', 'style'); el = document.createElementNS('http://www.w3.org/2000/svg', 'style');
@ -359,7 +361,7 @@
} }
// In Chrome content script is orphaned on an extension update/reload // In Chrome content script is orphaned on an extension update/reload
// so we need to detach event listeners // so we need to detach event listeners
[docRewriteObserver, docRootObserver].forEach(ob => ob && ob.takeRecords() && ob.disconnect()); [docRewriteObserver, docRootObserver].forEach(ob => ob && ob.disconnect());
window.removeEventListener(chrome.runtime.id, orphanCheck, true); window.removeEventListener(chrome.runtime.id, orphanCheck, true);
try { try {
chrome.runtime.onMessage.removeListener(applyOnMessage); chrome.runtime.onMessage.removeListener(applyOnMessage);
@ -411,6 +413,7 @@
let restorationCounter = 0; let restorationCounter = 0;
let observing = false; let observing = false;
let sorting = false; let sorting = false;
let observer;
// allow any types of elements between ours, except for the following: // allow any types of elements between ours, except for the following:
const ORDERED_TAGS = ['head', 'body', 'frameset', 'style', 'link']; const ORDERED_TAGS = ['head', 'body', 'frameset', 'style', 'link'];
@ -418,23 +421,23 @@
return; return;
function init() { function init() {
docRootObserver = new MutationObserver(sortStyleElements); observer = new MutationObserver(sortStyleElements);
Object.assign(docRootObserver, {start, stop, evade}); docRootObserver = {start, stop, evade, disconnect: stop};
setTimeout(sortStyleElements); setTimeout(sortStyleElements);
} }
function start({sort = false} = {}) { function start({sort = false} = {}) {
if (sort && sortStyleMap()) { if (sort && sortStyleMap()) {
sortStyleElements(); sortStyleElements();
} }
if (!observing && ROOT && docRootObserver) { if (!observing && ROOT && observer) {
docRootObserver.observe(ROOT, {childList: true}); observer.observe(ROOT, {childList: true});
observing = true; observing = true;
} }
} }
function stop() { function stop() {
if (observing) { if (observing) {
docRootObserver.takeRecords(); observer.takeRecords();
docRootObserver.disconnect(); observer.disconnect();
observing = false; observing = false;
} }
} }
@ -497,7 +500,7 @@
} }
if (sorting) { if (sorting) {
sorting = false; sorting = false;
if (docRootObserver) docRootObserver.takeRecords(); if (observer) observer.takeRecords();
if (!restorationLimitExceeded()) { if (!restorationLimitExceeded()) {
start(); start();
} else { } else {
@ -518,7 +521,7 @@
function moveAfter(el, expected) { function moveAfter(el, expected) {
if (!sorting) { if (!sorting) {
sorting = true; sorting = true;
if (docRootObserver) docRootObserver.stop(); if (observer) observer.stop();
} }
expected.insertAdjacentElement('afterend', el); expected.insertAdjacentElement('afterend', el);
if (el.disabled !== disableAll) { if (el.disabled !== disableAll) {