lazy load element sources

This commit is contained in:
siikamiika 2020-04-18 17:18:33 +03:00
parent bb3ad78e37
commit 66354f1f9e

View File

@ -89,14 +89,27 @@ class FrameOffsetForwarder {
} }
_findFrameWithContentWindow(contentWindow) { _findFrameWithContentWindow(contentWindow) {
const elements = [ const elementSources = [
...this._frameCache, () => [...this._frameCache],
// will contain duplicates, but frame elements are cheap to handle // will contain duplicates, but frame elements are cheap to handle
...document.querySelectorAll('frame, iframe:not(.yomichan-float)'), () => [...document.querySelectorAll('frame, iframe:not(.yomichan-float)')],
document.documentElement () => [document.documentElement]
]; ];
const getMoreElements = () => {
while (true) {
const source = elementSources.shift();
if (source) {
const elements = source();
if (elements.length === 0) { continue; }
return elements;
}
return [];
}
};
const elements = [];
const ELEMENT_NODE = Node.ELEMENT_NODE; const ELEMENT_NODE = Node.ELEMENT_NODE;
while (elements.length > 0) { while (elements.length > 0 || elements.push(...getMoreElements())) {
const element = elements.shift(); const element = elements.shift();
if (element.contentWindow === contentWindow) { if (element.contentWindow === contentWindow) {
this._frameCache.add(element); this._frameCache.add(element);