From 6ba1ffe74558dd174e3308d48885fb068fa37d55 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 20 Jan 2024 23:13:17 -0500 Subject: WebExtension class (#551) * Add WebExtension class * Use WebExtension class * Use WebExtension instance for all runtime message sending * Use getUrl * Add a sendMessage variant which ignores the response and error --- ext/js/yomitan.js | 59 ++++++++++++------------------------------------------- 1 file changed, 13 insertions(+), 46 deletions(-) (limited to 'ext/js/yomitan.js') diff --git a/ext/js/yomitan.js b/ext/js/yomitan.js index cd3f65fd..33afac27 100644 --- a/ext/js/yomitan.js +++ b/ext/js/yomitan.js @@ -23,6 +23,7 @@ import {EventDispatcher} from './core/event-dispatcher.js'; import {ExtensionError} from './core/extension-error.js'; import {log} from './core/logger.js'; import {deferPromise} from './core/utilities.js'; +import {WebExtension} from './extension/web-extension.js'; /** * @returns {boolean} @@ -61,6 +62,9 @@ export class Yomitan extends EventDispatcher { constructor() { super(); + /** @type {WebExtension} */ + this._webExtension = new WebExtension(); + /** @type {string} */ this._extensionName = 'Yomitan'; try { @@ -73,7 +77,7 @@ export class Yomitan extends EventDispatcher { /** @type {?string} */ this._extensionUrlBase = null; try { - this._extensionUrlBase = chrome.runtime.getURL('/'); + this._extensionUrlBase = this._webExtension.getUrl('/'); } catch (e) { // NOP } @@ -85,10 +89,6 @@ export class Yomitan extends EventDispatcher { /** @type {?CrossFrameAPI} */ this._crossFrame = null; /** @type {boolean} */ - this._isExtensionUnloaded = false; - /** @type {boolean} */ - this._isTriggeringExtensionUnloaded = false; - /** @type {boolean} */ this._isReady = false; const {promise, resolve} = /** @type {import('core').DeferredPromiseDetails} */ (deferPromise()); @@ -110,6 +110,11 @@ export class Yomitan extends EventDispatcher { /* eslint-enable no-multi-spaces */ } + /** @type {WebExtension} */ + get webExtension() { + return this._webExtension; + } + /** * Whether the current frame is the background page/service worker or not. * @type {boolean} @@ -119,14 +124,6 @@ export class Yomitan extends EventDispatcher { return /** @type {boolean} */ (this._isBackground); } - /** - * Whether or not the extension is unloaded. - * @type {boolean} - */ - get isExtensionUnloaded() { - return this._isExtensionUnloaded; - } - /** * Gets the API instance for communicating with the backend. * This value will be null on the background page/service worker. @@ -156,9 +153,9 @@ export class Yomitan extends EventDispatcher { chrome.runtime.onMessage.addListener(this._onMessage.bind(this)); if (!isBackground) { - this._api = new API(this); + this._api = new API(this._webExtension); - this.sendMessage({action: 'requestBackendReadySignal'}); + await this._webExtension.sendMessagePromise({action: 'requestBackendReadySignal'}); await this._isBackendReadyPromise; this._crossFrame = new CrossFrameAPI(); @@ -174,7 +171,7 @@ export class Yomitan extends EventDispatcher { */ ready() { this._isReady = true; - this.sendMessage({action: 'applicationReady'}); + this._webExtension.sendMessagePromise({action: 'applicationReady'}); } /** @@ -186,36 +183,6 @@ export class Yomitan extends EventDispatcher { return this._extensionUrlBase !== null && url.startsWith(this._extensionUrlBase); } - // TODO : this function needs type safety - /** - * Runs `chrome.runtime.sendMessage()` with additional exception handling events. - * @param {import('extension').ChromeRuntimeSendMessageArgs} args The arguments to be passed to `chrome.runtime.sendMessage()`. - * @throws {Error} Errors thrown by `chrome.runtime.sendMessage()` are re-thrown. - */ - sendMessage(...args) { - try { - // @ts-expect-error - issue with type conversion, somewhat difficult to resolve in pure JS - chrome.runtime.sendMessage(...args); - } catch (e) { - this.triggerExtensionUnloaded(); - throw e; - } - } - - /** - * Triggers the extensionUnloaded event. - */ - triggerExtensionUnloaded() { - this._isExtensionUnloaded = true; - if (this._isTriggeringExtensionUnloaded) { return; } - try { - this._isTriggeringExtensionUnloaded = true; - this.trigger('extensionUnloaded', {}); - } finally { - this._isTriggeringExtensionUnloaded = false; - } - } - /** */ triggerStorageChanged() { this.trigger('storageChanged', {}); -- cgit v1.2.3