aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/backend.js114
-rw-r--r--ext/fg/js/float.js6
-rw-r--r--ext/fg/js/frame-offset-forwarder.js4
-rw-r--r--ext/fg/js/frontend-initialize.js4
-rw-r--r--ext/fg/js/frontend.js6
-rw-r--r--ext/mixed/js/api.js4
-rw-r--r--ext/mixed/js/display.js4
7 files changed, 74 insertions, 68 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 6386319b..be8ea322 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -76,33 +76,33 @@ class Backend {
this.messageToken = yomichan.generateId(16);
this._messageHandlers = new Map([
- ['yomichanCoreReady', this._onApiYomichanCoreReady.bind(this)],
- ['optionsSchemaGet', this._onApiOptionsSchemaGet.bind(this)],
- ['optionsGet', this._onApiOptionsGet.bind(this)],
- ['optionsGetFull', this._onApiOptionsGetFull.bind(this)],
- ['optionsSet', this._onApiOptionsSet.bind(this)],
- ['optionsSave', this._onApiOptionsSave.bind(this)],
- ['kanjiFind', this._onApiKanjiFind.bind(this)],
- ['termsFind', this._onApiTermsFind.bind(this)],
- ['textParse', this._onApiTextParse.bind(this)],
- ['textParseMecab', this._onApiTextParseMecab.bind(this)],
- ['definitionAdd', this._onApiDefinitionAdd.bind(this)],
- ['definitionsAddable', this._onApiDefinitionsAddable.bind(this)],
- ['noteView', this._onApiNoteView.bind(this)],
- ['templateRender', this._onApiTemplateRender.bind(this)],
- ['commandExec', this._onApiCommandExec.bind(this)],
- ['audioGetUri', this._onApiAudioGetUri.bind(this)],
- ['screenshotGet', this._onApiScreenshotGet.bind(this)],
- ['forward', this._onApiForward.bind(this)],
- ['frameInformationGet', this._onApiFrameInformationGet.bind(this)],
- ['injectStylesheet', this._onApiInjectStylesheet.bind(this)],
- ['getEnvironmentInfo', this._onApiGetEnvironmentInfo.bind(this)],
- ['clipboardGet', this._onApiClipboardGet.bind(this)],
- ['getDisplayTemplatesHtml', this._onApiGetDisplayTemplatesHtml.bind(this)],
- ['getQueryParserTemplatesHtml', this._onApiGetQueryParserTemplatesHtml.bind(this)],
- ['getZoom', this._onApiGetZoom.bind(this)],
- ['getMessageToken', this._onApiGetMessageToken.bind(this)],
- ['getDefaultAnkiFieldTemplates', this._onApiGetDefaultAnkiFieldTemplates.bind(this)]
+ ['yomichanCoreReady', {handler: this._onApiYomichanCoreReady.bind(this), async: false}],
+ ['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: false}],
+ ['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: false}],
+ ['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: false}],
+ ['optionsSet', {handler: this._onApiOptionsSet.bind(this), async: true}],
+ ['optionsSave', {handler: this._onApiOptionsSave.bind(this), async: true}],
+ ['kanjiFind', {handler: this._onApiKanjiFind.bind(this), async: true}],
+ ['termsFind', {handler: this._onApiTermsFind.bind(this), async: true}],
+ ['textParse', {handler: this._onApiTextParse.bind(this), async: true}],
+ ['textParseMecab', {handler: this._onApiTextParseMecab.bind(this), async: true}],
+ ['definitionAdd', {handler: this._onApiDefinitionAdd.bind(this), async: true}],
+ ['definitionsAddable', {handler: this._onApiDefinitionsAddable.bind(this), async: true}],
+ ['noteView', {handler: this._onApiNoteView.bind(this), async: true}],
+ ['templateRender', {handler: this._onApiTemplateRender.bind(this), async: true}],
+ ['commandExec', {handler: this._onApiCommandExec.bind(this), async: false}],
+ ['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}],
+ ['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}],
+ ['broadcastTab', {handler: this._onApiBroadcastTab.bind(this), async: false}],
+ ['frameInformationGet', {handler: this._onApiFrameInformationGet.bind(this), async: true}],
+ ['injectStylesheet', {handler: this._onApiInjectStylesheet.bind(this), async: true}],
+ ['getEnvironmentInfo', {handler: this._onApiGetEnvironmentInfo.bind(this), async: true}],
+ ['clipboardGet', {handler: this._onApiClipboardGet.bind(this), async: true}],
+ ['getDisplayTemplatesHtml', {handler: this._onApiGetDisplayTemplatesHtml.bind(this), async: true}],
+ ['getQueryParserTemplatesHtml', {handler: this._onApiGetQueryParserTemplatesHtml.bind(this), async: true}],
+ ['getZoom', {handler: this._onApiGetZoom.bind(this), async: true}],
+ ['getMessageToken', {handler: this._onApiGetMessageToken.bind(this), async: false}],
+ ['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: false}]
]);
this._commandHandlers = new Map([
@@ -166,16 +166,23 @@ class Backend {
}
onMessage({action, params}, sender, callback) {
- const handler = this._messageHandlers.get(action);
- if (typeof handler !== 'function') { return false; }
+ const messageHandler = this._messageHandlers.get(action);
+ if (typeof messageHandler === 'undefined') { return false; }
+
+ const {handler, async} = messageHandler;
try {
- const promise = handler(params, sender);
- promise.then(
- (result) => callback({result}),
- (error) => callback({error: errorToJson(error)})
- );
- return true;
+ const promiseOrResult = handler(params, sender);
+ if (async) {
+ promiseOrResult.then(
+ (result) => callback({result}),
+ (error) => callback({error: errorToJson(error)})
+ );
+ return true;
+ } else {
+ callback({result: promiseOrResult});
+ return false;
+ }
} catch (error) {
callback({error: errorToJson(error)});
return false;
@@ -312,27 +319,26 @@ class Backend {
_onApiYomichanCoreReady(_params, sender) {
// tab ID isn't set in background (e.g. browser_action)
+ const callback = () => this.checkLastError(chrome.runtime.lastError);
+ const data = {action: 'backendPrepared'};
if (typeof sender.tab === 'undefined') {
- const callback = () => this.checkLastError(chrome.runtime.lastError);
- chrome.runtime.sendMessage({action: 'backendPrepared'}, callback);
- return Promise.resolve();
+ chrome.runtime.sendMessage(data, callback);
+ return false;
+ } else {
+ chrome.tabs.sendMessage(sender.tab.id, data, callback);
+ return true;
}
-
- const tabId = sender.tab.id;
- return new Promise((resolve) => {
- chrome.tabs.sendMessage(tabId, {action: 'backendPrepared'}, resolve);
- });
}
- async _onApiOptionsSchemaGet() {
+ _onApiOptionsSchemaGet() {
return this.getOptionsSchema();
}
- async _onApiOptionsGet({optionsContext}) {
+ _onApiOptionsGet({optionsContext}) {
return this.getOptions(optionsContext);
}
- async _onApiOptionsGetFull() {
+ _onApiOptionsGetFull() {
return this.getFullOptions();
}
@@ -539,7 +545,7 @@ class Backend {
return this._renderTemplate(template, data);
}
- async _onApiCommandExec({command, params}) {
+ _onApiCommandExec({command, params}) {
return this._runCommand(command, params);
}
@@ -559,15 +565,15 @@ class Backend {
});
}
- _onApiForward({action, params}, sender) {
+ _onApiBroadcastTab({action, params}, sender) {
if (!(sender && sender.tab)) {
- return Promise.resolve();
+ return false;
}
const tabId = sender.tab.id;
- return new Promise((resolve) => {
- chrome.tabs.sendMessage(tabId, {action, params}, (response) => resolve(response));
- });
+ const callback = () => this.checkLastError(chrome.runtime.lastError);
+ chrome.tabs.sendMessage(tabId, {action, params}, callback);
+ return true;
}
_onApiFrameInformationGet(params, sender) {
@@ -690,11 +696,11 @@ class Backend {
});
}
- async _onApiGetMessageToken() {
+ _onApiGetMessageToken() {
return this.messageToken;
}
- async _onApiGetDefaultAnkiFieldTemplates() {
+ _onApiGetDefaultAnkiFieldTemplates() {
return this.defaultAnkiFieldTemplates;
}
diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js
index 77e5ea0a..5c2c50c2 100644
--- a/ext/fg/js/float.js
+++ b/ext/fg/js/float.js
@@ -17,7 +17,7 @@
/* global
* Display
- * apiForward
+ * apiBroadcastTab
* apiGetMessageToken
* popupNestedInitialize
*/
@@ -79,7 +79,7 @@ class DisplayFloat extends Display {
this.setContentScale(scale);
- apiForward('popupPrepareCompleted', {targetPopupId: this._popupId});
+ apiBroadcastTab('popupPrepareCompleted', {targetPopupId: this._popupId});
}
onError(error) {
@@ -180,7 +180,7 @@ class DisplayFloat extends Display {
},
2000
);
- apiForward('requestDocumentInformationBroadcast', {uniqueId});
+ apiBroadcastTab('requestDocumentInformationBroadcast', {uniqueId});
const {title} = await promise;
return title;
diff --git a/ext/fg/js/frame-offset-forwarder.js b/ext/fg/js/frame-offset-forwarder.js
index b3c10bb8..c658c55a 100644
--- a/ext/fg/js/frame-offset-forwarder.js
+++ b/ext/fg/js/frame-offset-forwarder.js
@@ -16,7 +16,7 @@
*/
/* global
- * apiForward
+ * apiBroadcastTab
*/
class FrameOffsetForwarder {
@@ -96,6 +96,6 @@ class FrameOffsetForwarder {
}
_forwardFrameOffsetOrigin(offset, uniqueId) {
- apiForward('frameOffset', {offset, uniqueId});
+ apiBroadcastTab('frameOffset', {offset, uniqueId});
}
}
diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js
index 0a586ff9..5af7fdf0 100644
--- a/ext/fg/js/frontend-initialize.js
+++ b/ext/fg/js/frontend-initialize.js
@@ -20,7 +20,7 @@
* Frontend
* PopupProxy
* PopupProxyHost
- * apiForward
+ * apiBroadcastTab
* apiOptionsGet
*/
@@ -43,7 +43,7 @@ async function main() {
}
}
);
- apiForward('rootPopupRequestInformationBroadcast');
+ apiBroadcastTab('rootPopupRequestInformationBroadcast');
const {popupId, frameId} = await rootPopupInformationPromise;
const frameOffsetForwarder = new FrameOffsetForwarder();
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index 6fbbd0fb..55d699e5 100644
--- a/ext/fg/js/frontend.js
+++ b/ext/fg/js/frontend.js
@@ -17,7 +17,7 @@
/* global
* TextScanner
- * apiForward
+ * apiBroadcastTab
* apiGetZoom
* apiKanjiFind
* apiOptionsGet
@@ -260,12 +260,12 @@ class Frontend extends TextScanner {
_broadcastRootPopupInformation() {
if (!this.popup.isProxy() && this.popup.depth === 0) {
- apiForward('rootPopupInformation', {popupId: this.popup.id, frameId: this.popup.frameId});
+ apiBroadcastTab('rootPopupInformation', {popupId: this.popup.id, frameId: this.popup.frameId});
}
}
_broadcastDocumentInformation(uniqueId) {
- apiForward('documentInformationBroadcast', {
+ apiBroadcastTab('documentInformationBroadcast', {
uniqueId,
frameId: this.popup.frameId,
title: document.title
diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js
index 066077cf..50b285a5 100644
--- a/ext/mixed/js/api.js
+++ b/ext/mixed/js/api.js
@@ -80,8 +80,8 @@ function apiScreenshotGet(options) {
return _apiInvoke('screenshotGet', {options});
}
-function apiForward(action, params) {
- return _apiInvoke('forward', {action, params});
+function apiBroadcastTab(action, params) {
+ return _apiInvoke('broadcastTab', {action, params});
}
function apiFrameInformationGet() {
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index bf6990a1..63687dc2 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -22,9 +22,9 @@
* DisplayGenerator
* WindowScroll
* apiAudioGetUri
+ * apiBroadcastTab
* apiDefinitionAdd
* apiDefinitionsAddable
- * apiForward
* apiKanjiFind
* apiNoteView
* apiOptionsGet
@@ -854,7 +854,7 @@ class Display {
}
setPopupVisibleOverride(visible) {
- return apiForward('popupSetVisibleOverride', {visible});
+ return apiBroadcastTab('popupSetVisibleOverride', {visible});
}
setSpinnerVisible(visible) {