This commit is contained in:
Alex Yatskov 2016-05-02 17:27:26 -07:00
parent eaa7291683
commit c70aedc2ff
2 changed files with 35 additions and 11 deletions

View File

@ -18,7 +18,7 @@
function optionsToForm(opts) { function optionsToForm(opts) {
$('#enableAnkiConnect').prop('checked', opts.ankiConnect); $('#enableAnkiConnect').prop('checked', opts.enableAnkiConnect);
$('#selectMatchedText').prop('checked', opts.selectMatchedText); $('#selectMatchedText').prop('checked', opts.selectMatchedText);
$('#loadOnStartup').prop('checked', opts.loadOnStartup); $('#loadOnStartup').prop('checked', opts.loadOnStartup);
$('#scanLength').val(opts.scanLength); $('#scanLength').val(opts.scanLength);
@ -28,7 +28,7 @@ function formToOptions() {
return sanitizeOptions({ return sanitizeOptions({
loadOnStartup: $('#loadOnStartup').prop('checked'), loadOnStartup: $('#loadOnStartup').prop('checked'),
selectMatchedText: $('#selectMatchedText').prop('checked'), selectMatchedText: $('#selectMatchedText').prop('checked'),
ankiConnect: $('#enableAnkiConnect').prop('checked'), enableAnkiConnect: $('#enableAnkiConnect').prop('checked'),
scanLength: $('#scanLength').val() scanLength: $('#scanLength').val()
}); });
} }

View File

@ -50,17 +50,14 @@ class Yomichan {
onMessage(request, sender, callback) { onMessage(request, sender, callback) {
const {action, data} = request, handlers = { const {action, data} = request, handlers = {
findKanji: ({text}) => this.translator.findKanji(text), findKanji: ({text}) => this.findKanji(text, callback),
findTerm: ({text}) => this.translator.findTerm(text), findTerm: ({text}) => this.findTerm(text, callback),
getState: () => this.state, getOptions: () => callback(this.options),
getOptions: () => this.options, getState: () => callback(this.state),
renderText: ({data, template}) => Handlebars.templates[template](data) renderText: ({data, template}) => callback(Handlebars.templates[template](data))
}; };
const result = handlers[action].call(this, data); handlers[action].call(this, data);
if (callback !== null) {
callback(result);
}
} }
onBrowserAction(tab) { onBrowserAction(tab) {
@ -102,6 +99,33 @@ class Yomichan {
Yomichan.notifyChange('options', this.options); Yomichan.notifyChange('options', this.options);
} }
findTerm(text, callback) {
const results = this.translator.findTerm(text);
this.callAnkiApi('canAddNotes', results, (resultsFull) => {
callback(resultsFull || results);
});
}
findKanji(text, callback) {
const results = this.translator.findKanji(text);
this.callAnkiApi('cannAddNotes', results, (resultsFull) => {
callback(resultsFull || results);
});
}
callAnkiApi(action, data, callback) {
if (this.options.enableAnkiConnect) {
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => callback(JSON.parse(xhr.responseText)));
xhr.open('POST', 'http://127.0.0.1:8888');
xhr.withCredentials = true;
xhr.setRequestHeader('Content-Type', 'text/json');
xhr.send(JSON.stringify(data));
} else {
callback(null);
}
}
static notifyChange(name, value) { static notifyChange(name, value) {
chrome.tabs.query({}, (tabs) => { chrome.tabs.query({}, (tabs) => {
for (const tab of tabs) { for (const tab of tabs) {