simplify and speed up USO rebrand observer

This commit is contained in:
tophf 2017-04-20 01:19:33 +03:00
parent a80c677b3e
commit 98c34da9e7

View File

@ -21,12 +21,11 @@ function waitForBody() {
this.disconnect(); this.disconnect();
rebrand([{addedNodes: [document.body]}]); rebrand([{addedNodes: [document.body]}]);
const rebrandObserver = new MutationObserver(rebrand); new MutationObserver(rebrand)
rebrandObserver.observe(document.body, {childList: true, subtree: true}); .observe(document.body, {childList: true, subtree: true});
document.addEventListener('DOMContentLoaded', function _() { document.addEventListener('DOMContentLoaded', function _() {
document.removeEventListener('DOMContentLoaded', _); document.removeEventListener('DOMContentLoaded', _);
rebrandObserver.disconnect();
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
method: 'getStyles', method: 'getStyles',
url: getMeta('stylish-id-url') || location.href url: getMeta('stylish-id-url') || location.href
@ -141,19 +140,15 @@ function getResource(url) {
} }
function rebrand(mutations) { function rebrand(mutations, observer) {
/* stylish to stylus; https://github.com/schomery/stylish-chrome/issues/12 */ /* stylish to stylus; https://github.com/schomery/stylish-chrome/issues/12 */
for (let m = mutations.length; --m >= 0;) { if (!document.getElementById('hidden-meta') && document.readyState == 'loading') {
const added = mutations[m].addedNodes; return;
for (let n = added.length; --n >= 0;) {
const addedNode = added[n];
if (addedNode.nodeType != Node.ELEMENT_NODE) {
continue;
} }
const elementsToCheck = addedNode.matches('.install-status') ? [addedNode] observer.disconnect();
: addedNode.getElementsByClassName('install-status'); const elements = document.getElementsByClassName('install-status');
for (let i = elementsToCheck.length; --i >= 0;) { for (let i = elements.length; --i >= 0;) {
const el = elementsToCheck[i]; const el = elements[i];
if (!el.textContent.includes('Stylish')) { if (!el.textContent.includes('Stylish')) {
continue; continue;
} }
@ -167,8 +162,6 @@ function rebrand(mutations) {
} }
} }
} }
}
}
function styleSectionsEqual({sections: a}, {sections: b}) { function styleSectionsEqual({sections: a}, {sections: b}) {