Apply styles to more iframes

Previously we incorrectly assumed that each sub-element is present in addedNodes array, but actually we need to inspect each added node's children e.g. using the superfast getElementsByTagName.
This commit is contained in:
tophf 2017-03-27 22:56:16 +03:00
parent 4be9dc0413
commit 0c6b74d999

View File

@ -188,7 +188,11 @@ function applyStyles(styleHash) {
document.head.appendChild(document.getElementById(id)); document.head.appendChild(document.getElementById(id));
} }
} }
document.addEventListener('DOMContentLoaded', onDOMContentLoaded); if (document.readyState != 'loading') {
onDOMContentLoaded();
} else {
document.addEventListener('DOMContentLoaded', onDOMContentLoaded);
}
} }
if (retiredStyleIds.length) { if (retiredStyleIds.length) {
@ -330,15 +334,20 @@ function replaceAllpass2(newStyles, doc) {
// Observe dynamic IFRAMEs being added // Observe dynamic IFRAMEs being added
function initObserver() { function initObserver() {
let orphanCheckTimer; let orphanCheckTimer;
const iframesCollection = document.getElementsByTagName('iframe');
iframeObserver = new MutationObserver(function(mutations) { iframeObserver = new MutationObserver(function(mutations) {
// MutationObserver runs as a microtask so the timer won't fire
// until all queued mutations are fired
clearTimeout(orphanCheckTimer); clearTimeout(orphanCheckTimer);
// MutationObserver runs as a microtask so the timer won't fire until all queued mutations are fired
orphanCheckTimer = setTimeout(orphanCheck, 0); orphanCheckTimer = setTimeout(orphanCheck, 0);
// autoupdated HTMLCollection is superfast
if (!iframesCollection[0]) {
return;
}
// use a much faster method for very complex pages with lots of mutations
// (observer usually receives 1k-10k mutations per call)
if (mutations.length > 1000) { if (mutations.length > 1000) {
// use a much faster method for very complex pages with 100,000 mutations
// (observer usually receives 1k-10k mutations per call)
addDocumentStylesToAllIFrames(); addDocumentStylesToAllIFrames();
return; return;
} }
@ -349,20 +358,23 @@ function initObserver() {
}); });
function process(mutations) { function process(mutations) {
// var is slightly faster and MutationObserver may run a lot /* eslint-disable no-var # var is slightly faster and MutationObserver may run a lot */
// eslint-disable-next-line no-var for (var m = 0, mutation; (mutation = mutations[m++]);) {
for (var m = 0, ml = mutations.length; m < ml; m++) { var added = mutation.addedNodes;
const mutation = mutations[m]; for (var n = 0, node; (node = added[n++]);) {
if (mutation.type === 'childList') { // process only ELEMENT_NODE
// eslint-disable-next-line no-var if (node.nodeType == 1) {
for (var n = 0, nodes = mutation.addedNodes, nl = nodes.length; n < nl; n++) { var iframes = node.localName === 'iframe' ? [node] :
const node = nodes[n]; node.children.length && node.getElementsByTagName('iframe');
if (node.localName === 'iframe' && iframeIsDynamic(node)) { for (var i = 0, iframe; (iframe = iframes[i++]);) {
addDocumentStylesToIFrame(node); if (iframeIsDynamic(iframe)) {
addDocumentStylesToIFrame(iframe);
}
} }
} }
} }
} }
/* eslint-enable no-var */
} }
iframeObserver.start = () => { iframeObserver.start = () => {