simplify element source lazy load

This commit is contained in:
siikamiika 2020-04-18 22:08:38 +03:00
parent 691b739849
commit a81c33b60a

View File

@ -94,52 +94,42 @@ class FrameOffsetForwarder {
} }
_findFrameWithContentWindow(contentWindow) { _findFrameWithContentWindow(contentWindow) {
const elementSources = [
() => [...this._frameCache],
// will contain duplicates, but frame elements are cheap to handle
() => [...document.querySelectorAll('frame, iframe:not(.yomichan-float)')],
() => [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 || elements.push(...getMoreElements())) { for (const elements of this._getFrameElementSources()) {
const element = elements.shift(); while (elements.length > 0) {
if (element.contentWindow === contentWindow) { const element = elements.shift();
this._frameCache.add(element); if (element.contentWindow === contentWindow) {
return element; this._frameCache.add(element);
} return element;
}
const shadowRoot = element.shadowRoot; const shadowRoot = element.shadowRoot;
if (shadowRoot) { if (shadowRoot) {
for (const child of shadowRoot.children) { for (const child of shadowRoot.children) {
if (child.nodeType === ELEMENT_NODE) {
elements.push(child);
}
}
}
for (const child of element.children) {
if (child.nodeType === ELEMENT_NODE) { if (child.nodeType === ELEMENT_NODE) {
elements.push(child); elements.push(child);
} }
} }
} }
for (const child of element.children) {
if (child.nodeType === ELEMENT_NODE) {
elements.push(child);
}
}
} }
return null; return null;
} }
*_getFrameElementSources() {
yield [...this._frameCache];
// will contain duplicates, but frame elements are cheap to handle
yield [...document.querySelectorAll('frame, iframe:not(.yomichan-float)')];
yield [document.documentElement];
}
_forwardFrameOffsetParent(offset, uniqueId) { _forwardFrameOffsetParent(offset, uniqueId) {
window.parent.postMessage({action: 'getFrameOffset', params: {offset, uniqueId}}, '*'); window.parent.postMessage({action: 'getFrameOffset', params: {offset, uniqueId}}, '*');
} }