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:
parent
4be9dc0413
commit
0c6b74d999
40
apply.js
40
apply.js
|
@ -188,8 +188,12 @@ function applyStyles(styleHash) {
|
||||||
document.head.appendChild(document.getElementById(id));
|
document.head.appendChild(document.getElementById(id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (document.readyState != 'loading') {
|
||||||
|
onDOMContentLoaded();
|
||||||
|
} else {
|
||||||
document.addEventListener('DOMContentLoaded', onDOMContentLoaded);
|
document.addEventListener('DOMContentLoaded', onDOMContentLoaded);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (retiredStyleIds.length) {
|
if (retiredStyleIds.length) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
@ -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 (mutations.length > 1000) {
|
if (!iframesCollection[0]) {
|
||||||
// use a much faster method for very complex pages with 100,000 mutations
|
return;
|
||||||
|
}
|
||||||
|
// use a much faster method for very complex pages with lots of mutations
|
||||||
// (observer usually receives 1k-10k mutations per call)
|
// (observer usually receives 1k-10k mutations per call)
|
||||||
|
if (mutations.length > 1000) {
|
||||||
addDocumentStylesToAllIFrames();
|
addDocumentStylesToAllIFrames();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -349,21 +358,24 @@ 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 = () => {
|
||||||
// subsequent calls are ignored if already started observing
|
// subsequent calls are ignored if already started observing
|
||||||
|
|
Loading…
Reference in New Issue
Block a user