aboutsummaryrefslogtreecommitdiff
path: root/ext/js/background
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/background')
-rw-r--r--ext/js/background/offscreen-proxy.js2
-rw-r--r--ext/js/background/offscreen.js38
2 files changed, 22 insertions, 18 deletions
diff --git a/ext/js/background/offscreen-proxy.js b/ext/js/background/offscreen-proxy.js
index dfd342b4..99dc0741 100644
--- a/ext/js/background/offscreen-proxy.js
+++ b/ext/js/background/offscreen-proxy.js
@@ -74,7 +74,7 @@ export class OffscreenProxy {
/**
* @template {import('offscreen').MessageType} TMessageType
* @param {import('offscreen').Message<TMessageType>} message
- * @returns {Promise<import('offscreen').MessageReturn<TMessageType>>}
+ * @returns {Promise<import('offscreen').OffscreenApiReturn<TMessageType>>}
*/
sendMessagePromise(message) {
return new Promise((resolve, reject) => {
diff --git a/ext/js/background/offscreen.js b/ext/js/background/offscreen.js
index d1cf3384..44b0af77 100644
--- a/ext/js/background/offscreen.js
+++ b/ext/js/background/offscreen.js
@@ -19,6 +19,7 @@
import * as wanakana from '../../lib/wanakana.js';
import {ClipboardReader} from '../comm/clipboard-reader.js';
import {invokeMessageHandler} from '../core.js';
+import {createApiMap, getApiMapHandler} from '../core/api-map.js';
import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js';
import {DictionaryDatabase} from '../language/dictionary-database.js';
import {JapaneseUtil} from '../language/sandbox/japanese-util.js';
@@ -50,9 +51,10 @@ export class Offscreen {
richContentPasteTargetSelector: '#clipboard-rich-content-paste-target'
});
+
/* eslint-disable no-multi-spaces */
- /** @type {import('offscreen').MessageHandlerMap} */
- this._messageHandlers = new Map(/** @type {import('offscreen').MessageHandlerMapInit} */ ([
+ /** @type {import('offscreen').OffscreenApiMapInit} */
+ const messageHandlersInit = [
['clipboardGetTextOffscreen', this._getTextHandler.bind(this)],
['clipboardGetImageOffscreen', this._getImageHandler.bind(this)],
['clipboardSetBrowserOffscreen', this._setClipboardBrowser.bind(this)],
@@ -65,8 +67,10 @@ export class Offscreen {
['findTermsOffscreen', this._findTermsHandler.bind(this)],
['getTermFrequenciesOffscreen', this._getTermFrequenciesHandler.bind(this)],
['clearDatabaseCachesOffscreen', this._clearDatabaseCachesHandler.bind(this)]
- ]));
- /* eslint-enable no-multi-spaces */
+ ];
+
+ /** @type {import('offscreen').OffscreenApiMap} */
+ this._messageHandlers = createApiMap(messageHandlersInit);
/** @type {?Promise<void>} */
this._prepareDatabasePromise = null;
@@ -77,22 +81,22 @@ export class Offscreen {
chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
}
- /** @type {import('offscreen').MessageHandler<'clipboardGetTextOffscreen', true>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'clipboardGetTextOffscreen'>} */
async _getTextHandler({useRichText}) {
return await this._clipboardReader.getText(useRichText);
}
- /** @type {import('offscreen').MessageHandler<'clipboardGetImageOffscreen', true>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'clipboardGetImageOffscreen'>} */
async _getImageHandler() {
return await this._clipboardReader.getImage();
}
- /** @type {import('offscreen').MessageHandler<'clipboardSetBrowserOffscreen', false>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'clipboardSetBrowserOffscreen'>} */
_setClipboardBrowser({value}) {
this._clipboardReader.browser = value;
}
- /** @type {import('offscreen').MessageHandler<'databasePrepareOffscreen', true>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'databasePrepareOffscreen'>} */
_prepareDatabaseHandler() {
if (this._prepareDatabasePromise !== null) {
return this._prepareDatabasePromise;
@@ -101,29 +105,29 @@ export class Offscreen {
return this._prepareDatabasePromise;
}
- /** @type {import('offscreen').MessageHandler<'getDictionaryInfoOffscreen', true>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'getDictionaryInfoOffscreen'>} */
async _getDictionaryInfoHandler() {
return await this._dictionaryDatabase.getDictionaryInfo();
}
- /** @type {import('offscreen').MessageHandler<'databasePurgeOffscreen', true>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'databasePurgeOffscreen'>} */
async _purgeDatabaseHandler() {
return await this._dictionaryDatabase.purge();
}
- /** @type {import('offscreen').MessageHandler<'databaseGetMediaOffscreen', true>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'databaseGetMediaOffscreen'>} */
async _getMediaHandler({targets}) {
const media = await this._dictionaryDatabase.getMedia(targets);
const serializedMedia = media.map((m) => ({...m, content: ArrayBufferUtil.arrayBufferToBase64(m.content)}));
return serializedMedia;
}
- /** @type {import('offscreen').MessageHandler<'translatorPrepareOffscreen', false>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'translatorPrepareOffscreen'>} */
_prepareTranslatorHandler({deinflectionReasons}) {
this._translator.prepare(deinflectionReasons);
}
- /** @type {import('offscreen').MessageHandler<'findKanjiOffscreen', true>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'findKanjiOffscreen'>} */
async _findKanjiHandler({text, options}) {
/** @type {import('translation').FindKanjiOptions} */
const modifiedOptions = {
@@ -133,7 +137,7 @@ export class Offscreen {
return await this._translator.findKanji(text, modifiedOptions);
}
- /** @type {import('offscreen').MessageHandler<'findTermsOffscreen', true>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'findTermsOffscreen'>} */
_findTermsHandler({mode, text, options}) {
const enabledDictionaryMap = new Map(options.enabledDictionaryMap);
const excludeDictionaryDefinitions = (
@@ -160,19 +164,19 @@ export class Offscreen {
return this._translator.findTerms(mode, text, modifiedOptions);
}
- /** @type {import('offscreen').MessageHandler<'getTermFrequenciesOffscreen', true>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'getTermFrequenciesOffscreen'>} */
_getTermFrequenciesHandler({termReadingList, dictionaries}) {
return this._translator.getTermFrequencies(termReadingList, dictionaries);
}
- /** @type {import('offscreen').MessageHandler<'clearDatabaseCachesOffscreen', false>} */
+ /** @type {import('offscreen').OffscreenApiHandler<'clearDatabaseCachesOffscreen'>} */
_clearDatabaseCachesHandler() {
this._translator.clearDatabaseCaches();
}
/** @type {import('extension').ChromeRuntimeOnMessageCallback} */
_onMessage({action, params}, sender, callback) {
- const messageHandler = this._messageHandlers.get(/** @type {import('offscreen').MessageType} */ (action));
+ const messageHandler = getApiMapHandler(this._messageHandlers, action);
if (typeof messageHandler === 'undefined') { return false; }
return invokeMessageHandler(messageHandler, params, callback, sender);
}