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/comm/api.js | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'ext/js/comm') diff --git a/ext/js/comm/api.js b/ext/js/comm/api.js index 50814aa2..2e1e8826 100644 --- a/ext/js/comm/api.js +++ b/ext/js/comm/api.js @@ -20,11 +20,11 @@ import {ExtensionError} from '../core/extension-error.js'; export class API { /** - * @param {import('../yomitan.js').Yomitan} yomitan + * @param {import('../extension/web-extension.js').WebExtension} webExtension */ - constructor(yomitan) { - /** @type {import('../yomitan.js').Yomitan} */ - this._yomitan = yomitan; + constructor(webExtension) { + /** @type {import('../extension/web-extension.js').WebExtension} */ + this._webExtension = webExtension; } /** @@ -375,13 +375,15 @@ export class API { const data = {action, params}; return new Promise((resolve, reject) => { try { - this._yomitan.sendMessage(data, (response) => { - this._checkLastError(chrome.runtime.lastError); + this._webExtension.sendMessage(data, (response) => { + this._webExtension.getLastError(); if (response !== null && typeof response === 'object') { - if (typeof response.error !== 'undefined') { - reject(ExtensionError.deserialize(response.error)); + const {error} = /** @type {import('core').UnknownObject} */ (response); + if (typeof error !== 'undefined') { + reject(ExtensionError.deserialize(/** @type {import('core').SerializedError} */ (error))); } else { - resolve(response.result); + const {result} = /** @type {import('core').UnknownObject} */ (response); + resolve(/** @type {import('api').ApiReturn} */ (result)); } } else { const message = response === null ? 'Unexpected null response' : `Unexpected response of type ${typeof response}`; @@ -393,11 +395,4 @@ export class API { } }); } - - /** - * @param {chrome.runtime.LastError|undefined} _ignore - */ - _checkLastError(_ignore) { - // NOP - } } -- cgit v1.2.3