yomichan/ext/bg/js/yomichan.js

237 lines
7.6 KiB
JavaScript
Raw Normal View History

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/>.
*/
2017-03-05 01:56:10 +00:00
window.yomichan = new class {
2016-03-26 21:16:21 +00:00
constructor() {
2017-03-05 03:16:19 +00:00
handlebarsRegister();
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-03-26 21:16:21 +00:00
2017-07-10 22:20:07 +00:00
this.translator.prepare().then(optionsLoad).then(options => {
this.optionsSet(options);
2017-03-18 20:00:29 +00:00
chrome.commands.onCommand.addListener(this.onCommand.bind(this));
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
2017-07-10 22:20:07 +00:00
2017-04-09 01:01:36 +00:00
if (this.options.general.showGuide) {
chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')});
2017-03-18 20:00:29 +00:00
}
});
2016-03-26 21:16:21 +00:00
}
2017-03-05 02:24:57 +00:00
optionsSet(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.
this.options = JSON.parse(JSON.stringify(options));
2016-10-16 02:05:53 +00:00
if (!this.options.general.enable) {
chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'});
chrome.browserAction.setBadgeText({text: 'off'});
} else if (!dictConfigured(this.options)) {
chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'});
chrome.browserAction.setBadgeText({text: '!'});
} else {
chrome.browserAction.setBadgeText({text: ''});
2017-02-26 03:14:44 +00:00
}
if (this.options.anki.enable) {
2017-02-05 19:44:59 +00:00
this.anki = new AnkiConnect(this.options.anki.server);
2017-01-13 04:14:05 +00:00
} else {
this.anki = new AnkiNull();
2016-10-16 02:05:53 +00:00
}
2016-05-29 20:26:09 +00:00
}
2017-03-05 02:24:57 +00:00
noteFormat(definition, mode) {
2017-03-25 17:46:59 +00:00
const note = {
fields: {},
tags: this.options.anki.tags
};
2016-05-29 19:46:37 +00:00
let fields = [];
if (mode === 'kanji') {
2017-01-14 05:06:33 +00:00
fields = this.options.anki.kanji.fields;
note.deckName = this.options.anki.kanji.deck;
note.modelName = this.options.anki.kanji.model;
2016-05-29 19:46:37 +00:00
} else {
2017-01-14 05:06:33 +00:00
fields = this.options.anki.terms.fields;
note.deckName = this.options.anki.terms.deck;
note.modelName = this.options.anki.terms.model;
2017-03-25 19:01:20 +00: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);
}
}
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-03-03 05:01:49 +00:00
note.fields[name] = dictFieldFormat(
2017-01-22 03:30:01 +00:00
fields[name],
definition,
mode,
2017-01-27 04:22:52 +00:00
this.options
2017-01-22 03:30:01 +00:00
);
2016-05-29 19:46:37 +00:00
}
return note;
}
2017-03-04 19:45:55 +00:00
termsFind(text) {
const searcher = this.options.general.groupResults ?
this.translator.findTermsGrouped.bind(this.translator) :
this.translator.findTerms.bind(this.translator);
2017-05-25 03:29:50 +00:00
return searcher(text, dictEnabledSet(this.options), this.options.scanning.alphanumeric).then(({definitions, length}) => {
2017-03-04 19:45:55 +00:00
return {length, definitions: definitions.slice(0, this.options.general.maxResults)};
});
}
kanjiFind(text) {
return this.translator.findKanji(text, dictEnabledSet(this.options)).then(definitions => {
2017-03-04 19:45:55 +00:00
return definitions.slice(0, this.options.general.maxResults);
});
}
2017-03-05 01:56:10 +00:00
definitionAdd(definition, mode) {
2017-03-25 17:46:59 +00:00
let promise = Promise.resolve();
2017-03-25 19:01:20 +00:00
if (mode !== 'kanji') {
promise = audioInject(definition, this.options.anki.terms.fields, this.options.general.audioSource);
2017-03-25 17:46:59 +00:00
}
return promise.then(() => {
const note = this.noteFormat(definition, mode);
return this.anki.addNote(note);
});
2016-05-22 00:44:02 +00:00
}
2017-03-05 01:56:10 +00:00
definitionsAddable(definitions, modes) {
2016-10-16 06:23:40 +00:00
const notes = [];
for (const definition of definitions) {
for (const mode of modes) {
2017-03-05 02:24:57 +00:00
notes.push(this.noteFormat(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
2017-03-05 01:56:10 +00:00
return 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
});
2017-03-05 01:56:10 +00:00
}
2016-10-22 03:08:16 +00:00
2017-07-02 01:27:49 +00:00
noteView(noteId) {
return this.anki.guiBrowse(`nid:${noteId}`);
}
2017-03-05 03:16:19 +00:00
templateRender(template, data) {
return Promise.resolve(handlebarsRender(template, data));
2017-03-05 01:56:10 +00:00
}
2017-03-18 20:00:29 +00:00
onCommand(command) {
2017-03-25 23:28:15 +00:00
const handlers = {
search: () => {
chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')});
},
help: () => {
chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'});
},
options: () => {
chrome.runtime.openOptionsPage();
},
toggle: () => {
this.options.general.enable = !this.options.general.enable;
optionsSave(this.options).then(() => this.optionsSet(this.options));
}
};
const handler = handlers[command];
if (handler) {
handler();
2017-03-18 20:00:29 +00:00
}
}
2017-03-27 05:42:17 +00:00
onMessage({action, params}, sender, callback) {
const handlers = {
optionsGet: ({callback}) => {
2017-03-05 02:24:57 +00:00
promiseCallback(optionsLoad(), callback);
2017-03-27 05:42:17 +00:00
},
2017-03-05 01:56:10 +00:00
2017-03-27 05:42:17 +00:00
kanjiFind: ({text, callback}) => {
2017-03-05 02:24:57 +00:00
promiseCallback(this.kanjiFind(text), callback);
2017-03-27 05:42:17 +00:00
},
2017-03-05 01:56:10 +00:00
2017-03-27 05:42:17 +00:00
termsFind: ({text, callback}) => {
2017-03-05 02:24:57 +00:00
promiseCallback(this.termsFind(text), callback);
2017-03-27 05:42:17 +00:00
},
2017-03-05 01:56:10 +00:00
2017-03-27 05:42:17 +00:00
templateRender: ({template, data, callback}) => {
2017-03-05 03:16:19 +00:00
promiseCallback(this.templateRender(template, data), callback);
2017-03-27 05:42:17 +00:00
},
2017-03-05 01:56:10 +00:00
2017-03-27 05:42:17 +00:00
definitionAdd: ({definition, mode, callback}) => {
2017-03-05 02:24:57 +00:00
promiseCallback(this.definitionAdd(definition, mode), callback);
2017-03-27 05:42:17 +00:00
},
2017-03-05 02:24:57 +00:00
2017-03-27 05:42:17 +00:00
definitionsAddable: ({definitions, modes, callback}) => {
2017-03-05 02:24:57 +00:00
promiseCallback(this.definitionsAddable(definitions, modes), callback);
2017-07-02 01:27:49 +00:00
},
noteView: ({noteId}) => {
promiseCallback(this.noteView(noteId), callback);
2017-03-05 02:24:57 +00:00
}
};
2017-03-27 05:42:17 +00:00
const handler = handlers[action];
if (handler) {
2017-03-05 02:24:57 +00:00
params.callback = callback;
2017-03-27 05:42:17 +00:00
handler(params);
2017-03-05 02:24:57 +00:00
}
return true;
2017-03-05 01:56:10 +00:00
}
};