2021-02-06 19:32:21 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2021-2022 Yomichan Authors
|
2021-02-06 19:32:21 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is used to return the ancestor frame IDs for the current frame.
|
|
|
|
* This is a workaround to using the `webNavigation.getAllFrames` API, which
|
|
|
|
* would require an additional permission that is otherwise unnecessary.
|
2021-02-08 22:52:56 +00:00
|
|
|
* It is also used to track the correlation between child frame elements and their IDs.
|
2021-02-06 19:32:21 +00:00
|
|
|
*/
|
|
|
|
class FrameAncestryHandler {
|
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
* @param frameId The frame ID of the current frame the instance is instantiated in.
|
|
|
|
*/
|
|
|
|
constructor(frameId) {
|
|
|
|
this._frameId = frameId;
|
|
|
|
this._isPrepared = false;
|
|
|
|
this._requestMessageId = 'FrameAncestryHandler.requestFrameInfo';
|
2021-02-06 21:19:55 +00:00
|
|
|
this._responseMessageIdBase = `${this._requestMessageId}.response.`;
|
|
|
|
this._getFrameAncestryInfoPromise = null;
|
|
|
|
this._childFrameMap = new Map();
|
2021-02-06 19:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the frame ID that the instance is instantiated in.
|
|
|
|
*/
|
|
|
|
get frameId() {
|
|
|
|
return this._frameId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes event event listening.
|
|
|
|
*/
|
|
|
|
prepare() {
|
|
|
|
if (this._isPrepared) { return; }
|
|
|
|
window.addEventListener('message', this._onWindowMessage.bind(this), false);
|
|
|
|
this._isPrepared = true;
|
|
|
|
}
|
|
|
|
|
2021-02-08 22:52:56 +00:00
|
|
|
/**
|
|
|
|
* Returns whether or not this frame is the root frame in the tab.
|
|
|
|
* @returns `true` if it is the root, otherwise `false`.
|
|
|
|
*/
|
|
|
|
isRootFrame() {
|
|
|
|
return (window === window.parent);
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:32:21 +00:00
|
|
|
/**
|
|
|
|
* Gets the frame ancestry information for the current frame. If the frame is the
|
|
|
|
* root frame, an empty array is returned. Otherwise, an array of frame IDs is returned,
|
|
|
|
* starting from the nearest ancestor.
|
|
|
|
* @param timeout The maximum time to wait to receive a response to frame information requests.
|
|
|
|
* @returns An array of frame IDs corresponding to the ancestors of the current frame.
|
|
|
|
*/
|
2021-02-06 21:19:55 +00:00
|
|
|
async getFrameAncestryInfo() {
|
|
|
|
if (this._getFrameAncestryInfoPromise === null) {
|
|
|
|
this._getFrameAncestryInfoPromise = this._getFrameAncestryInfo(5000);
|
|
|
|
}
|
|
|
|
return await this._getFrameAncestryInfoPromise;
|
|
|
|
}
|
|
|
|
|
2021-02-08 22:52:56 +00:00
|
|
|
/**
|
|
|
|
* Gets the frame element of a child frame given a frame ID.
|
|
|
|
* For this function to work, the `getFrameAncestryInfo` function needs to have
|
|
|
|
* been invoked previously.
|
|
|
|
* @param frameId The frame ID of the child frame to get.
|
|
|
|
* @returns The element corresponding to the frame with ID `frameId`, otherwise `null`.
|
|
|
|
*/
|
|
|
|
getChildFrameElement(frameId) {
|
|
|
|
const frameInfo = this._childFrameMap.get(frameId);
|
|
|
|
if (typeof frameInfo === 'undefined') { return null; }
|
|
|
|
|
|
|
|
let {frameElement} = frameInfo;
|
|
|
|
if (typeof frameElement === 'undefined') {
|
|
|
|
frameElement = this._findFrameElementWithContentWindow(frameInfo.window);
|
|
|
|
frameInfo.frameElement = frameElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frameElement;
|
|
|
|
}
|
|
|
|
|
2021-02-06 21:19:55 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_getFrameAncestryInfo(timeout=5000) {
|
2021-02-06 19:32:21 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const targetWindow = window.parent;
|
|
|
|
if (window === targetWindow) {
|
|
|
|
resolve([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uniqueId = generateId(16);
|
2021-02-06 21:19:55 +00:00
|
|
|
let nonce = generateId(16);
|
|
|
|
const responseMessageId = `${this._responseMessageIdBase}${uniqueId}`;
|
2021-02-06 19:32:21 +00:00
|
|
|
const results = [];
|
|
|
|
let timer = null;
|
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
if (timer !== null) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
yomichan.crossFrame.unregisterHandler(responseMessageId);
|
2021-02-06 19:32:21 +00:00
|
|
|
};
|
2021-02-06 21:19:55 +00:00
|
|
|
const onMessage = (params) => {
|
|
|
|
if (params.nonce !== nonce) { return null; }
|
2021-02-06 19:32:21 +00:00
|
|
|
|
|
|
|
// Add result
|
2021-02-06 21:19:55 +00:00
|
|
|
const {frameId, more} = params;
|
|
|
|
results.push(frameId);
|
|
|
|
nonce = generateId(16);
|
2021-02-06 19:32:21 +00:00
|
|
|
|
2021-02-06 21:19:55 +00:00
|
|
|
if (!more) {
|
2021-02-06 19:32:21 +00:00
|
|
|
// Cleanup
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
// Finish
|
|
|
|
resolve(results);
|
|
|
|
}
|
2021-02-06 21:19:55 +00:00
|
|
|
return {nonce};
|
2021-02-06 19:32:21 +00:00
|
|
|
};
|
|
|
|
const onTimeout = () => {
|
|
|
|
timer = null;
|
|
|
|
cleanup();
|
|
|
|
reject(new Error(`Request for parent frame ID timed out after ${timeout}ms`));
|
|
|
|
};
|
|
|
|
const resetTimeout = () => {
|
|
|
|
if (timer !== null) { clearTimeout(timer); }
|
|
|
|
timer = setTimeout(onTimeout, timeout);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Start
|
2021-02-14 20:53:35 +00:00
|
|
|
yomichan.crossFrame.registerHandlers([[responseMessageId, {async: false, handler: onMessage}]]);
|
2021-02-06 19:32:21 +00:00
|
|
|
resetTimeout();
|
2021-02-06 21:19:55 +00:00
|
|
|
const frameId = this._frameId;
|
|
|
|
this._requestFrameInfo(targetWindow, frameId, frameId, uniqueId, nonce);
|
2021-02-06 19:32:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onWindowMessage(event) {
|
2021-02-06 21:19:55 +00:00
|
|
|
const {source} = event;
|
|
|
|
if (source === window || source.parent !== window) { return; }
|
|
|
|
|
2021-02-06 19:32:21 +00:00
|
|
|
const {data} = event;
|
|
|
|
if (
|
|
|
|
typeof data === 'object' &&
|
|
|
|
data !== null &&
|
|
|
|
data.action === this._requestMessageId
|
|
|
|
) {
|
2021-02-06 21:19:55 +00:00
|
|
|
this._onRequestFrameInfo(data.params, source);
|
2021-02-06 19:32:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 21:19:55 +00:00
|
|
|
async _onRequestFrameInfo(params, source) {
|
|
|
|
try {
|
|
|
|
let {originFrameId, childFrameId, uniqueId, nonce} = params;
|
|
|
|
if (
|
|
|
|
!this._isNonNegativeInteger(originFrameId) ||
|
|
|
|
typeof uniqueId !== 'string' ||
|
|
|
|
typeof nonce !== 'string'
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-06 19:32:21 +00:00
|
|
|
|
2021-02-06 21:19:55 +00:00
|
|
|
const frameId = this._frameId;
|
|
|
|
const {parent} = window;
|
|
|
|
const more = (window !== parent);
|
|
|
|
const responseParams = {frameId, nonce, more};
|
|
|
|
const responseMessageId = `${this._responseMessageIdBase}${uniqueId}`;
|
2021-02-06 19:32:21 +00:00
|
|
|
|
2021-02-06 21:19:55 +00:00
|
|
|
try {
|
2021-02-14 20:53:35 +00:00
|
|
|
const response = await yomichan.crossFrame.invoke(originFrameId, responseMessageId, responseParams);
|
2021-02-06 21:19:55 +00:00
|
|
|
if (response === null) { return; }
|
|
|
|
nonce = response.nonce;
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-06 19:32:21 +00:00
|
|
|
|
2021-02-06 21:19:55 +00:00
|
|
|
if (!this._childFrameMap.has(childFrameId)) {
|
2021-02-08 22:52:56 +00:00
|
|
|
this._childFrameMap.set(childFrameId, {window: source, frameElement: void 0});
|
2021-02-06 21:19:55 +00:00
|
|
|
}
|
2021-02-06 19:32:21 +00:00
|
|
|
|
2021-02-06 21:19:55 +00:00
|
|
|
if (more) {
|
|
|
|
this._requestFrameInfo(parent, originFrameId, frameId, uniqueId, nonce);
|
|
|
|
}
|
2021-02-06 19:32:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 21:19:55 +00:00
|
|
|
_requestFrameInfo(targetWindow, originFrameId, childFrameId, uniqueId, nonce) {
|
2021-02-06 19:32:21 +00:00
|
|
|
targetWindow.postMessage({
|
|
|
|
action: this._requestMessageId,
|
2021-02-06 21:19:55 +00:00
|
|
|
params: {originFrameId, childFrameId, uniqueId, nonce}
|
2021-02-06 19:32:21 +00:00
|
|
|
}, '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
_isNonNegativeInteger(value) {
|
|
|
|
return (
|
|
|
|
typeof value === 'number' &&
|
|
|
|
Number.isFinite(value) &&
|
|
|
|
value >= 0 &&
|
|
|
|
Math.floor(value) === value
|
|
|
|
);
|
|
|
|
}
|
2021-02-08 22:52:56 +00:00
|
|
|
|
|
|
|
_findFrameElementWithContentWindow(contentWindow) {
|
|
|
|
// Check frameElement, for non-null same-origin frames
|
|
|
|
try {
|
|
|
|
const {frameElement} = contentWindow;
|
|
|
|
if (frameElement !== null) { return frameElement; }
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check frames
|
|
|
|
const frameTypes = ['iframe', 'frame', 'embed'];
|
|
|
|
for (const frameType of frameTypes) {
|
|
|
|
for (const frame of document.getElementsByTagName(frameType)) {
|
|
|
|
if (frame.contentWindow === contentWindow) {
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for shadow roots
|
|
|
|
const rootElements = [document.documentElement];
|
|
|
|
while (rootElements.length > 0) {
|
|
|
|
const rootElement = rootElements.shift();
|
|
|
|
const walker = document.createTreeWalker(rootElement, NodeFilter.SHOW_ELEMENT);
|
|
|
|
while (walker.nextNode()) {
|
|
|
|
const element = walker.currentNode;
|
|
|
|
|
|
|
|
if (element.contentWindow === contentWindow) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
const shadowRoot = (
|
|
|
|
element.shadowRoot ||
|
|
|
|
element.openOrClosedShadowRoot // Available to Firefox 63+ for WebExtensions
|
|
|
|
);
|
|
|
|
if (shadowRoot) {
|
|
|
|
rootElements.push(shadowRoot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found
|
|
|
|
return null;
|
|
|
|
}
|
2021-02-06 19:32:21 +00:00
|
|
|
}
|