summaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/backend.js54
-rw-r--r--ext/bg/js/search.js11
2 files changed, 30 insertions, 35 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 6e594f9b..07c32fa2 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -294,28 +294,16 @@ class Backend {
const messageHandler = this._messageHandlers.get(action);
if (typeof messageHandler === 'undefined') { return false; }
- const {handler, async, contentScript} = messageHandler;
-
- try {
- if (!contentScript) {
+ if (!messageHandler.contentScript) {
+ try {
this._validatePrivilegedMessageSender(sender);
- }
-
- const promiseOrResult = handler(params, sender);
- if (async) {
- promiseOrResult.then(
- (result) => callback({result}),
- (error) => callback({error: errorToJson(error)})
- );
- return true;
- } else {
- callback({result: promiseOrResult});
+ } catch (error) {
+ callback({error: errorToJson(error)});
return false;
}
- } catch (error) {
- callback({error: errorToJson(error)});
- return false;
}
+
+ return yomichan.invokeMessageHandler(messageHandler, params, callback, sender);
}
_onConnect(port) {
@@ -814,11 +802,7 @@ class Backend {
if (tab !== null) {
await this._focusTab(tab);
if (queryParams.query) {
- await new Promise((resolve) => chrome.tabs.sendMessage(
- tab.id,
- {action: 'searchQueryUpdate', params: {text: queryParams.query}},
- resolve
- ));
+ await this._updateSearchQuery(tab.id, queryParams.query);
}
return true;
}
@@ -882,6 +866,21 @@ class Backend {
// Utilities
+ _updateSearchQuery(tabId, text) {
+ new Promise((resolve, reject) => {
+ const callback = (response) => {
+ try {
+ resolve(yomichan.getMessageResponseResult(response));
+ } catch (error) {
+ reject(error);
+ }
+ };
+
+ const message = {action: 'updateSearchQuery', params: {text}};
+ chrome.tabs.sendMessage(tabId, message, callback);
+ });
+ }
+
_sendMessageAllTabs(action, params={}) {
const callback = () => this._checkLastError(chrome.runtime.lastError);
chrome.tabs.query({}, (tabs) => {
@@ -1286,11 +1285,10 @@ class Backend {
return new Promise((resolve) => {
chrome.tabs.sendMessage(tab.id, {action: 'getUrl'}, {frameId: 0}, (response) => {
let url = null;
- if (!chrome.runtime.lastError) {
- url = (response !== null && typeof response === 'object' && !Array.isArray(response) ? response.url : null);
- if (url !== null && typeof url !== 'string') {
- url = null;
- }
+ try {
+ url = yomichan.getMessageResponseResult(response);
+ } catch (error) {
+ // NOP
}
resolve({tab, url});
});
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js
index 9bbc66f2..52fc19f8 100644
--- a/ext/bg/js/search.js
+++ b/ext/bg/js/search.js
@@ -61,7 +61,7 @@ class DisplaySearch extends Display {
['Shift', new Set()]
]);
this._runtimeMessageHandlers = new Map([
- ['searchQueryUpdate', this._onExternalSearchUpdate.bind(this)]
+ ['updateSearchQuery', {async: false, handler: this._onExternalSearchUpdate.bind(this)}]
]);
this.setOptionsContext({
@@ -206,12 +206,9 @@ class DisplaySearch extends Display {
}
_onRuntimeMessage({action, params}, sender, callback) {
- const handler = this._runtimeMessageHandlers.get(action);
- if (typeof handler !== 'function') { return false; }
-
- const result = handler(params, sender);
- callback(result);
- return false;
+ const messageHandler = this._runtimeMessageHandlers.get(action);
+ if (typeof messageHandler === 'undefined') { return false; }
+ return yomichan.invokeMessageHandler(messageHandler, params, callback, sender);
}
_onCopy() {