move open shadow root iframe finder to a function
This commit is contained in:
parent
85706c421b
commit
b786e2da19
@ -81,23 +81,9 @@ class FrameOffsetForwarder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (sourceFrame === null) {
|
if (sourceFrame === null) {
|
||||||
const getShadowRootElements = (documentOrElement) => {
|
sourceFrame = this._getOpenShadowRootSourceFrame(e.source);
|
||||||
const elements = Array.from(documentOrElement.querySelectorAll('*'))
|
|
||||||
.filter((el) => !!el.shadowRoot);
|
|
||||||
const childElements = elements
|
|
||||||
.map((el) => el.shadowRoot)
|
|
||||||
.map(getShadowRootElements);
|
|
||||||
elements.push(childElements.flat());
|
|
||||||
|
|
||||||
return elements.flat();
|
|
||||||
};
|
|
||||||
|
|
||||||
sourceFrame = getShadowRootElements(document)
|
|
||||||
.map((el) => Array.from(el.shadowRoot.querySelectorAll('frame, iframe:not(.yomichan-float)')))
|
|
||||||
.flat()
|
|
||||||
.find((el) => el.contentWindow === e.source);
|
|
||||||
|
|
||||||
if (!sourceFrame) {
|
if (!sourceFrame) {
|
||||||
|
// closed shadow root etc.
|
||||||
this._forwardFrameOffsetOrigin(null, uniqueId);
|
this._forwardFrameOffsetOrigin(null, uniqueId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -110,6 +96,24 @@ class FrameOffsetForwarder {
|
|||||||
this._forwardFrameOffset(offset, uniqueId);
|
this._forwardFrameOffset(offset, uniqueId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_getOpenShadowRootSourceFrame(sourceWindow) {
|
||||||
|
const getShadowRootElements = (documentOrElement) => {
|
||||||
|
const elements = Array.from(documentOrElement.querySelectorAll('*'))
|
||||||
|
.filter((el) => !!el.shadowRoot);
|
||||||
|
const childElements = elements
|
||||||
|
.map((el) => el.shadowRoot)
|
||||||
|
.map(getShadowRootElements);
|
||||||
|
elements.push(childElements.flat());
|
||||||
|
|
||||||
|
return elements.flat();
|
||||||
|
};
|
||||||
|
|
||||||
|
return getShadowRootElements(document)
|
||||||
|
.map((el) => Array.from(el.shadowRoot.querySelectorAll('frame, iframe:not(.yomichan-float)')))
|
||||||
|
.flat()
|
||||||
|
.find((el) => el.contentWindow === sourceWindow);
|
||||||
|
}
|
||||||
|
|
||||||
_forwardFrameOffsetParent(offset, uniqueId) {
|
_forwardFrameOffsetParent(offset, uniqueId) {
|
||||||
window.parent.postMessage({action: 'getFrameOffset', params: {offset, uniqueId}}, '*');
|
window.parent.postMessage({action: 'getFrameOffset', params: {offset, uniqueId}}, '*');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user