diff --git a/ext/bg/background.html b/ext/bg/background.html index 1e9f3809..40f37b11 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -19,6 +19,7 @@ + diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index bc2693e5..024ba75e 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,34 +17,21 @@ */ -/* - * Backend - */ - -function backend() { - return chrome.extension.getBackgroundPage().yomichan_backend; -} - - -/* - * API - */ - async function apiOptionsSet(options) { // In Firefox, setting options from the options UI somehow carries references // 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().onOptionsUpdated(JSON.parse(JSON.stringify(options))); + utilBackend().onOptionsUpdated(JSON.parse(JSON.stringify(options))); } async function apiOptionsGet() { - return backend().options; + return utilBackend().options; } async function apiTermsFind(text) { - const options = backend().options; - const translator = backend().translator; + const options = utilBackend().options; + const translator = utilBackend().translator; const searcher = options.general.groupResults ? translator.findTermsGrouped.bind(translator) : @@ -63,13 +50,13 @@ async function apiTermsFind(text) { } async function apiKanjiFind(text) { - const options = backend().options; - const definitions = await backend().translator.findKanji(text, dictEnabledSet(options)); + const options = utilBackend().options; + const definitions = await utilBackend().translator.findKanji(text, dictEnabledSet(options)); return definitions.slice(0, options.general.maxResults); } async function apiDefinitionAdd(definition, mode) { - const options = backend().options; + const options = utilBackend().options; if (mode !== 'kanji') { await audioInject( @@ -79,18 +66,18 @@ async function apiDefinitionAdd(definition, mode) { ); } - return backend().anki.addNote(dictNoteFormat(definition, mode, options)); + return utilBackend().anki.addNote(dictNoteFormat(definition, mode, options)); } async function apiDefinitionsAddable(definitions, modes) { const notes = []; for (const definition of definitions) { for (const mode of modes) { - notes.push(dictNoteFormat(definition, mode, backend().options)); + notes.push(dictNoteFormat(definition, mode, utilBackend().options)); } } - const results = await backend().anki.canAddNotes(notes); + const results = await utilBackend().anki.canAddNotes(notes); const states = []; for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { const state = {}; @@ -105,7 +92,7 @@ async function apiDefinitionsAddable(definitions, modes) { } async function apiNoteView(noteId) { - return backend().anki.guiBrowse(`nid:${noteId}`); + return utilBackend().anki.guiBrowse(`nid:${noteId}`); } async function apiTemplateRender(template, data) { @@ -127,7 +114,7 @@ async function apiCommandExec(command) { }, toggle: async () => { - const options = backend().options; + const options = utilBackend().options; options.general.enable = !options.general.enable; await optionsSave(options); await apiOptionsSet(options); diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 1c058433..f61e9742 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -50,9 +50,9 @@ window.yomichan_backend = new class { } if (options.anki.enable) { - backend().anki = new AnkiConnect(options.anki.server); + this.anki = new AnkiConnect(options.anki.server); } else { - backend().anki = new AnkiNull(); + this.anki = new AnkiNull(); } chrome.tabs.query({}, tabs => { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 1edbab01..ce9e14a2 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -17,39 +17,6 @@ */ -/* - * Utilities - */ - -function utilAnkiGetModelNames() { - return backend().anki.getModelNames(); -} - -function utilAnkiGetDeckNames() { - return backend().anki.getDeckNames(); -} - -function utilAnkiGetModelFieldNames(modelName) { - return backend().anki.getModelFieldNames(modelName); -} - -function utilDatabaseGetDictionaries() { - return backend().translator.database.getDictionaries(); -} - -function utilDatabasePurge() { - return backend().translator.database.purge(); -} - -function utilDatabaseImport(data, progress) { - return backend().translator.database.importDictionary(data, progress); -} - - -/* - * General - */ - async function formRead() { const optionsOld = await optionsLoad(); const optionsNew = $.extend(true, {}, optionsOld); diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js new file mode 100644 index 00000000..9dc57950 --- /dev/null +++ b/ext/bg/js/util.js @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function utilBackend() { + return chrome.extension.getBackgroundPage().yomichan_backend; +} + +function utilAnkiGetModelNames() { + return utilBackend().anki.getModelNames(); +} + +function utilAnkiGetDeckNames() { + return utilBackend().anki.getDeckNames(); +} + +function utilAnkiGetModelFieldNames(modelName) { + return utilBackend().anki.getModelFieldNames(modelName); +} + +function utilDatabaseGetDictionaries() { + return utilBackend().translator.database.getDictionaries(); +} + +function utilDatabasePurge() { + return utilBackend().translator.database.purge(); +} + +function utilDatabaseImport(data, progress) { + return utilBackend().translator.database.importDictionary(data, progress); +} diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 7833a21d..719c67a2 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -283,6 +283,7 @@ +