more cleanup

This commit is contained in:
Alex Yatskov 2017-08-05 19:02:03 -07:00
parent 257c864bb5
commit 8e1c6776d1
3 changed files with 140 additions and 126 deletions

View File

@ -17,72 +17,34 @@
*/ */
async function apiCommandExec(command) { /*
const handlers = { * Backend
search: () => { */
chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')});
},
help: () => { function backend() {
chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'}); return chrome.extension.getBackgroundPage().yomichan_backend;
},
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();
}
} }
/*
* API
*/
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
// become a "DeadObject" after the options page is closed. The workaround used // become a "DeadObject" after the options page is closed. The workaround used
// here is to create a deep copy of the options object. // here is to create a deep copy of the options object.
const yomichan = chrome.extension.getBackgroundPage().yomichan; backend().optionsSet(JSON.parse(JSON.stringify(options)));
yomichan.options = JSON.parse(JSON.stringify(options));
if (!options.general.enable) {
chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'});
chrome.browserAction.setBadgeText({text: 'off'});
} else if (!dictConfigured(options)) {
chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'});
chrome.browserAction.setBadgeText({text: '!'});
} else {
chrome.browserAction.setBadgeText({text: ''});
}
if (options.anki.enable) {
yomichan.anki = new AnkiConnect(options.anki.server);
} else {
yomichan.anki = new AnkiNull();
}
chrome.tabs.query({}, tabs => {
for (const tab of tabs) {
chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null);
}
});
} }
async function apiOptionsGet() { async function apiOptionsGet() {
return chrome.extension.getBackgroundPage().yomichan.options; return backend().options;
} }
async function apiTermsFind(text) { async function apiTermsFind(text) {
const yomichan = chrome.extension.getBackgroundPage().yomichan; const options = backend().options;
const options = yomichan.options; const translator = backend().translator;
const translator = yomichan.translator;
const searcher = options.general.groupResults ? const searcher = options.general.groupResults ?
translator.findTermsGrouped.bind(translator) : translator.findTermsGrouped.bind(translator) :
@ -101,15 +63,13 @@ async function apiTermsFind(text) {
} }
async function apiKanjiFind(text) { async function apiKanjiFind(text) {
const yomichan = chrome.extension.getBackgroundPage().yomichan; const options = backend().options;
const options = yomichan.options; const definitions = await backend().translator.findKanji(text, dictEnabledSet(options));
const definitions = await yomichan.translator.findKanji(text, dictEnabledSet(options));
return definitions.slice(0, options.general.maxResults); return definitions.slice(0, options.general.maxResults);
} }
async function apiDefinitionAdd(definition, mode) { async function apiDefinitionAdd(definition, mode) {
const yomichan = chrome.extension.getBackgroundPage().yomichan; const options = backend().options;
const options = yomichan.options;
if (mode !== 'kanji') { if (mode !== 'kanji') {
await audioInject( await audioInject(
@ -119,21 +79,18 @@ async function apiDefinitionAdd(definition, mode) {
); );
} }
return yomichan.anki.addNote(dictNoteFormat(definition, mode, options)); return backend().anki.addNote(dictNoteFormat(definition, mode, options));
} }
async function apiDefinitionsAddable(definitions, modes) { async function apiDefinitionsAddable(definitions, modes) {
const yomichan = chrome.extension.getBackgroundPage().yomichan;
const options = yomichan.options;
const notes = []; const notes = [];
for (const definition of definitions) { for (const definition of definitions) {
for (const mode of modes) { for (const mode of modes) {
notes.push(dictNoteFormat(definition, mode, options)); notes.push(dictNoteFormat(definition, mode, backend().options));
} }
} }
const results = await yomichan.anki.canAddNotes(notes); const results = await backend().anki.canAddNotes(notes);
const states = []; const states = [];
for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) {
const state = {}; const state = {};
@ -148,10 +105,38 @@ async function apiDefinitionsAddable(definitions, modes) {
} }
async function apiNoteView(noteId) { async function apiNoteView(noteId) {
const yomichan = chrome.extension.getBackgroundPage().yomichan; return backend().anki.guiBrowse(`nid:${noteId}`);
return yomichan.anki.guiBrowse(`nid:${noteId}`);
} }
async function apiTemplateRender(template, data) { async function apiTemplateRender(template, data) {
return handlebarsRender(template, data); return handlebarsRender(template, data);
} }
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 options = backend().options;
options.general.enable = !options.general.enable;
await optionsSave(options);
await apiOptionsSet(options);
}
};
const handler = handlers[command];
if (handler) {
handler();
}
}

