From 7e9f7e2616973418cc50f7706bd8f644cb9d5559 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 18 Feb 2024 07:58:59 -0500 Subject: Application data refactor (#699) * Pass tabId and frameId to Application * Remove casts * Remove redundant frameInformationGet calls * Expose tabId and frameId * Remove unsed * Simplify * Update FrameAncestryHandler to not need a direct frameId * Remove frameId from FrameOffsetForwarder * Remove frameId from PopupFactory * Remove frameId from Frontend * Remove frameId from PopupPreviewFrame * Fix PopupFactory and Frontend constructor * Remove frameId from Display * Remove frameId from SearchDisplayController * Restore if check --- ext/js/app/content-script-main.js | 9 +-------- ext/js/app/frontend.js | 23 ++++++++++------------- ext/js/app/popup-factory.js | 21 ++++++++++----------- 3 files changed, 21 insertions(+), 32 deletions(-) (limited to 'ext/js/app') diff --git a/ext/js/app/content-script-main.js b/ext/js/app/content-script-main.js index 23d36b48..34160fd1 100644 --- a/ext/js/app/content-script-main.js +++ b/ext/js/app/content-script-main.js @@ -22,21 +22,14 @@ import {Frontend} from './frontend.js'; import {PopupFactory} from './popup-factory.js'; await Application.main(async (application) => { - const {tabId, frameId} = await application.api.frameInformationGet(); - if (typeof frameId !== 'number') { - throw new Error('Failed to get frameId'); - } - const hotkeyHandler = new HotkeyHandler(); hotkeyHandler.prepare(application.crossFrame); - const popupFactory = new PopupFactory(application, frameId); + const popupFactory = new PopupFactory(application); popupFactory.prepare(); const frontend = new Frontend({ application, - tabId, - frameId, popupFactory, depth: 0, parentPopupId: null, diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js index 84a8f1e6..1a3fbbe1 100644 --- a/ext/js/app/frontend.js +++ b/ext/js/app/frontend.js @@ -39,8 +39,6 @@ export class Frontend { pageType, popupFactory, depth, - tabId, - frameId, parentPopupId, parentFrameId, useProxyPopup, @@ -57,10 +55,6 @@ export class Frontend { this._popupFactory = popupFactory; /** @type {number} */ this._depth = depth; - /** @type {number|undefined} */ - this._tabId = tabId; - /** @type {number} */ - this._frameId = frameId; /** @type {?string} */ this._parentPopupId = parentPopupId; /** @type {?number} */ @@ -588,8 +582,13 @@ export class Frontend { return null; } + const {frameId} = this._application; + if (frameId === null) { + return null; + } + return await this._popupFactory.getOrCreatePopup({ - frameId: this._frameId, + frameId, depth: this._depth, childrenSupported: this._childrenSupported }); @@ -703,12 +702,10 @@ export class Frontend { }; if (sentence !== null) { detailsState.sentence = sentence; } if (documentTitle !== null) { detailsState.documentTitle = documentTitle; } + const {tabId, frameId} = this._application; /** @type {import('display').HistoryContent} */ const detailsContent = { - contentOrigin: { - tabId: this._tabId, - frameId: this._frameId - } + contentOrigin: {tabId, frameId} }; if (dictionaryEntries !== null) { detailsContent.dictionaryEntries = dictionaryEntries; @@ -819,7 +816,7 @@ export class Frontend { */ _signalFrontendReady(targetFrameId) { /** @type {import('application').ApiMessageNoFrameId<'frontendReady'>} */ - const message = {action: 'frontendReady', params: {frameId: this._frameId}}; + const message = {action: 'frontendReady', params: {frameId: this._application.frameId}}; if (targetFrameId === null) { this._application.api.broadcastTab(message); } else { @@ -867,7 +864,7 @@ export class Frontend { } chrome.runtime.onMessage.addListener(onMessage); - this._application.api.broadcastTab({action: 'frontendRequestReadyBroadcast', params: {frameId: this._frameId}}); + this._application.api.broadcastTab({action: 'frontendRequestReadyBroadcast', params: {frameId: this._application.frameId}}); }); } diff --git a/ext/js/app/popup-factory.js b/ext/js/app/popup-factory.js index c5187291..4338bb3a 100644 --- a/ext/js/app/popup-factory.js +++ b/ext/js/app/popup-factory.js @@ -29,15 +29,12 @@ export class PopupFactory { /** * Creates a new instance. * @param {import('../application.js').Application} application - * @param {number} frameId The frame ID of the host frame. */ - constructor(application, frameId) { + constructor(application) { /** @type {import('../application.js').Application} */ this._application = application; - /** @type {number} */ - this._frameId = frameId; /** @type {FrameOffsetForwarder} */ - this._frameOffsetForwarder = new FrameOffsetForwarder(application.crossFrame, frameId); + this._frameOffsetForwarder = new FrameOffsetForwarder(application.crossFrame); /** @type {Map} */ this._popups = new Map(); /** @type {Map} */ @@ -115,6 +112,9 @@ export class PopupFactory { depth = 0; } + const currentFrameId = this._application.frameId; + if (currentFrameId === null) { throw new Error('Cannot create popup: no frameId'); } + if (popupWindow) { // New unique id if (id === null) { @@ -124,11 +124,11 @@ export class PopupFactory { application: this._application, id, depth, - frameId: this._frameId + frameId: currentFrameId }); this._popups.set(id, popup); return popup; - } else if (frameId === this._frameId) { + } else if (frameId === currentFrameId) { // New unique id if (id === null) { id = generateId(16); @@ -137,7 +137,7 @@ export class PopupFactory { application: this._application, id, depth, - frameId: this._frameId, + frameId: currentFrameId, childrenSupported }); if (parent !== null) { @@ -155,13 +155,12 @@ export class PopupFactory { throw new Error('Invalid frameId'); } const useFrameOffsetForwarder = (parentPopupId === null); - /** @type {{id: string, depth: number, frameId: number}} */ - const info = await this._application.crossFrame.invoke(frameId, 'popupFactoryGetOrCreatePopup', /** @type {import('popup-factory').GetOrCreatePopupDetails} */ ({ + const info = await this._application.crossFrame.invoke(frameId, 'popupFactoryGetOrCreatePopup', { id, parentPopupId, frameId, childrenSupported - })); + }); id = info.id; const popup = new PopupProxy({ application: this._application, -- cgit v1.2.3