diff options
Diffstat (limited to 'ext/js/comm')
-rw-r--r-- | ext/js/comm/cross-frame-api.js | 14 | ||||
-rw-r--r-- | ext/js/comm/frame-ancestry-handler.js | 26 | ||||
-rw-r--r-- | ext/js/comm/frame-offset-forwarder.js | 18 |
3 files changed, 31 insertions, 27 deletions
diff --git a/ext/js/comm/cross-frame-api.js b/ext/js/comm/cross-frame-api.js index 4b4e9419..33a91c89 100644 --- a/ext/js/comm/cross-frame-api.js +++ b/ext/js/comm/cross-frame-api.js @@ -313,6 +313,20 @@ export class CrossFrameAPI { this._frameId = frameId; } + /** + * @type {?number} + */ + get tabId() { + return this._tabId; + } + + /** + * @type {?number} + */ + get frameId() { + return this._frameId; + } + /** */ prepare() { chrome.runtime.onConnect.addListener(this._onConnect.bind(this)); diff --git a/ext/js/comm/frame-ancestry-handler.js b/ext/js/comm/frame-ancestry-handler.js index 3e58d57b..92ed3b8c 100644 --- a/ext/js/comm/frame-ancestry-handler.js +++ b/ext/js/comm/frame-ancestry-handler.js @@ -28,13 +28,10 @@ export class FrameAncestryHandler { /** * Creates a new instance. * @param {import('../comm/cross-frame-api.js').CrossFrameAPI} crossFrameApi - * @param {number} frameId The frame ID of the current frame the instance is instantiated in. */ - constructor(crossFrameApi, frameId) { + constructor(crossFrameApi) { /** @type {import('../comm/cross-frame-api.js').CrossFrameAPI} */ this._crossFrameApi = crossFrameApi; - /** @type {number} */ - this._frameId = frameId; /** @type {boolean} */ this._isPrepared = false; /** @type {string} */ @@ -48,14 +45,6 @@ export class FrameAncestryHandler { } /** - * Gets the frame ID that the instance is instantiated in. - * @type {number} - */ - get frameId() { - return this._frameId; - } - - /** * Initializes event event listening. */ prepare() { @@ -116,8 +105,9 @@ export class FrameAncestryHandler { */ _getFrameAncestryInfo(timeout = 5000) { return new Promise((resolve, reject) => { + const {frameId} = this._crossFrameApi; const targetWindow = window.parent; - if (window === targetWindow) { + if (frameId === null || window === targetWindow) { resolve([]); return; } @@ -141,11 +131,10 @@ export class FrameAncestryHandler { if (params.nonce !== nonce) { return null; } // Add result - const {frameId, more} = params; - results.push(frameId); + results.push(params.frameId); nonce = generateId(16); - if (!more) { + if (!params.more) { // Cleanup cleanup(); @@ -167,7 +156,6 @@ export class FrameAncestryHandler { // Start this._addResponseHandler(uniqueId, onMessage); resetTimeout(); - const frameId = this._frameId; this._requestFrameInfo(targetWindow, frameId, frameId, uniqueId, nonce); }); } @@ -208,7 +196,9 @@ export class FrameAncestryHandler { return; } - const frameId = this._frameId; + const {frameId} = this._crossFrameApi; + if (frameId === null) { return; } + const {parent} = window; const more = (window !== parent); diff --git a/ext/js/comm/frame-offset-forwarder.js b/ext/js/comm/frame-offset-forwarder.js index fe1ff98a..59659606 100644 --- a/ext/js/comm/frame-offset-forwarder.js +++ b/ext/js/comm/frame-offset-forwarder.js @@ -21,15 +21,12 @@ import {FrameAncestryHandler} from './frame-ancestry-handler.js'; export class FrameOffsetForwarder { /** * @param {import('../comm/cross-frame-api.js').CrossFrameAPI} crossFrameApi - * @param {number} frameId */ - constructor(crossFrameApi, frameId) { + constructor(crossFrameApi) { /** @type {import('../comm/cross-frame-api.js').CrossFrameAPI} */ this._crossFrameApi = crossFrameApi; - /** @type {number} */ - this._frameId = frameId; /** @type {FrameAncestryHandler} */ - this._frameAncestryHandler = new FrameAncestryHandler(crossFrameApi, frameId); + this._frameAncestryHandler = new FrameAncestryHandler(crossFrameApi); } /** @@ -50,15 +47,18 @@ export class FrameOffsetForwarder { return [0, 0]; } + const {frameId} = this._crossFrameApi; + if (frameId === null) { return null; } + try { const ancestorFrameIds = await this._frameAncestryHandler.getFrameAncestryInfo(); - let childFrameId = this._frameId; + let childFrameId = frameId; /** @type {Promise<?import('frame-offset-forwarder').ChildFrameRect>[]} */ const promises = []; - for (const frameId of ancestorFrameIds) { - promises.push(this._crossFrameApi.invoke(frameId, 'frameOffsetForwarderGetChildFrameRect', {frameId: childFrameId})); - childFrameId = frameId; + for (const ancestorFrameId of ancestorFrameIds) { + promises.push(this._crossFrameApi.invoke(ancestorFrameId, 'frameOffsetForwarderGetChildFrameRect', {frameId: childFrameId})); + childFrameId = ancestorFrameId; } const results = await Promise.all(promises); |