aboutsummaryrefslogtreecommitdiff
path: root/ext/js/yomitan.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/yomitan.js')
-rw-r--r--ext/js/yomitan.js59
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', {});