View File

@ -17,67 +17,102 @@
*/ */
window.yomichan = new class { window.yomichan_backend = new class {
constructor() { constructor() {
this.translator = new Translator(); this.translator = new Translator();
this.anki = new AnkiNull(); this.anki = new AnkiNull();
this.options = null; this.options = null;
}
this.translator.prepare().then(optionsLoad).then(options => { async prepare() {
apiOptionsSet(options); await this.translator.prepare();
await apiOptionsSet(await optionsLoad());
chrome.commands.onCommand.addListener(apiCommandExec); chrome.commands.onCommand.addListener(this.onCommand.bind(this));
chrome.runtime.onMessage.addListener(({action, params}, sender, callback) => { chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
const forward = (promise, callback) => {
return promise.then(result => {
callback({result});
}).catch(error => {
callback({error});
});
};
const handlers = { if (this.options.general.showGuide) {
optionsGet: ({callback}) => { chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')});
forward(optionsLoad(), callback); }
}, }
kanjiFind: ({text, callback}) => { optionsSet(options) {
forward(apiKanjiFind(text), callback); this.options = options;
},
termsFind: ({text, callback}) => { if (!options.general.enable) {
forward(apiTermsFind(text), callback); chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'});
}, chrome.browserAction.setBadgeText({text: 'off'});
} else if (!dictConfigured(options)) {
chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'});
chrome.browserAction.setBadgeText({text: '!'});
} else {
chrome.browserAction.setBadgeText({text: ''});
}
templateRender: ({template, data, callback}) => { if (options.anki.enable) {
forward(apiTemplateRender(template, data), callback); backend().anki = new AnkiConnect(options.anki.server);
}, } else {
backend().anki = new AnkiNull();
}
definitionAdd: ({definition, mode, callback}) => { chrome.tabs.query({}, tabs => {
forward(apiDefinitionAdd(definition, mode), callback); for (const tab of tabs) {
}, chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null);
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) {
chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')});
} }
}); });
} }
onCommand(command) {
apiCommandExec(command);
}
onMessage({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;
}
}; };
window.yomichan_backend.prepare();

View File

@ -22,33 +22,27 @@
*/ */
function utilAnkiGetModelNames() { function utilAnkiGetModelNames() {
const yomichan = chrome.extension.getBackgroundPage().yomichan; return backend().anki.getModelNames();
return yomichan.anki.getModelNames();
} }
function utilAnkiGetDeckNames() { function utilAnkiGetDeckNames() {
const yomichan = chrome.extension.getBackgroundPage().yomichan; return backend().anki.getDeckNames();
return yomichan.anki.getDeckNames();
} }
function utilAnkiGetModelFieldNames(modelName) { function utilAnkiGetModelFieldNames(modelName) {
const yomichan = chrome.extension.getBackgroundPage().yomichan; return backend().anki.getModelFieldNames(modelName);
return yomichan.anki.getModelFieldNames(modelName);
} }
function utilDatabaseGetDictionaries() { function utilDatabaseGetDictionaries() {
const yomichan = chrome.extension.getBackgroundPage().yomichan; return backend().translator.database.getDictionaries();
return yomichan.translator.database.getDictionaries();
} }
function utilDatabasePurge() { function utilDatabasePurge() {
const yomichan = chrome.extension.getBackgroundPage().yomichan; return backend().translator.database.purge();
return yomichan.translator.database.purge();
} }
function utilDatabaseImport(data, progress) { function utilDatabaseImport(data, progress) {
const yomichan = chrome.extension.getBackgroundPage().yomichan; return backend().translator.database.importDictionary(data, progress);
return yomichan.translator.database.importDictionary(data, progress);
} }