summaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/api.js52
-rw-r--r--ext/bg/js/backend.js84
2 files changed, 48 insertions, 88 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index 474fe604..222e7ffe 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -126,35 +126,35 @@ async function apiTemplateRender(template, data, dynamic) {
}
async function apiCommandExec(command) {
- const handlers = {
- search: () => {
- chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')});
- },
-
- help: () => {
- chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'});
- },
-
- options: () => {
- chrome.runtime.openOptionsPage();
- },
-
- toggle: async () => {
- const optionsContext = {
- depth: 0,
- url: window.location.href
- };
- const options = await apiOptionsGet(optionsContext);
- options.general.enable = !options.general.enable;
- await apiOptionsSave('popup');
- }
- };
-
- const handler = handlers[command];
- if (handler) {
+ const handlers = apiCommandExec.handlers;
+ if (handlers.hasOwnProperty(command)) {
+ const handler = handlers[command];
handler();
}
}
+apiCommandExec.handlers = {
+ search: () => {
+ chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')});
+ },
+
+ help: () => {
+ chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'});
+ },
+
+ options: () => {
+ chrome.runtime.openOptionsPage();
+ },
+
+ toggle: async () => {
+ const optionsContext = {
+ depth: 0,
+ url: window.location.href
+ };
+ const options = await apiOptionsGet(optionsContext);
+ options.general.enable = !options.general.enable;
+ await apiOptionsSave('popup');
+ }
+};
async function apiAudioGetUrl(definition, source) {
return audioBuildUrl(definition, source);
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 4068b760..3c5ad885 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -69,68 +69,13 @@ class Backend {
}
onMessage({action, params}, sender, callback) {
- const forward = (promise, callback) => {
- return promise.then(result => {
- callback({result});
- }).catch(error => {
- callback({error: error.toString ? error.toString() : error});
- });
- };
-
- const handlers = {
- optionsGet: ({optionsContext, callback}) => {
- forward(apiOptionsGet(optionsContext), callback);
- },
-
- kanjiFind: ({text, optionsContext, callback}) => {
- forward(apiKanjiFind(text, optionsContext), callback);
- },
-
- termsFind: ({text, optionsContext, callback}) => {
- forward(apiTermsFind(text, optionsContext), callback);
- },
-
- definitionAdd: ({definition, mode, context, optionsContext, callback}) => {
- forward(apiDefinitionAdd(definition, mode, context, optionsContext), callback);
- },
-
- definitionsAddable: ({definitions, modes, optionsContext, callback}) => {
- forward(apiDefinitionsAddable(definitions, modes, optionsContext), callback);
- },
-
- noteView: ({noteId}) => {
- forward(apiNoteView(noteId), callback);
- },
-
- templateRender: ({template, data, dynamic, callback}) => {
- forward(apiTemplateRender(template, data, dynamic), callback);
- },
-
- commandExec: ({command, callback}) => {
- forward(apiCommandExec(command), callback);
- },
-
- audioGetUrl: ({definition, source, callback}) => {
- forward(apiAudioGetUrl(definition, source), callback);
- },
-
- screenshotGet: ({options}) => {
- forward(apiScreenshotGet(options, sender), callback);
- },
-
- forward: ({action, params}) => {
- forward(apiForward(action, params, sender), callback);
- },
-
- frameInformationGet: () => {
- forward(apiFrameInformationGet(sender), callback);
- }
- };
-
- const handler = handlers[action];
- if (handler) {
- params.callback = callback;
- handler(params);
+ const handlers = Backend.messageHandlers;
+ if (handlers.hasOwnProperty(action)) {
+ const handler = handlers[action];
+ const promise = handler(params, sender);
+ promise
+ .then(result => callback({result}))
+ .catch(error => callback({error: typeof error.toString === 'function' ? error.toString() : error}));
}
return true;
@@ -227,5 +172,20 @@ class Backend {
}
}
+Backend.messageHandlers = {
+ optionsGet: ({optionsContext}) => apiOptionsGet(optionsContext),
+ kanjiFind: ({text, optionsContext}) => apiKanjiFind(text, optionsContext),
+ termsFind: ({text, optionsContext}) => apiTermsFind(text, optionsContext),
+ definitionAdd: ({definition, mode, context, optionsContext}) => apiDefinitionAdd(definition, mode, context, optionsContext),
+ definitionsAddable: ({definitions, modes, optionsContext}) => apiDefinitionsAddable(definitions, modes, optionsContext),
+ noteView: ({noteId}) => apiNoteView(noteId),
+ templateRender: ({template, data, dynamic}) => apiTemplateRender(template, data, dynamic),
+ commandExec: ({command}) => apiCommandExec(command),
+ audioGetUrl: ({definition, source}) => apiAudioGetUrl(definition, source),
+ screenshotGet: ({options}, sender) => apiScreenshotGet(options, sender),
+ forward: ({action, params}, sender) => apiForward(action, params, sender),
+ frameInformationGet: (params, sender) => apiFrameInformationGet(sender),
+};
+
window.yomichan_backend = new Backend();
window.yomichan_backend.prepare();