2016-03-26 21:16:21 +00: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class Yomichan {
|
|
|
|
constructor() {
|
2016-04-04 05:22:38 +00:00
|
|
|
Handlebars.partials = Handlebars.templates;
|
2016-09-13 05:14:58 +00:00
|
|
|
Handlebars.registerHelper('kanjiLinks', kanjiLinks);
|
2016-03-26 21:16:21 +00:00
|
|
|
|
2016-03-28 03:00:41 +00:00
|
|
|
this.translator = new Translator();
|
2016-10-16 06:23:40 +00:00
|
|
|
this.anki = new AnkiNull();
|
2016-09-29 03:14:21 +00:00
|
|
|
this.options = null;
|
2016-05-06 01:55:43 +00:00
|
|
|
this.setState('disabled');
|
2016-03-26 21:16:21 +00:00
|
|
|
|
2016-06-04 04:17:55 +00:00
|
|
|
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
|
|
|
|
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
|
2016-11-15 05:02:04 +00:00
|
|
|
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
|
2016-06-04 04:17:55 +00:00
|
|
|
|
2016-09-13 20:38:37 +00:00
|
|
|
loadOptions().then(opts => {
|
2016-05-06 01:55:43 +00:00
|
|
|
this.setOptions(opts);
|
2016-05-08 01:24:31 +00:00
|
|
|
if (this.options.activateOnStartup) {
|
2016-05-06 01:55:43 +00:00
|
|
|
this.setState('loading');
|
2016-04-07 04:04:38 +00:00
|
|
|
}
|
2016-04-07 03:48:36 +00:00
|
|
|
});
|
2016-03-26 21:16:21 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 05:02:04 +00:00
|
|
|
onInstalled(details) {
|
|
|
|
if (details.reason === 'install') {
|
|
|
|
chrome.tabs.create({url: chrome.extension.getURL('bg/guide.html')});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:16:21 +00:00
|
|
|
onMessage(request, sender, callback) {
|
2016-05-22 01:07:57 +00:00
|
|
|
const {action, params} = request, method = this['api_' + action];
|
2016-03-26 21:16:21 +00:00
|
|
|
|
2016-05-22 00:44:02 +00:00
|
|
|
if (typeof(method) === 'function') {
|
2016-05-22 01:07:57 +00:00
|
|
|
params.callback = callback;
|
|
|
|
method.call(this, params);
|
2016-05-22 00:44:02 +00:00
|
|
|
}
|
2016-05-22 00:11:17 +00:00
|
|
|
|
2016-05-22 00:44:02 +00:00
|
|
|
return true;
|
2016-05-22 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 06:02:55 +00:00
|
|
|
onBrowserAction() {
|
2016-03-26 21:16:21 +00:00
|
|
|
switch (this.state) {
|
|
|
|
case 'disabled':
|
2016-05-06 01:55:43 +00:00
|
|
|
this.setState('loading');
|
2016-03-26 21:16:21 +00:00
|
|
|
break;
|
|
|
|
case 'enabled':
|
2016-05-06 01:55:43 +00:00
|
|
|
this.setState('disabled');
|
2016-03-26 21:16:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 01:55:43 +00:00
|
|
|
setState(state) {
|
2016-04-06 05:18:55 +00:00
|
|
|
if (this.state === state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:16:21 +00:00
|
|
|
this.state = state;
|
2016-03-28 00:04:49 +00:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 'disabled':
|
2016-05-21 22:40:08 +00:00
|
|
|
chrome.browserAction.setBadgeText({text: 'off'});
|
2016-03-28 00:04:49 +00:00
|
|
|
break;
|
|
|
|
case 'enabled':
|
2016-05-21 22:40:08 +00:00
|
|
|
chrome.browserAction.setBadgeText({text: ''});
|
2016-03-28 00:04:49 +00:00
|
|
|
break;
|
|
|
|
case 'loading':
|
|
|
|
chrome.browserAction.setBadgeText({text: '...'});
|
2016-11-13 19:58:23 +00:00
|
|
|
this.translator.prepare().then(this.setState('enabled'));
|
2016-03-28 00:04:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-07-23 22:14:13 +00:00
|
|
|
this.tabInvokeAll('setEnabled', this.state === 'enabled');
|
2016-04-06 05:18:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 01:55:43 +00:00
|
|
|
setOptions(options) {
|
2016-04-07 04:02:31 +00:00
|
|
|
this.options = options;
|
2016-10-16 02:05:53 +00:00
|
|
|
|
|
|
|
switch (options.ankiMethod) {
|
|
|
|
case 'ankiweb':
|
|
|
|
this.anki = new AnkiWeb(options.ankiUsername, options.ankiPassword);
|
|
|
|
break;
|
|
|
|
case 'ankiconnect':
|
|
|
|
this.anki = new AnkiConnect();
|
|
|
|
break;
|
|
|
|
default:
|
2016-10-16 06:23:40 +00:00
|
|
|
this.anki = new AnkiNull();
|
2016-10-16 02:05:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-07-23 22:14:13 +00:00
|
|
|
this.tabInvokeAll('setOptions', this.options);
|
2016-04-06 05:18:55 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 22:14:13 +00:00
|
|
|
tabInvokeAll(action, params) {
|
2016-09-11 19:29:18 +00:00
|
|
|
chrome.tabs.query({}, tabs => {
|
2016-08-14 19:08:06 +00:00
|
|
|
for (const tab of tabs) {
|
2016-07-23 22:14:13 +00:00
|
|
|
this.tabInvoke(tab.id, action, params);
|
2016-05-29 20:26:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-23 22:14:13 +00:00
|
|
|
tabInvoke(tabId, action, params) {
|
|
|
|
chrome.tabs.sendMessage(tabId, {action, params}, () => null);
|
|
|
|
}
|
|
|
|
|
2016-05-29 19:46:37 +00:00
|
|
|
formatNote(definition, mode) {
|
2016-06-15 03:43:43 +00:00
|
|
|
const note = {fields: {}, tags: this.options.ankiCardTags};
|
2016-05-29 19:46:37 +00:00
|
|
|
|
|
|
|
let fields = [];
|
|
|
|
if (mode === 'kanji') {
|
2016-08-14 19:08:06 +00:00
|
|
|
fields = this.options.ankiKanjiFields;
|
|
|
|
note.deckName = this.options.ankiKanjiDeck;
|
2016-05-29 19:46:37 +00:00
|
|
|
note.modelName = this.options.ankiKanjiModel;
|
|
|
|
} else {
|
2016-08-14 19:08:06 +00:00
|
|
|
fields = this.options.ankiTermFields;
|
|
|
|
note.deckName = this.options.ankiTermDeck;
|
2016-08-10 15:44:52 +00:00
|
|
|
note.modelName = this.options.ankiTermModel;
|
2016-07-17 17:35:16 +00:00
|
|
|
|
|
|
|
const audio = {
|
2016-10-19 15:52:22 +00:00
|
|
|
kanji: definition.expression,
|
|
|
|
kana: definition.reading,
|
2016-07-17 17:35:16 +00:00
|
|
|
fields: []
|
|
|
|
};
|
|
|
|
|
2016-08-14 19:08:06 +00:00
|
|
|
for (const name in fields) {
|
2016-09-11 19:29:18 +00:00
|
|
|
if (fields[name].includes('{audio}')) {
|
2016-07-17 17:35:16 +00:00
|
|
|
audio.fields.push(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio.fields.length > 0) {
|
|
|
|
note.audio = audio;
|
|
|
|
}
|
2016-05-29 19:46:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 04:48:40 +00:00
|
|
|
for (const name in fields) {
|
2017-01-08 22:08:36 +00:00
|
|
|
note.fields[name] = formatField(fields[name], definition, mode);
|
2016-05-29 19:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2016-09-29 03:14:21 +00:00
|
|
|
api_getEnabled({callback}) {
|
2016-10-20 04:21:00 +00:00
|
|
|
callback({result: this.state === 'enabled'});
|
2016-09-29 03:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api_getOptions({callback}) {
|
2016-10-22 03:08:16 +00:00
|
|
|
promiseCallback(loadOptions(), callback);
|
2016-09-29 03:14:21 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 02:50:55 +00:00
|
|
|
api_findKanji({text, callback}) {
|
2016-11-13 03:29:30 +00:00
|
|
|
const dictionaries = [];
|
|
|
|
for (const title in this.options.dictionaries) {
|
|
|
|
if (this.options.dictionaries[title].enableKanji) {
|
|
|
|
dictionaries.push(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
promiseCallback(
|
|
|
|
this.translator.findKanji(text, dictionaries),
|
|
|
|
callback
|
|
|
|
);
|
2016-10-11 02:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api_findTerm({text, callback}) {
|
2016-11-13 03:29:30 +00:00
|
|
|
const dictionaries = [];
|
|
|
|
for (const title in this.options.dictionaries) {
|
|
|
|
if (this.options.dictionaries[title].enableTerms) {
|
|
|
|
dictionaries.push(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
promiseCallback(
|
2017-01-08 23:33:45 +00:00
|
|
|
this.translator.findTerm(
|
|
|
|
text,
|
|
|
|
dictionaries,
|
|
|
|
this.options.enableSoftKatakanaSearch
|
|
|
|
),
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
api_findTermGrouped({text, callback}) {
|
|
|
|
const dictionaries = [];
|
|
|
|
for (const title in this.options.dictionaries) {
|
|
|
|
if (this.options.dictionaries[title].enableTerms) {
|
|
|
|
dictionaries.push(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
promiseCallback(
|
|
|
|
this.translator.findTermGrouped(
|
|
|
|
text,
|
|
|
|
dictionaries,
|
|
|
|
this.options.enableSoftKatakanaSearch
|
|
|
|
),
|
2016-11-13 03:29:30 +00:00
|
|
|
callback
|
|
|
|
);
|
2016-10-11 02:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api_renderText({template, data, callback}) {
|
2016-10-20 04:21:00 +00:00
|
|
|
callback({result: Handlebars.templates[template](data)});
|
2016-10-11 02:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:26:09 +00:00
|
|
|
api_addDefinition({definition, mode, callback}) {
|
2016-10-16 06:23:40 +00:00
|
|
|
const note = this.formatNote(definition, mode);
|
2016-10-22 03:08:16 +00:00
|
|
|
promiseCallback(this.anki.addNote(note), callback);
|
2016-05-22 00:44:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:26:09 +00:00
|
|
|
api_canAddDefinitions({definitions, modes, callback}) {
|
2016-10-16 06:23:40 +00:00
|
|
|
const notes = [];
|
|
|
|
for (const definition of definitions) {
|
|
|
|
for (const mode of modes) {
|
|
|
|
notes.push(this.formatNote(definition, mode));
|
2016-10-16 02:05:53 +00:00
|
|
|
}
|
2016-10-16 06:23:40 +00:00
|
|
|
}
|
2016-05-29 19:46:37 +00:00
|
|
|
|
2016-10-22 03:08:16 +00:00
|
|
|
const promise = this.anki.canAddNotes(notes).then(raw => {
|
2016-10-16 06:23:40 +00:00
|
|
|
const states = [];
|
2016-10-20 04:21:00 +00: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 20:26:09 +00:00
|
|
|
}
|
2016-10-20 04:21:00 +00:00
|
|
|
|
|
|
|
states.push(state);
|
2016-10-16 06:23:40 +00:00
|
|
|
}
|
2016-05-29 19:46:37 +00:00
|
|
|
|
2016-10-22 03:08:16 +00:00
|
|
|
return states;
|
2016-10-16 06:23:40 +00:00
|
|
|
});
|
2016-10-22 03:08:16 +00:00
|
|
|
|
|
|
|
promiseCallback(promise, callback);
|
2016-05-22 00:44:02 +00:00
|
|
|
}
|
2016-03-26 21:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.yomichan = new Yomichan();
|