summaryrefslogtreecommitdiff
path: root/ext/js/background
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-12-20 00:18:55 -0500
committerGitHub <noreply@github.com>2023-12-20 05:18:55 +0000
commit3c226215419ca815712e9568f7d871a96f5ff1cf (patch)
tree8bfe561a0d36589860b64c1758c2e29e92e7e3f4 /ext/js/background
parente0e29dc1aa0965b3e0fb97de64a27c2b695e068b (diff)
Simplify message handlers (#396)
Diffstat (limited to 'ext/js/background')
-rw-r--r--ext/js/background/backend.js100
-rw-r--r--ext/js/background/offscreen.js32
2 files changed, 60 insertions, 72 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js
index 09edbd6e..c3de6fc7 100644
--- a/ext/js/background/backend.js
+++ b/ext/js/background/backend.js
@@ -149,51 +149,51 @@ export class Backend {
this._permissionsUtil = new PermissionsUtil();
/* eslint-disable no-multi-spaces */
- /** @type {import('backend').MessageHandlerMap} */
- this._messageHandlers = new Map(/** @type {import('backend').MessageHandlerMapInit} */ ([
- ['requestBackendReadySignal', {async: false, contentScript: true, handler: this._onApiRequestBackendReadySignal.bind(this)}],
- ['optionsGet', {async: false, contentScript: true, handler: this._onApiOptionsGet.bind(this)}],
- ['optionsGetFull', {async: false, contentScript: true, handler: this._onApiOptionsGetFull.bind(this)}],
- ['kanjiFind', {async: true, contentScript: true, handler: this._onApiKanjiFind.bind(this)}],
- ['termsFind', {async: true, contentScript: true, handler: this._onApiTermsFind.bind(this)}],
- ['parseText', {async: true, contentScript: true, handler: this._onApiParseText.bind(this)}],
- ['getAnkiConnectVersion', {async: true, contentScript: true, handler: this._onApiGetAnkiConnectVersion.bind(this)}],
- ['isAnkiConnected', {async: true, contentScript: true, handler: this._onApiIsAnkiConnected.bind(this)}],
- ['addAnkiNote', {async: true, contentScript: true, handler: this._onApiAddAnkiNote.bind(this)}],
- ['getAnkiNoteInfo', {async: true, contentScript: true, handler: this._onApiGetAnkiNoteInfo.bind(this)}],
- ['injectAnkiNoteMedia', {async: true, contentScript: true, handler: this._onApiInjectAnkiNoteMedia.bind(this)}],
- ['noteView', {async: true, contentScript: true, handler: this._onApiNoteView.bind(this)}],
- ['suspendAnkiCardsForNote', {async: true, contentScript: true, handler: this._onApiSuspendAnkiCardsForNote.bind(this)}],
- ['commandExec', {async: false, contentScript: true, handler: this._onApiCommandExec.bind(this)}],
- ['getTermAudioInfoList', {async: true, contentScript: true, handler: this._onApiGetTermAudioInfoList.bind(this)}],
- ['sendMessageToFrame', {async: false, contentScript: true, handler: this._onApiSendMessageToFrame.bind(this)}],
- ['broadcastTab', {async: false, contentScript: true, handler: this._onApiBroadcastTab.bind(this)}],
- ['frameInformationGet', {async: true, contentScript: true, handler: this._onApiFrameInformationGet.bind(this)}],
- ['injectStylesheet', {async: true, contentScript: true, handler: this._onApiInjectStylesheet.bind(this)}],
- ['getStylesheetContent', {async: true, contentScript: true, handler: this._onApiGetStylesheetContent.bind(this)}],
- ['getEnvironmentInfo', {async: false, contentScript: true, handler: this._onApiGetEnvironmentInfo.bind(this)}],
- ['clipboardGet', {async: true, contentScript: true, handler: this._onApiClipboardGet.bind(this)}],
- ['getDisplayTemplatesHtml', {async: true, contentScript: true, handler: this._onApiGetDisplayTemplatesHtml.bind(this)}],
- ['getZoom', {async: true, contentScript: true, handler: this._onApiGetZoom.bind(this)}],
- ['getDefaultAnkiFieldTemplates', {async: false, contentScript: true, handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this)}],
- ['getDictionaryInfo', {async: true, contentScript: true, handler: this._onApiGetDictionaryInfo.bind(this)}],
- ['purgeDatabase', {async: true, contentScript: false, handler: this._onApiPurgeDatabase.bind(this)}],
- ['getMedia', {async: true, contentScript: true, handler: this._onApiGetMedia.bind(this)}],
- ['log', {async: false, contentScript: true, handler: this._onApiLog.bind(this)}],
- ['logIndicatorClear', {async: false, contentScript: true, handler: this._onApiLogIndicatorClear.bind(this)}],
- ['createActionPort', {async: false, contentScript: true, handler: this._onApiCreateActionPort.bind(this)}],
- ['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}],
- ['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}],
- ['setAllSettings', {async: true, contentScript: false, handler: this._onApiSetAllSettings.bind(this)}],
- ['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}],
- ['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.bind(this)}],
- ['triggerDatabaseUpdated', {async: false, contentScript: true, handler: this._onApiTriggerDatabaseUpdated.bind(this)}],
- ['testMecab', {async: true, contentScript: true, handler: this._onApiTestMecab.bind(this)}],
- ['textHasJapaneseCharacters', {async: false, contentScript: true, handler: this._onApiTextHasJapaneseCharacters.bind(this)}],
- ['getTermFrequencies', {async: true, contentScript: true, handler: this._onApiGetTermFrequencies.bind(this)}],
- ['findAnkiNotes', {async: true, contentScript: true, handler: this._onApiFindAnkiNotes.bind(this)}],
- ['loadExtensionScripts', {async: true, contentScript: true, handler: this._onApiLoadExtensionScripts.bind(this)}],
- ['openCrossFramePort', {async: false, contentScript: true, handler: this._onApiOpenCrossFramePort.bind(this)}]
+ /** @type {import('core').MessageHandlerMap} */
+ this._messageHandlers = new Map(/** @type {import('core').MessageHandlerMapInit} */ ([
+ ['requestBackendReadySignal', this._onApiRequestBackendReadySignal.bind(this)],
+ ['optionsGet', this._onApiOptionsGet.bind(this)],
+ ['optionsGetFull', this._onApiOptionsGetFull.bind(this)],
+ ['kanjiFind', this._onApiKanjiFind.bind(this)],
+ ['termsFind', this._onApiTermsFind.bind(this)],
+ ['parseText', this._onApiParseText.bind(this)],
+ ['getAnkiConnectVersion', this._onApiGetAnkiConnectVersion.bind(this)],
+ ['isAnkiConnected', this._onApiIsAnkiConnected.bind(this)],
+ ['addAnkiNote', this._onApiAddAnkiNote.bind(this)],
+ ['getAnkiNoteInfo', this._onApiGetAnkiNoteInfo.bind(this)],
+ ['injectAnkiNoteMedia', this._onApiInjectAnkiNoteMedia.bind(this)],
+ ['noteView', this._onApiNoteView.bind(this)],
+ ['suspendAnkiCardsForNote', this._onApiSuspendAnkiCardsForNote.bind(this)],
+ ['commandExec', this._onApiCommandExec.bind(this)],
+ ['getTermAudioInfoList', this._onApiGetTermAudioInfoList.bind(this)],
+ ['sendMessageToFrame', this._onApiSendMessageToFrame.bind(this)],
+ ['broadcastTab', this._onApiBroadcastTab.bind(this)],
+ ['frameInformationGet', this._onApiFrameInformationGet.bind(this)],
+ ['injectStylesheet', this._onApiInjectStylesheet.bind(this)],
+ ['getStylesheetContent', this._onApiGetStylesheetContent.bind(this)],
+ ['getEnvironmentInfo', this._onApiGetEnvironmentInfo.bind(this)],
+ ['clipboardGet', this._onApiClipboardGet.bind(this)],
+ ['getDisplayTemplatesHtml', this._onApiGetDisplayTemplatesHtml.bind(this)],
+ ['getZoom', this._onApiGetZoom.bind(this)],
+ ['getDefaultAnkiFieldTemplates', this._onApiGetDefaultAnkiFieldTemplates.bind(this)],
+ ['getDictionaryInfo', this._onApiGetDictionaryInfo.bind(this)],
+ ['purgeDatabase', this._onApiPurgeDatabase.bind(this)],
+ ['getMedia', this._onApiGetMedia.bind(this)],
+ ['log', this._onApiLog.bind(this)],
+ ['logIndicatorClear', this._onApiLogIndicatorClear.bind(this)],
+ ['createActionPort', this._onApiCreateActionPort.bind(this)],
+ ['modifySettings', this._onApiModifySettings.bind(this)],
+ ['getSettings', this._onApiGetSettings.bind(this)],
+ ['setAllSettings', this._onApiSetAllSettings.bind(this)],
+ ['getOrCreateSearchPopup', this._onApiGetOrCreateSearchPopup.bind(this)],
+ ['isTabSearchPopup', this._onApiIsTabSearchPopup.bind(this)],
+ ['triggerDatabaseUpdated', this._onApiTriggerDatabaseUpdated.bind(this)],
+ ['testMecab', this._onApiTestMecab.bind(this)],
+ ['textHasJapaneseCharacters', this._onApiTextHasJapaneseCharacters.bind(this)],
+ ['getTermFrequencies', this._onApiGetTermFrequencies.bind(this)],
+ ['findAnkiNotes', this._onApiFindAnkiNotes.bind(this)],
+ ['loadExtensionScripts', this._onApiLoadExtensionScripts.bind(this)],
+ ['openCrossFramePort', this._onApiOpenCrossFramePort.bind(this)]
]));
/* eslint-enable no-multi-spaces */
/** @type {import('backend').MessageHandlerWithProgressMap} */
@@ -408,16 +408,6 @@ export class Backend {
_onMessage({action, params}, sender, callback) {
const messageHandler = this._messageHandlers.get(action);
if (typeof messageHandler === 'undefined') { return false; }
-
- if (!messageHandler.contentScript) {
- try {
- this._validatePrivilegedMessageSender(sender);
- } catch (error) {
- callback({error: ExtensionError.serialize(error)});
- return false;
- }
- }
-
return invokeMessageHandler(messageHandler, params, callback, sender);
}
diff --git a/ext/js/background/offscreen.js b/ext/js/background/offscreen.js
index 85b6847b..bcbcda6b 100644
--- a/ext/js/background/offscreen.js
+++ b/ext/js/background/offscreen.js
@@ -52,23 +52,21 @@ export class Offscreen {
/* eslint-disable no-multi-spaces */
/** @type {import('offscreen').MessageHandlerMap} */
- const messageHandlers = new Map([
- ['clipboardGetTextOffscreen', {async: true, handler: this._getTextHandler.bind(this)}],
- ['clipboardGetImageOffscreen', {async: true, handler: this._getImageHandler.bind(this)}],
- ['clipboardSetBrowserOffscreen', {async: false, handler: this._setClipboardBrowser.bind(this)}],
- ['databasePrepareOffscreen', {async: true, handler: this._prepareDatabaseHandler.bind(this)}],
- ['getDictionaryInfoOffscreen', {async: true, handler: this._getDictionaryInfoHandler.bind(this)}],
- ['databasePurgeOffscreen', {async: true, handler: this._purgeDatabaseHandler.bind(this)}],
- ['databaseGetMediaOffscreen', {async: true, handler: this._getMediaHandler.bind(this)}],
- ['translatorPrepareOffscreen', {async: false, handler: this._prepareTranslatorHandler.bind(this)}],
- ['findKanjiOffscreen', {async: true, handler: this._findKanjiHandler.bind(this)}],
- ['findTermsOffscreen', {async: true, handler: this._findTermsHandler.bind(this)}],
- ['getTermFrequenciesOffscreen', {async: true, handler: this._getTermFrequenciesHandler.bind(this)}],
- ['clearDatabaseCachesOffscreen', {async: false, handler: this._clearDatabaseCachesHandler.bind(this)}]
- ]);
+ this._messageHandlers = new Map(/** @type {import('offscreen').MessageHandlerMapInit} */ ([
+ ['clipboardGetTextOffscreen', this._getTextHandler.bind(this)],
+ ['clipboardGetImageOffscreen', this._getImageHandler.bind(this)],
+ ['clipboardSetBrowserOffscreen', this._setClipboardBrowser.bind(this)],
+ ['databasePrepareOffscreen', this._prepareDatabaseHandler.bind(this)],
+ ['getDictionaryInfoOffscreen', this._getDictionaryInfoHandler.bind(this)],
+ ['databasePurgeOffscreen', this._purgeDatabaseHandler.bind(this)],
+ ['databaseGetMediaOffscreen', this._getMediaHandler.bind(this)],
+ ['translatorPrepareOffscreen', this._prepareTranslatorHandler.bind(this)],
+ ['findKanjiOffscreen', this._findKanjiHandler.bind(this)],
+ ['findTermsOffscreen', this._findTermsHandler.bind(this)],
+ ['getTermFrequenciesOffscreen', this._getTermFrequenciesHandler.bind(this)],
+ ['clearDatabaseCachesOffscreen', this._clearDatabaseCachesHandler.bind(this)]
+ ]));
/* eslint-enable no-multi-spaces */
- /** @type {import('offscreen').MessageHandlerMap<string>} */
- this._messageHandlers = messageHandlers;
const onMessage = this._onMessage.bind(this);
chrome.runtime.onMessage.addListener(onMessage);
@@ -172,7 +170,7 @@ export class Offscreen {
/** @type {import('extension').ChromeRuntimeOnMessageCallback} */
_onMessage({action, params}, sender, callback) {
- const messageHandler = this._messageHandlers.get(action);
+ const messageHandler = this._messageHandlers.get(/** @type {import('offscreen').MessageType} */ (action));
if (typeof messageHandler === 'undefined') { return false; }
return invokeMessageHandler(messageHandler, params, callback, sender);
}