diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/js/api.js | 35 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 10 | ||||
-rw-r--r-- | ext/bg/js/settings.js | 47 | ||||
-rw-r--r-- | ext/bg/settings.html | 9 |
4 files changed, 71 insertions, 30 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 3db0558b..6d6ec2ea 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -92,7 +92,8 @@ function utilCommandDispatch(command) { } 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}; let fields = []; @@ -137,7 +138,8 @@ async function apiOptionsSet(options) { // 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 // 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) { chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'}); @@ -150,9 +152,9 @@ async function apiOptionsSet(options) { } if (options.anki.enable) { - Backend.instance().anki = new AnkiConnect(options.anki.server); + yomichan.anki = new AnkiConnect(options.anki.server); } else { - Backend.instance().anki = new AnkiNull(); + yomichan.anki = new AnkiNull(); } chrome.tabs.query({}, tabs => { @@ -163,12 +165,14 @@ async function apiOptionsSet(options) { } async function apiOptionsGet() { - return Backend.instance().options; + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + return yomichan.options; } async function apiTermsFind(text) { - const options = Backend.instance().options; - const translator = Backend.instance().translator; + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + const options = yomichan.options; + const translator = yomichan.translator; const searcher = options.general.groupResults ? translator.findTermsGrouped.bind(translator) : @@ -187,14 +191,17 @@ async function apiTermsFind(text) { } async function apiKanjiFind(text) { - const options = Backend.instance().options; - const definitions = await Backend.instance().translator.findKanji(text, dictEnabledSet(options)); + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + const options = yomichan.options; + const definitions = await yomichan.translator.findKanji(text, dictEnabledSet(options)); return definitions.slice(0, options.general.maxResults); } async function apiDefinitionAdd(definition, mode) { + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + if (mode !== 'kanji') { - const options = Backend.instance().options; + const options = yomichan.options; await audioInject( definition, 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) { @@ -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 = []; for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { const state = {}; @@ -228,7 +236,8 @@ async function apiDefinitionsAddable(definitions, modes) { } 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) { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 0615ffae..4fabe4b2 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -17,7 +17,7 @@ */ -class Backend { +window.yomichanBackend = new class { constructor() { handlebarsRegister(); @@ -36,10 +36,4 @@ class Backend { } }); } - - static instance() { - return chrome.extension.getBackgroundPage().yomichanBackend; - } -} - -window.yomichanBackend = new Backend(); +}; diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 3cd64fbd..09e92249 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -18,6 +18,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 */ @@ -217,7 +252,7 @@ async function dictionaryGroupsPopulate(options) { const dictGroups = $('#dict-groups').empty(); const dictWarning = $('#dict-warning').hide(); - const dictRows = await instDb().getDictionaries(); + const dictRows = await utilDatabaseGetDictionaries(); if (dictRows.length === 0) { dictWarning.show(); } @@ -253,7 +288,7 @@ async function onDictionaryPurge(e) {(async () => { dictionaryErrorShow(); dictionarySpinnerShow(true); - await instDb().purge(); + await utilDatabasePurge(); const options = await optionsLoad(); options.dictionaries = {}; await optionsSave(options); @@ -283,7 +318,7 @@ function onDictionaryImport(e) {(async () => { setProgress(0.0); 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}; await optionsSave(options); @@ -344,12 +379,12 @@ async function ankiDeckAndModelPopulate(options) { $('#anki-terms-deck').val(options.anki.terms.deck); $('#anki-kanji-deck').val(options.anki.kanji.deck); - const deckNames = await instAnki().getDeckNames(); + const deckNames = await utilAnkiGetDeckNames(); const ankiDeck = $('.anki-deck'); ankiDeck.find('option').remove(); deckNames.sort().forEach(name => ankiDeck.append($('<option/>', {value: name, text: name}))); - const modelNames = await instAnki().getModelNames(); + const modelNames = await utilAnkiGetModelNames(); const ankiModel = $('.anki-model'); ankiModel.find('option').remove(); modelNames.sort().forEach(name => ankiModel.append($('<option/>', {value: name, text: name}))); @@ -398,7 +433,7 @@ async function ankiFieldsPopulate(element, options) { ] }[tabId] || {}; - for (const name of await instAnki().getModelFieldNames(modelName)) { + for (const name of await utilAnkiGetModelFieldNames(modelName)) { const value = options.anki[tabId].fields[name] || ''; const html = Handlebars.templates['model.html']({name, markers, value}); container.append($(html)); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 2d0b4b4c..03a9870b 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -275,12 +275,15 @@ <script src="/mixed/lib/jquery.min.js"></script> <script src="/mixed/lib/bootstrap/js/bootstrap.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/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/request.js"></script> - <script src="/bg/js/options.js"></script> + <script src="/bg/js/settings.js"></script> </body> </html> |