This commit is contained in:
Alex Yatskov 2017-08-05 13:13:06 -07:00
parent ef43b742b0
commit b2003a0a56
3 changed files with 79 additions and 81 deletions

View File

@ -21,81 +21,6 @@
* Helpers * 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) { function utilNoteFormat(definition, mode) {
const options = chrome.extension.getBackgroundPage().yomichan.options; const options = chrome.extension.getBackgroundPage().yomichan.options;
const note = {fields: {}, tags: options.anki.tags}; const note = {fields: {}, tags: options.anki.tags};
@ -142,6 +67,33 @@ function utilNoteFormat(definition, mode) {
* API * 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) { async function apiOptionsSet(options) {
// In Firefox, setting options from the options UI somehow carries references // In Firefox, setting options from the options UI somehow carries references
// to the DOM across to the background page, causing the options object to // to the DOM across to the background page, causing the options object to

View File

@ -26,8 +26,54 @@ window.yomichan = new class {
this.translator.prepare().then(optionsLoad).then(options => { this.translator.prepare().then(optionsLoad).then(options => {
apiOptionsSet(options); apiOptionsSet(options);
chrome.commands.onCommand.addListener(utilCommandDispatch); chrome.commands.onCommand.addListener(apiCommandExec);
chrome.runtime.onMessage.addListener(utilMessageDispatch); chrome.runtime.onMessage.addListener(({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;
});
if (options.general.showGuide) { if (options.general.showGuide) {
chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')});

View File

@ -18,14 +18,14 @@
$(document).ready(() => { $(document).ready(() => {
$('#open-search').click(() => utilCommandDispatch('search')); $('#open-search').click(() => apiCommandExec('search'));
$('#open-options').click(() => utilCommandDispatch('options')); $('#open-options').click(() => apiCommandExec('options'));
$('#open-help').click(() => utilCommandDispatch('help')); $('#open-help').click(() => apiCommandExec('help'));
optionsLoad().then(options => { optionsLoad().then(options => {
const toggle = $('#enable-search'); const toggle = $('#enable-search');
toggle.prop('checked', options.general.enable).change(); toggle.prop('checked', options.general.enable).change();
toggle.bootstrapToggle(); toggle.bootstrapToggle();
toggle.change(() => utilCommandDispatch('toggle')); toggle.change(() => apiCommandExec('toggle'));
}); });
}); });