more cleanup

This commit is contained in:
Alex Yatskov 2017-08-05 19:23:17 -07:00
parent dfecef1f23
commit 7e635d6382
6 changed files with 62 additions and 60 deletions

View File

@ -19,6 +19,7 @@
<script src="/bg/js/options.js"></script> <script src="/bg/js/options.js"></script>
<script src="/bg/js/templates.js"></script> <script src="/bg/js/templates.js"></script>
<script src="/bg/js/translator.js"></script> <script src="/bg/js/translator.js"></script>
<script src="/bg/js/util.js"></script>
<script src="/mixed/js/audio.js"></script> <script src="/mixed/js/audio.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>

View File

@ -17,34 +17,21 @@
*/ */
/*
* Backend
*/
function backend() {
return chrome.extension.getBackgroundPage().yomichan_backend;
}
/*
* 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.
backend().onOptionsUpdated(JSON.parse(JSON.stringify(options))); utilBackend().onOptionsUpdated(JSON.parse(JSON.stringify(options)));
} }
async function apiOptionsGet() { async function apiOptionsGet() {
return backend().options; return utilBackend().options;
} }
async function apiTermsFind(text) { async function apiTermsFind(text) {
const options = backend().options; const options = utilBackend().options;
const translator = backend().translator; const translator = utilBackend().translator;
const searcher = options.general.groupResults ? const searcher = options.general.groupResults ?
translator.findTermsGrouped.bind(translator) : translator.findTermsGrouped.bind(translator) :
@ -63,13 +50,13 @@ async function apiTermsFind(text) {
} }
async function apiKanjiFind(text) { async function apiKanjiFind(text) {
const options = backend().options; const options = utilBackend().options;
const definitions = await backend().translator.findKanji(text, dictEnabledSet(options)); const definitions = await utilBackend().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 options = backend().options; const options = utilBackend().options;
if (mode !== 'kanji') { if (mode !== 'kanji') {
await audioInject( 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) { async function apiDefinitionsAddable(definitions, modes) {
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, 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 = []; 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 = {};
@ -105,7 +92,7 @@ async function apiDefinitionsAddable(definitions, modes) {
} }
async function apiNoteView(noteId) { async function apiNoteView(noteId) {
return backend().anki.guiBrowse(`nid:${noteId}`); return utilBackend().anki.guiBrowse(`nid:${noteId}`);
} }
async function apiTemplateRender(template, data) { async function apiTemplateRender(template, data) {
@ -127,7 +114,7 @@ async function apiCommandExec(command) {
}, },
toggle: async () => { toggle: async () => {
const options = backend().options; const options = utilBackend().options;
options.general.enable = !options.general.enable; options.general.enable = !options.general.enable;
await optionsSave(options); await optionsSave(options);
await apiOptionsSet(options); await apiOptionsSet(options);

View File

@ -50,9 +50,9 @@ window.yomichan_backend = new class {
} }
if (options.anki.enable) { if (options.anki.enable) {
backend().anki = new AnkiConnect(options.anki.server); this.anki = new AnkiConnect(options.anki.server);
} else { } else {
backend().anki = new AnkiNull(); this.anki = new AnkiNull();
} }
chrome.tabs.query({}, tabs => { chrome.tabs.query({}, tabs => {

View File

@ -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() { async function formRead() {
const optionsOld = await optionsLoad(); const optionsOld = await optionsLoad();
const optionsNew = $.extend(true, {}, optionsOld); const optionsNew = $.extend(true, {}, optionsOld);

46
ext/bg/js/util.js Normal file
View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}

View File

@ -283,6 +283,7 @@
<script src="/bg/js/handlebars.js"></script> <script src="/bg/js/handlebars.js"></script>
<script src="/bg/js/options.js"></script> <script src="/bg/js/options.js"></script>
<script src="/bg/js/templates.js"></script> <script src="/bg/js/templates.js"></script>
<script src="/bg/js/util.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>