diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2024-01-20 23:13:17 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-21 04:13:17 +0000 |
commit | 6ba1ffe74558dd174e3308d48885fb068fa37d55 (patch) | |
tree | 5519bbf972096e0e3370171d2b62a26d6164d671 /ext/js/yomitan.js | |
parent | ebdde1ee612a262256ad0384131e53bc29b1e10f (diff) |
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
Diffstat (limited to 'ext/js/yomitan.js')
-rw-r--r-- | ext/js/yomitan.js | 59 |
1 files changed, 13 insertions, 46 deletions
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<void>} */ (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} @@ -120,14 +125,6 @@ export class Yomitan extends EventDispatcher { } /** - * 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. * @type {API} @@ -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', {}); |