aboutsummaryrefslogtreecommitdiff
path: root/ext/js/background
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2024-01-20 23:13:17 -0500
committerGitHub <noreply@github.com>2024-01-21 04:13:17 +0000
commit6ba1ffe74558dd174e3308d48885fb068fa37d55 (patch)
tree5519bbf972096e0e3370171d2b62a26d6164d671 /ext/js/background
parentebdde1ee612a262256ad0384131e53bc29b1e10f (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/background')
-rw-r--r--ext/js/background/backend.js11
-rw-r--r--ext/js/background/background-main.js2
-rw-r--r--ext/js/background/offscreen-proxy.js22
3 files changed, 17 insertions, 18 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js
index db7a3c0f..b61f27b1 100644
--- a/ext/js/background/backend.js
+++ b/ext/js/background/backend.js
@@ -49,9 +49,11 @@ import {injectStylesheet} from './script-manager.js';
*/
export class Backend {
/**
- * Creates a new instance.
+ * @param {import('../extension/web-extension.js').WebExtension} webExtension
*/
- constructor() {
+ constructor(webExtension) {
+ /** @type {import('../extension/web-extension.js').WebExtension} */
+ this._webExtension = webExtension;
/** @type {JapaneseUtil} */
this._japaneseUtil = new JapaneseUtil(wanakana);
/** @type {Environment} */
@@ -80,7 +82,7 @@ export class Backend {
});
} else {
/** @type {?OffscreenProxy} */
- this._offscreen = new OffscreenProxy();
+ this._offscreen = new OffscreenProxy(webExtension);
/** @type {DictionaryDatabase|DictionaryDatabaseProxy} */
this._dictionaryDatabase = new DictionaryDatabaseProxy(this._offscreen);
/** @type {Translator|TranslatorProxy} */
@@ -1902,8 +1904,7 @@ export class Backend {
* @param {import('application').ApiMessage<TName>} message
*/
_sendMessageIgnoreResponse(message) {
- const callback = () => this._checkLastError(chrome.runtime.lastError);
- chrome.runtime.sendMessage(message, callback);
+ this._webExtension.sendMessageIgnoreResponse(message);
}
/**
diff --git a/ext/js/background/background-main.js b/ext/js/background/background-main.js
index 2c19e871..f5871a14 100644
--- a/ext/js/background/background-main.js
+++ b/ext/js/background/background-main.js
@@ -23,7 +23,7 @@ import {Backend} from './backend.js';
async function main() {
yomitan.prepare(true);
- const backend = new Backend();
+ const backend = new Backend(yomitan.webExtension);
await backend.prepare();
}
diff --git a/ext/js/background/offscreen-proxy.js b/ext/js/background/offscreen-proxy.js
index 77f5448a..555c3abc 100644
--- a/ext/js/background/offscreen-proxy.js
+++ b/ext/js/background/offscreen-proxy.js
@@ -16,12 +16,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-import {isObject} from '../core/utilities.js';
import {ExtensionError} from '../core/extension-error.js';
+import {isObject} from '../core/utilities.js';
import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js';
export class OffscreenProxy {
- constructor() {
+ /**
+ * @param {import('../extension/web-extension.js').WebExtension} webExtension
+ */
+ constructor(webExtension) {
+ /** @type {import('../extension/web-extension.js').WebExtension} */
+ this._webExtension = webExtension;
/** @type {?Promise<void>} */
this._creatingOffscreen = null;
}
@@ -76,16 +81,9 @@ export class OffscreenProxy {
* @param {import('offscreen').ApiMessage<TMessageType>} message
* @returns {Promise<import('offscreen').ApiReturn<TMessageType>>}
*/
- sendMessagePromise(message) {
- return new Promise((resolve, reject) => {
- chrome.runtime.sendMessage(message, (response) => {
- try {
- resolve(this._getMessageResponseResult(response));
- } catch (error) {
- reject(error);
- }
- });
- });
+ async sendMessagePromise(message) {
+ const response = await this._webExtension.sendMessagePromise(message);
+ return this._getMessageResponseResult(/** @type {import('core').Response<import('offscreen').ApiReturn<TMessageType>>} */ (response));
}
/**