More cleanup
This commit is contained in:
parent
22cbafb7b7
commit
809e4f855f
@ -50,9 +50,9 @@ class Yomichan {
|
||||
|
||||
onMessage(request, sender, callback) {
|
||||
const {action, params} = request, handlers = {
|
||||
canAddNotes: ({definitions}) => this.ankiInvoke('canAddNotes', definitions, callback),
|
||||
findKanji: ({text}) => callback(this.translator.findKanji(text)),
|
||||
findTerm: ({text}) => callback(this.translator.findTerm(text)),
|
||||
canAddNotes: (definitions) => this.ankiInvoke('canAddNotes', definitions, callback),
|
||||
findKanji: (text) => callback(this.translator.findKanji(text)),
|
||||
findTerm: (text) => callback(this.translator.findTerm(text)),
|
||||
getOptions: () => callback(this.options),
|
||||
getState: () => callback(this.state),
|
||||
renderText: ({data, template}) => callback(Handlebars.templates[template](data))
|
||||
|
@ -21,30 +21,30 @@
|
||||
// Background APIs
|
||||
//
|
||||
|
||||
function sendMessage(action, params, callback) {
|
||||
function bgSendMessage(action, params, callback) {
|
||||
chrome.runtime.sendMessage({action: action, params: params}, callback);
|
||||
}
|
||||
|
||||
function findTerm(text, callback) {
|
||||
sendMessage('findTerm', {text: text}, callback);
|
||||
function bgFindTerm(text, callback) {
|
||||
bgSendMessage('findTerm', text, callback);
|
||||
}
|
||||
|
||||
function findKanji(text, callback) {
|
||||
sendMessage('findKanji', {text: text}, callback);
|
||||
function bgFindKanji(text, callback) {
|
||||
bgSendMessage('findKanji', text, callback);
|
||||
}
|
||||
|
||||
function renderText(data, template, callback) {
|
||||
sendMessage('renderText', {data: data, template: template}, callback);
|
||||
function bgRenderText(data, template, callback) {
|
||||
bgSendMessage('renderText', {data: data, template: template}, callback);
|
||||
}
|
||||
|
||||
function getOptions(callback) {
|
||||
sendMessage('getOptions', null, callback);
|
||||
function bgGetOptions(callback) {
|
||||
bgSendMessage('getOptions', null, callback);
|
||||
}
|
||||
|
||||
function getState(callback) {
|
||||
sendMessage('getState', null, callback);
|
||||
function bgGetState(callback) {
|
||||
bgSendMessage('getState', null, callback);
|
||||
}
|
||||
|
||||
function canAddNotes(definitions, callback) {
|
||||
sendMessage('canAddNotes', {definitions: definitions}, callback);
|
||||
function bgCanAddNotes(definitions, callback) {
|
||||
bgSendMessage('canAddNotes', definitions, callback);
|
||||
}
|
||||
|
@ -37,9 +37,9 @@ class Client {
|
||||
window.addEventListener('scroll', (e) => this.hidePopup());
|
||||
window.addEventListener('resize', (e) => this.hidePopup());
|
||||
|
||||
getOptions((opts) => {
|
||||
bgGetOptions((opts) => {
|
||||
this.setOptions(opts);
|
||||
getState((state) => this.setEnabled(state === 'enabled'));
|
||||
bgGetState((state) => this.setEnabled(state === 'enabled'));
|
||||
});
|
||||
}
|
||||
|
||||
@ -101,19 +101,21 @@ class Client {
|
||||
}
|
||||
|
||||
range.setLength(this.options.scanLength);
|
||||
findTerm(range.text(), ({definitions, length}) => {
|
||||
bgFindTerm(range.text(), ({definitions, length}) => {
|
||||
if (length === 0) {
|
||||
this.hidePopup();
|
||||
} else {
|
||||
range.setLength(length);
|
||||
renderText(
|
||||
bgRenderText(
|
||||
{defs: definitions, root: this.fgRoot, options: this.options},
|
||||
'term-list.html',
|
||||
(content) => {
|
||||
this.definitions = definitions;
|
||||
this.showPopup(range, content);
|
||||
canAddNotes(definitions, (states) => {
|
||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index: index, state: state}));
|
||||
bgCanAddNotes(definitions, (states) => {
|
||||
if (states !== null) {
|
||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index: index, state: state}));
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
@ -129,8 +131,8 @@ class Client {
|
||||
}
|
||||
|
||||
actionDisplayKanji(kanji) {
|
||||
findKanji(kanji, (definitions) => {
|
||||
renderText(
|
||||
bgFindKanji(kanji, (definitions) => {
|
||||
bgRenderText(
|
||||
{defs: definitions, root: this.fgRoot, options: this.options},
|
||||
'kanji-list.html',
|
||||
(content) => {
|
||||
|
Loading…
Reference in New Issue
Block a user