yomichan/ext/bg/js/yomichan.js

192 lines
6.1 KiB
JavaScript
Raw Normal View History

2016-03-26 14:16:21 -07:00
/*
* 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/>.
*/
2017-03-04 17:56:10 -08:00
window.yomichan = new class {
2016-03-26 14:16:21 -07:00
constructor() {
2017-03-04 19:16:19 -08:00
handlebarsRegister();
2016-03-26 14:16:21 -07:00
2016-03-27 20:00:41 -07:00
this.translator = new Translator();
2016-10-15 23:23:40 -07:00
this.anki = new AnkiNull();
2016-09-28 20:14:21 -07:00
this.options = null;
2016-03-26 14:16:21 -07:00
2016-06-03 21:17:55 -07:00
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
2017-03-04 17:56:10 -08:00
if (chrome.runtime.onInstalled) {
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
}
2016-06-03 21:17:55 -07:00
2017-03-04 18:24:57 -08:00
this.translator.prepare().then(optionsLoad).then(this.optionsSet.bind(this));
2016-03-26 14:16:21 -07:00
}
2017-03-04 18:24:57 -08:00
optionsSet(options) {
2016-04-06 21:02:31 -07:00
this.options = options;
2016-10-15 19:05:53 -07:00
2017-03-04 19:16:19 -08:00
let configured = false;
2017-02-25 19:14:44 -08:00
for (const title in options.dictionaries) {
if (options.dictionaries[title].enabled) {
2017-03-04 19:16:19 -08:00
configured = true;
2017-02-25 19:14:44 -08:00
break;
}
}
chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'});
2017-03-04 19:16:19 -08:00
chrome.browserAction.setBadgeText({text: configured ? '' : '!'});
2017-02-25 19:14:44 -08:00
2017-01-13 21:06:33 -08:00
if (options.anki.enable) {
2017-02-05 11:44:59 -08:00
this.anki = new AnkiConnect(this.options.anki.server);
2017-01-12 20:14:05 -08:00
} else {
this.anki = new AnkiNull();
2016-10-15 19:05:53 -07:00
}
2016-05-29 13:26:09 -07:00
}
2017-03-04 18:24:57 -08:00
noteFormat(definition, mode) {
2017-01-13 21:06:33 -08:00
const note = {fields: {}, tags: this.options.anki.tags};
2016-05-29 12:46:37 -07:00
let fields = [];
if (mode === 'kanji') {
2017-01-13 21:06:33 -08:00
fields = this.options.anki.kanji.fields;
note.deckName = this.options.anki.kanji.deck;
note.modelName = this.options.anki.kanji.model;
2016-05-29 12:46:37 -07:00
} else {
2017-01-13 21:06:33 -08:00
fields = this.options.anki.terms.fields;
note.deckName = this.options.anki.terms.deck;
note.modelName = this.options.anki.terms.model;
2016-07-17 10:35:16 -07:00
2017-02-19 10:09:15 -08:00
if (definition.audio) {
const audio = {
url: definition.audio.url,
filename: definition.audio.filename,
skipHash: '7e2c2f954ef6051373ba916f000168dc',
fields: []
};
for (const name in fields) {
if (fields[name].includes('{audio}')) {
audio.fields.push(name);
}
2016-07-17 10:35:16 -07:00
}
2017-02-19 10:09:15 -08:00
if (audio.fields.length > 0) {
note.audio = audio;
}
2016-07-17 10:35:16 -07:00
}
2016-05-29 12:46:37 -07:00
}
2016-08-18 21:48:40 -07:00
for (const name in fields) {
2017-03-02 21:01:49 -08:00
note.fields[name] = dictFieldFormat(
2017-01-21 19:30:01 -08:00
fields[name],
definition,
mode,
2017-01-26 20:22:52 -08:00
this.options
2017-01-21 19:30:01 -08:00
);
2016-05-29 12:46:37 -07:00
}
return note;
}
2017-03-04 11:45:55 -08:00
termsFind(text) {
const searcher = this.options.general.groupResults ?
this.translator.findTermsGrouped.bind(this.translator) :
this.translator.findTerms.bind(this.translator);
return searcher(text, dictEnabled(this.options), this.options.general.softKatakana).then(({definitions, length}) => {
return {length, definitions: definitions.slice(0, this.options.general.maxResults)};
});
}
kanjiFind(text) {
return this.translator.findKanji(text, dictEnabled(this.options)).then(definitions => {
return definitions.slice(0, this.options.general.maxResults);
});
}
2017-03-04 17:56:10 -08:00
definitionAdd(definition, mode) {
2017-03-04 18:24:57 -08:00
const note = this.noteFormat(definition, mode);
2017-03-04 17:56:10 -08:00
return this.anki.addNote(note);
2016-05-21 17:44:02 -07:00
}
2017-03-04 17:56:10 -08:00
definitionsAddable(definitions, modes) {
2016-10-15 23:23:40 -07:00
const notes = [];
for (const definition of definitions) {
for (const mode of modes) {
2017-03-04 18:24:57 -08:00
notes.push(this.noteFormat(definition, mode));
2016-10-15 19:05:53 -07:00
}
2016-10-15 23:23:40 -07:00
}
2016-05-29 12:46:37 -07:00
2017-03-04 17:56:10 -08:00
return this.anki.canAddNotes(notes).then(raw => {
2016-10-15 23:23:40 -07:00
const states = [];
2016-10-19 21:21:00 -07:00
for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) {
const state = {};
for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) {
state[modes[modeOffset]] = raw[resultBase + modeOffset];
2016-05-29 13:26:09 -07:00
}
2016-10-19 21:21:00 -07:00
states.push(state);
2016-10-15 23:23:40 -07:00
}
2016-05-29 12:46:37 -07:00
2016-10-21 20:08:16 -07:00
return states;
2016-10-15 23:23:40 -07:00
});
2017-03-04 17:56:10 -08:00
}
2016-10-21 20:08:16 -07:00
2017-03-04 19:16:19 -08:00
templateRender(template, data) {
return Promise.resolve(handlebarsRender(template, data));
2017-03-04 17:56:10 -08:00
}
2017-03-04 18:24:57 -08:00
onInstalled(details) {
if (details.reason === 'install') {
2017-03-04 19:53:28 -08:00
chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')});
2017-03-04 18:24:57 -08:00
}
2016-05-21 17:44:02 -07:00
}
2016-03-26 14:16:21 -07:00
2017-03-04 18:24:57 -08:00
onMessage(request, sender, callback) {
const handlers = new class {
api_optionsGet({callback}) {
promiseCallback(optionsLoad(), callback);
}
2017-03-04 17:56:10 -08:00
2017-03-04 18:24:57 -08:00
api_kanjiFind({text, callback}) {
promiseCallback(this.kanjiFind(text), callback);
}
2017-03-04 17:56:10 -08:00
2017-03-04 18:24:57 -08:00
api_termsFind({text, callback}) {
promiseCallback(this.termsFind(text), callback);
}
2017-03-04 17:56:10 -08:00
2017-03-04 19:16:19 -08:00
api_templateRender({template, data, callback}) {
promiseCallback(this.templateRender(template, data), callback);
2017-03-04 18:24:57 -08:00
}
2017-03-04 17:56:10 -08:00
2017-03-04 18:24:57 -08:00
api_definitionAdd({definition, mode, callback}) {
promiseCallback(this.definitionAdd(definition, mode), callback);
}
api_definitionsAddable({definitions, modes, callback}) {
promiseCallback(this.definitionsAddable(definitions, modes), callback);
}
};
const {action, params} = request, method = handlers[`api_${action}`];
if (typeof(method) === 'function') {
params.callback = callback;
method.call(this, params);
}
return true;
2017-03-04 17:56:10 -08:00
}
};