summaryrefslogtreecommitdiff
path: root/ext/bg/js/api.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-08-05 13:13:06 -0700
committerAlex Yatskov <alex@foosoft.net>2017-08-05 13:13:06 -0700
commitb2003a0a560a9bd469e23e860c39dc5b21412021 (patch)
tree49304a40694712044c264268300bd2fb5651e020 /ext/bg/js/api.js
parentef43b742b0b0705e8fe44792933878c161d55d8f (diff)
cleanup
Diffstat (limited to 'ext/bg/js/api.js')
-rw-r--r--ext/bg/js/api.js102
1 files changed, 27 insertions, 75 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index ac53a7a3..103d247c 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -21,81 +21,6 @@
* Helpers
*/
-function utilMessageDispatch({action, params}, sender, callback) {
- const forward = (promise, callback) => {
- return promise.then(result => {
- callback({result});
- }).catch(error => {
- callback({error});
- });
- };
-
- const handlers = {
- optionsGet: ({callback}) => {
- forward(optionsLoad(), callback);
- },
-
- kanjiFind: ({text, callback}) => {
- forward(apiKanjiFind(text), callback);
- },
-
- termsFind: ({text, callback}) => {
- forward(apiTermsFind(text), callback);
- },
-
- templateRender: ({template, data, callback}) => {
- forward(apiTemplateRender(template, data), callback);
- },
-
- definitionAdd: ({definition, mode, callback}) => {
- forward(apiDefinitionAdd(definition, mode), callback);
- },
-
- definitionsAddable: ({definitions, modes, callback}) => {
- forward(apiDefinitionsAddable(definitions, modes), callback);
- },
-
- noteView: ({noteId}) => {
- forward(apiNoteView(noteId), callback);
- }
- };
-
- const handler = handlers[action];
- if (handler) {
- params.callback = callback;
- handler(params);
- }
-
- return true;
-}
-
-function utilCommandDispatch(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: () => {
- const options = chrome.extension.getBackgroundPage().yomichan.options;
- options.general.enable = !options.general.enable;
- optionsSave(options).then(() => apiOptionsSet(options));
- }
- };
-
- const handler = handlers[command];
- if (handler) {
- handler();
- }
-}
-
function utilNoteFormat(definition, mode) {
const options = chrome.extension.getBackgroundPage().yomichan.options;
const note = {fields: {}, tags: options.anki.tags};
@@ -142,6 +67,33 @@ function utilNoteFormat(definition, mode) {
* API
*/
+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: () => {
+ const options = chrome.extension.getBackgroundPage().yomichan.options;
+ options.general.enable = !options.general.enable;
+ optionsSave(options).then(() => apiOptionsSet(options));
+ }
+ };
+
+ const handler = handlers[command];
+ if (handler) {
+ handler();
+ }
+}
+
async function apiOptionsSet(options) {
// In Firefox, setting options from the options UI somehow carries references
// to the DOM across to the background page, causing the options object to