This commit is contained in:
Alex Yatskov 2017-07-27 21:42:14 -07:00
parent eed630e9fa
commit adbc7a32cc
4 changed files with 71 additions and 30 deletions

View File

@ -92,7 +92,8 @@ function utilCommandDispatch(command) {
} }
function utilNoteFormat(definition, mode) { function utilNoteFormat(definition, mode) {
const options = Backend.instance().options; const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
const options = yomichan.options;
const note = {fields: {}, tags: options.anki.tags}; const note = {fields: {}, tags: options.anki.tags};
let fields = []; let fields = [];
@ -137,7 +138,8 @@ async function apiOptionsSet(options) {
// 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.
Backend.instance().options = JSON.parse(JSON.stringify(options)); const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
yomichan.options = JSON.parse(JSON.stringify(options));
if (!options.general.enable) { if (!options.general.enable) {
chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'}); chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'});
@ -150,9 +152,9 @@ async function apiOptionsSet(options) {
} }
if (options.anki.enable) { if (options.anki.enable) {
Backend.instance().anki = new AnkiConnect(options.anki.server); yomichan.anki = new AnkiConnect(options.anki.server);
} else { } else {
Backend.instance().anki = new AnkiNull(); yomichan.anki = new AnkiNull();
} }
chrome.tabs.query({}, tabs => { chrome.tabs.query({}, tabs => {
@ -163,12 +165,14 @@ async function apiOptionsSet(options) {
} }
async function apiOptionsGet() { async function apiOptionsGet() {
return Backend.instance().options; const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
return yomichan.options;
} }
async function apiTermsFind(text) { async function apiTermsFind(text) {
const options = Backend.instance().options; const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
const translator = Backend.instance().translator; const options = yomichan.options;
const translator = yomichan.translator;
const searcher = options.general.groupResults ? const searcher = options.general.groupResults ?
translator.findTermsGrouped.bind(translator) : translator.findTermsGrouped.bind(translator) :
@ -187,14 +191,17 @@ async function apiTermsFind(text) {
} }
async function apiKanjiFind(text) { async function apiKanjiFind(text) {
const options = Backend.instance().options; const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
const definitions = await Backend.instance().translator.findKanji(text, dictEnabledSet(options)); const options = yomichan.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().yomichanBackend;
if (mode !== 'kanji') { if (mode !== 'kanji') {
const options = Backend.instance().options; const options = yomichan.options;
await audioInject( await audioInject(
definition, definition,
options.anki.terms.fields, options.anki.terms.fields,
@ -202,7 +209,7 @@ async function apiDefinitionAdd(definition, mode) {
); );
} }
return Backend.instance().anki.addNote(utilNoteFormat(definition, mode)); return yomichan.anki.addNote(utilNoteFormat(definition, mode));
} }
async function apiDefinitionsAddable(definitions, modes) { async function apiDefinitionsAddable(definitions, modes) {
@ -213,7 +220,8 @@ async function apiDefinitionsAddable(definitions, modes) {
} }
} }
const results = await Backend.instance().anki.canAddNotes(notes); const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
const results = await yomichan.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 = {};
@ -228,7 +236,8 @@ async function apiDefinitionsAddable(definitions, modes) {
} }
async function apiNoteView(noteId) { async function apiNoteView(noteId) {
return Backend.instance().anki.guiBrowse(`nid:${noteId}`); const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
return yomichan.anki.guiBrowse(`nid:${noteId}`);
} }
async function apiTemplateRender(template, data) { async function apiTemplateRender(template, data) {

View File

@ -17,7 +17,7 @@
*/ */
class Backend { window.yomichanBackend = new class {
constructor() { constructor() {
handlebarsRegister(); handlebarsRegister();
@ -36,10 +36,4 @@ class Backend {
} }
}); });
} }
};
static instance() {
return chrome.extension.getBackgroundPage().yomichanBackend;
}
}
window.yomichanBackend = new Backend();

View File

@ -17,6 +17,41 @@
*/ */
/*
* Utilities
*/
function utilAnkiGetModelNames() {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
return yomichan.anki.getModelNames();
}
function utilAnkiGetDeckNames() {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
return yomichan.anki.getDeckNames();
}
function utilAnkiGetModelFieldNames(modelName) {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
return yomichan.anki.utilAnkiGetModelFieldNames(modelName);
}
function utilDatabaseGetDictionaries() {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
return yomichan.translator.database.getDictionaries();
}
function utilDatabasePurge() {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
return yomichan.translator.database.purge();
}
function utilDatabaseImport(data, progress) {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend;
return yomichan.translator.database.importDictionary(data, progress);
}
/* /*
* General * General
*/ */
@ -217,7 +252,7 @@ async function dictionaryGroupsPopulate(options) {
const dictGroups = $('#dict-groups').empty(); const dictGroups = $('#dict-groups').empty();
const dictWarning = $('#dict-warning').hide(); const dictWarning = $('#dict-warning').hide();
const dictRows = await instDb().getDictionaries(); const dictRows = await utilDatabaseGetDictionaries();
if (dictRows.length === 0) { if (dictRows.length === 0) {
dictWarning.show(); dictWarning.show();
} }
@ -253,7 +288,7 @@ async function onDictionaryPurge(e) {(async () => {
dictionaryErrorShow(); dictionaryErrorShow();
dictionarySpinnerShow(true); dictionarySpinnerShow(true);
await instDb().purge(); await utilDatabasePurge();
const options = await optionsLoad(); const options = await optionsLoad();
options.dictionaries = {}; options.dictionaries = {};
await optionsSave(options); await optionsSave(options);
@ -283,7 +318,7 @@ function onDictionaryImport(e) {(async () => {
setProgress(0.0); setProgress(0.0);
const options = await optionsLoad(); const options = await optionsLoad();
const summary = await instDb().importDictionary(e.target.files[0], updateProgress); const summary = await utilDatabaseImport(e.target.files[0], updateProgress);
options.dictionaries[summary.title] = {enabled: true, priority: 0}; options.dictionaries[summary.title] = {enabled: true, priority: 0};
await optionsSave(options); await optionsSave(options);
@ -344,12 +379,12 @@ async function ankiDeckAndModelPopulate(options) {
$('#anki-terms-deck').val(options.anki.terms.deck); $('#anki-terms-deck').val(options.anki.terms.deck);
$('#anki-kanji-deck').val(options.anki.kanji.deck); $('#anki-kanji-deck').val(options.anki.kanji.deck);
const deckNames = await instAnki().getDeckNames(); const deckNames = await utilAnkiGetDeckNames();
const ankiDeck = $('.anki-deck'); const ankiDeck = $('.anki-deck');
ankiDeck.find('option').remove(); ankiDeck.find('option').remove();
deckNames.sort().forEach(name => ankiDeck.append($('<option/>', {value: name, text: name}))); deckNames.sort().forEach(name => ankiDeck.append($('<option/>', {value: name, text: name})));
const modelNames = await instAnki().getModelNames(); const modelNames = await utilAnkiGetModelNames();
const ankiModel = $('.anki-model'); const ankiModel = $('.anki-model');
ankiModel.find('option').remove(); ankiModel.find('option').remove();
modelNames.sort().forEach(name => ankiModel.append($('<option/>', {value: name, text: name}))); modelNames.sort().forEach(name => ankiModel.append($('<option/>', {value: name, text: name})));
@ -398,7 +433,7 @@ async function ankiFieldsPopulate(element, options) {
] ]
}[tabId] || {}; }[tabId] || {};
for (const name of await instAnki().getModelFieldNames(modelName)) { for (const name of await utilAnkiGetModelFieldNames(modelName)) {
const value = options.anki[tabId].fields[name] || ''; const value = options.anki[tabId].fields[name] || '';
const html = Handlebars.templates['model.html']({name, markers, value}); const html = Handlebars.templates['model.html']({name, markers, value});
container.append($(html)); container.append($(html));

View File

@ -275,12 +275,15 @@
<script src="/mixed/lib/jquery.min.js"></script> <script src="/mixed/lib/jquery.min.js"></script>
<script src="/mixed/lib/bootstrap/js/bootstrap.min.js"></script> <script src="/mixed/lib/bootstrap/js/bootstrap.min.js"></script>
<script src="/mixed/lib/handlebars.min.js"></script> <script src="/mixed/lib/handlebars.min.js"></script>
<script src="/bg/js/templates.js"></script>
<script src="/bg/js/handlebars.js"></script> <script src="/bg/js/api.js"></script>
<script src="/bg/js/dictionary.js"></script> <script src="/bg/js/dictionary.js"></script>
<script src="/bg/js/handlebars.js"></script>
<script src="/bg/js/options.js"></script>
<script src="/bg/js/templates.js"></script>
<script src="/mixed/js/japanese.js"></script> <script src="/mixed/js/japanese.js"></script>
<script src="/mixed/js/request.js"></script> <script src="/mixed/js/request.js"></script>
<script src="/bg/js/options.js"></script>
<script src="/bg/js/settings.js"></script> <script src="/bg/js/settings.js"></script>
</body> </body>
</html> </html>