This commit is contained in:
Alex Yatskov 2016-05-05 18:55:43 -07:00
parent e74386faa4
commit dd4b2f7365
4 changed files with 40 additions and 37 deletions

View File

@ -36,45 +36,45 @@ class Yomichan {
this.translator = new Translator(); this.translator = new Translator();
this.xhr = null; this.xhr = null;
this.updateState('disabled'); this.setState('disabled');
loadOptions((opts) => { loadOptions((opts) => {
this.updateOptions(opts); this.setOptions(opts);
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
if (this.options.loadOnStartup) { if (this.options.loadOnStartup) {
this.updateState('loading'); this.setState('loading');
} }
}); });
} }
onMessage(request, sender, callback) { onMessage(request, sender, callback) {
const {action, data} = request, handlers = { const {action, params} = request, handlers = {
findKanji: ({text}) => this.findKanji(text, callback), findKanji: ({text}) => this.actionFindKanji(text, callback),
findTerm: ({text}) => this.findTerm(text, callback), findTerm: ({text}) => this.actionFindTerm(text, callback),
getOptions: () => callback(this.options), getOptions: () => callback(this.options),
getState: () => callback(this.state), getState: () => callback(this.state),
renderText: ({data, template}) => callback(Handlebars.templates[template](data)) renderText: ({data, template}) => callback(Handlebars.templates[template](data))
}; };
handlers[action].call(this, data); handlers[action].call(this, params);
return true; return true;
} }
onBrowserAction(tab) { onBrowserAction(tab) {
switch (this.state) { switch (this.state) {
case 'disabled': case 'disabled':
this.updateState('loading'); this.setState('loading');
break; break;
case 'enabled': case 'enabled':
this.updateState('disabled'); this.setState('disabled');
break; break;
} }
} }
updateState(state) { setState(state) {
if (this.state === state) { if (this.state === state) {
return; return;
} }
@ -90,19 +90,19 @@ class Yomichan {
break; break;
case 'loading': case 'loading':
chrome.browserAction.setBadgeText({text: '...'}); chrome.browserAction.setBadgeText({text: '...'});
this.translator.loadData(() => this.updateState('enabled')); this.translator.loadData(() => this.setState('enabled'));
break; break;
} }
Yomichan.notifyChange('state', this.state); Yomichan.notifyChange('state', this.state);
} }
updateOptions(options) { setOptions(options) {
this.options = options; this.options = options;
Yomichan.notifyChange('options', this.options); Yomichan.notifyChange('options', this.options);
} }
findTerm(text, callback) { actionFindTerm(text, callback) {
const results = this.translator.findTerm(text); const results = this.translator.findTerm(text);
this.callAnkiApi('canAddNotes', results.results, (definitions) => { this.callAnkiApi('canAddNotes', results.results, (definitions) => {
if (definitions !== null) { if (definitions !== null) {
@ -113,7 +113,7 @@ class Yomichan {
}); });
} }
findKanji(text, callback) { actionFindKanji(text, callback) {
const results = this.translator.findKanji(text); const results = this.translator.findKanji(text);
this.callAnkiApi('cannAddNotes', results.results, (definitions) => { this.callAnkiApi('cannAddNotes', results.results, (definitions) => {
if (definitions !== null) { if (definitions !== null) {
@ -124,7 +124,7 @@ class Yomichan {
}); });
} }
callAnkiApi(action, data, callback) { callAnkiApi(action, params, callback) {
if (!this.options.enableAnkiConnect) { if (!this.options.enableAnkiConnect) {
callback(null); callback(null);
return; return;
@ -144,7 +144,7 @@ class Yomichan {
this.xhr.open('POST', 'http://127.0.0.1:8888'); this.xhr.open('POST', 'http://127.0.0.1:8888');
this.xhr.withCredentials = true; this.xhr.withCredentials = true;
this.xhr.setRequestHeader('Content-Type', 'text/json'); this.xhr.setRequestHeader('Content-Type', 'text/json');
this.xhr.send(JSON.stringify({action: action, data: data})); this.xhr.send(JSON.stringify({action: action, params: params}));
} }
static notifyChange(name, value) { static notifyChange(name, value) {

View File

@ -17,8 +17,8 @@
*/ */
function sendMessage(action, data, callback) { function sendMessage(action, params, callback) {
chrome.runtime.sendMessage({action: action, data: data}, callback); chrome.runtime.sendMessage({action: action, params: params}, callback);
} }
function findTerm(text, callback) { function findTerm(text, callback) {

View File

@ -80,17 +80,13 @@ class Client {
} }
onFrameMessage(e) { onFrameMessage(e) {
const callback = (data) => { const {action, params} = e.data, handlers = {
e.source.postMessage(data, e.origin); addNote: ({mode, index}) => this.actionAddNote(mode, index, (data) => e.source.postMessage(data, e.origin)),
}; displayKanji: this.actionDisplayKanji
const {action, data} = e.data, handlers = {
addNote: ({mode, index}) => this.addNote(mode, index, callback),
displayKanji: this.displayKanji
}; };
if (handlers.hasOwnProperty(action)) { if (handlers.hasOwnProperty(action)) {
handlers[action].call(this, data); handlers[action].call(this, params);
} }
} }
@ -123,12 +119,17 @@ class Client {
}); });
} }
addNote(mode, index, callback) { actionAddNote(mode, index, callback) {
callback({action: 'disableAction', data: {mode: mode, index: index}}); callback({
// this.callAnkiApi('addNote', {mode: mode, definition: this.results[index]}); action: 'disableAction',
params: {
mode: mode,
index: index
}
});
} }
displayKanji(kanji) { actionDisplayKanji(kanji) {
findKanji(kanji, (results) => { findKanji(kanji, (results) => {
renderText( renderText(
{defs: results, root: this.fgRoot, options: this.options}, {defs: results, root: this.fgRoot, options: this.options},
@ -172,7 +173,7 @@ class Client {
this.options = opts; this.options = opts;
} }
callAnkiApi(action, data, callback) { callAnkiApi(action, params, callback) {
if (!this.options.enableAnkiConnect) { if (!this.options.enableAnkiConnect) {
callback(null); callback(null);
return; return;
@ -192,7 +193,7 @@ class Client {
this.xhr.open('POST', 'http://127.0.0.1:8888'); this.xhr.open('POST', 'http://127.0.0.1:8888');
this.xhr.withCredentials = true; this.xhr.withCredentials = true;
this.xhr.setRequestHeader('Content-Type', 'text/json'); this.xhr.setRequestHeader('Content-Type', 'text/json');
this.xhr.send(JSON.stringify({action: action, data: data})); this.xhr.send(JSON.stringify({action: action, params: params}));
} }
} }

View File

@ -21,7 +21,7 @@ function registerKanjiLinks() {
for (const link of [].slice.call(document.getElementsByClassName('kanji-link'))) { for (const link of [].slice.call(document.getElementsByClassName('kanji-link'))) {
link.addEventListener('click', (e) => { link.addEventListener('click', (e) => {
e.preventDefault(); e.preventDefault();
window.parent.postMessage({action: 'displayKanji', data: e.target.innerHTML}, '*'); window.parent.postMessage({action: 'displayKanji', params: e.target.innerHTML}, '*');
}); });
} }
} }
@ -31,7 +31,7 @@ function registerActionLinks() {
link.addEventListener('click', (e) => { link.addEventListener('click', (e) => {
e.preventDefault(); e.preventDefault();
const ds = e.currentTarget.dataset; const ds = e.currentTarget.dataset;
window.parent.postMessage({action: 'addNote', data: {index: ds.index, mode: ds.mode}}, '*'); window.parent.postMessage({action: 'addNote', params: {index: ds.index, mode: ds.mode}}, '*');
}); });
} }
} }
@ -42,14 +42,16 @@ function onDomContentLoaded() {
} }
function onMessage(e) { function onMessage(e) {
const {action, data} = e.data, handlers = { const {action, params} = e.data, handlers = {
'disableAction': ({mode, index}) => { disableAction: ({mode, index}) => {
const matches = document.querySelectorAll(`.action-link[data-index="${index}"][data-mode="${mode}"]`); const matches = document.querySelectorAll(`.action-link[data-index="${index}"][data-mode="${mode}"]`);
matches[0].classList.add('disabled'); matches[0].classList.add('disabled');
} }
}; };
handlers[action](data); if (handlers.hasOwnProperty(action)) {
handlers[action](params);
}
} }
document.addEventListener('DOMContentLoaded', onDomContentLoaded, false); document.addEventListener('DOMContentLoaded', onDomContentLoaded, false);