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,30 +140,24 @@ 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]; observer.disconnect();
if (addedNode.nodeType != Node.ELEMENT_NODE) { const elements = document.getElementsByClassName('install-status');
continue; for (let i = elements.length; --i >= 0;) {
} const el = elements[i];
const elementsToCheck = addedNode.matches('.install-status') ? [addedNode] if (!el.textContent.includes('Stylish')) {
: addedNode.getElementsByClassName('install-status'); continue;
for (let i = elementsToCheck.length; --i >= 0;) { }
const el = elementsToCheck[i]; const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
if (!el.textContent.includes('Stylish')) { while (walker.nextNode()) {
continue; const node = walker.currentNode;
} const text = node.nodeValue;
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT); if (text.includes('Stylish') && node.parentNode.localName != 'a') {
while (walker.nextNode()) { node.nodeValue = text.replace(/Stylish/g, 'Stylus');
const node = walker.currentNode;
const text = node.nodeValue;
if (text.includes('Stylish') && node.parentNode.localName != 'a') {
node.nodeValue = text.replace(/Stylish/g, 'Stylus');
}
}
} }
} }
} }