This commit is contained in:
Alex Yatskov 2017-07-27 23:26:36 -07:00
parent 9ecc7c17f3
commit ba155e7706
3 changed files with 24 additions and 15 deletions

View File

@ -17,6 +17,10 @@
*/ */
/*
* Helpers
*/
function utilMessageDispatch({action, params}, sender, callback) { function utilMessageDispatch({action, params}, sender, callback) {
const forward = (promise, callback) => { const forward = (promise, callback) => {
return promise.then(result => { return promise.then(result => {
@ -92,7 +96,7 @@ function utilCommandDispatch(command) {
} }
function utilNoteFormat(definition, mode) { function utilNoteFormat(definition, mode) {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; const yomichan = chrome.extension.getBackgroundPage().yomichan;
const options = yomichan.options; const options = yomichan.options;
const note = {fields: {}, tags: options.anki.tags}; const note = {fields: {}, tags: options.anki.tags};
let fields = []; let fields = [];
@ -133,12 +137,17 @@ function utilNoteFormat(definition, mode) {
return note; return note;
} }
/*
* 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().yomichanBackend; const yomichan = chrome.extension.getBackgroundPage().yomichan;
yomichan.options = JSON.parse(JSON.stringify(options)); yomichan.options = JSON.parse(JSON.stringify(options));
if (!options.general.enable) { if (!options.general.enable) {
@ -165,12 +174,12 @@ async function apiOptionsSet(options) {
} }
async function apiOptionsGet() { async function apiOptionsGet() {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; const yomichan = chrome.extension.getBackgroundPage().yomichan;
return yomichan.options; return yomichan.options;
} }
async function apiTermsFind(text) { async function apiTermsFind(text) {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; const yomichan = chrome.extension.getBackgroundPage().yomichan;
const options = yomichan.options; const options = yomichan.options;
const translator = yomichan.translator; const translator = yomichan.translator;
@ -191,14 +200,14 @@ async function apiTermsFind(text) {
} }
async function apiKanjiFind(text) { async function apiKanjiFind(text) {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; const yomichan = chrome.extension.getBackgroundPage().yomichan;
const options = yomichan.options; const options = yomichan.options;
const definitions = await yomichan.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().yomichanBackend; const yomichan = chrome.extension.getBackgroundPage().yomichan;
if (mode !== 'kanji') { if (mode !== 'kanji') {
const options = yomichan.options; const options = yomichan.options;
@ -220,7 +229,7 @@ async function apiDefinitionsAddable(definitions, modes) {
} }
} }
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; const yomichan = chrome.extension.getBackgroundPage().yomichan;
const results = await yomichan.anki.canAddNotes(notes); 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) {
@ -236,7 +245,7 @@ async function apiDefinitionsAddable(definitions, modes) {
} }
async function apiNoteView(noteId) { async function apiNoteView(noteId) {
const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; const yomichan = chrome.extension.getBackgroundPage().yomichan;
return yomichan.anki.guiBrowse(`nid:${noteId}`); return yomichan.anki.guiBrowse(`nid:${noteId}`);
} }

View File

@ -17,7 +17,7 @@
*/ */
window.yomichanBackend = new class { window.yomichan = new class {
constructor() { constructor() {
handlebarsRegister(); handlebarsRegister();

View File

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