From dd4b2f73656d652bc56be784a27e7378ab969ac6 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 5 May 2016 18:55:43 -0700 Subject: Cleanup --- ext/fg/js/api.js | 4 ++-- ext/fg/js/client.js | 29 +++++++++++++++-------------- ext/fg/js/frame.js | 12 +++++++----- 3 files changed, 24 insertions(+), 21 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index c65b1702..97c14640 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -17,8 +17,8 @@ */ -function sendMessage(action, data, callback) { - chrome.runtime.sendMessage({action: action, data: data}, callback); +function sendMessage(action, params, callback) { + chrome.runtime.sendMessage({action: action, params: params}, callback); } function findTerm(text, callback) { diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js index a60bc7ff..12b04066 100644 --- a/ext/fg/js/client.js +++ b/ext/fg/js/client.js @@ -80,17 +80,13 @@ class Client { } onFrameMessage(e) { - const callback = (data) => { - e.source.postMessage(data, e.origin); - }; - - const {action, data} = e.data, handlers = { - addNote: ({mode, index}) => this.addNote(mode, index, callback), - displayKanji: this.displayKanji + const {action, params} = e.data, handlers = { + addNote: ({mode, index}) => this.actionAddNote(mode, index, (data) => e.source.postMessage(data, e.origin)), + displayKanji: this.actionDisplayKanji }; if (handlers.hasOwnProperty(action)) { - handlers[action].call(this, data); + handlers[action].call(this, params); } } @@ -123,12 +119,17 @@ class Client { }); } - addNote(mode, index, callback) { - callback({action: 'disableAction', data: {mode: mode, index: index}}); - // this.callAnkiApi('addNote', {mode: mode, definition: this.results[index]}); + actionAddNote(mode, index, callback) { + callback({ + action: 'disableAction', + params: { + mode: mode, + index: index + } + }); } - displayKanji(kanji) { + actionDisplayKanji(kanji) { findKanji(kanji, (results) => { renderText( {defs: results, root: this.fgRoot, options: this.options}, @@ -172,7 +173,7 @@ class Client { this.options = opts; } - callAnkiApi(action, data, callback) { + callAnkiApi(action, params, callback) { if (!this.options.enableAnkiConnect) { callback(null); return; @@ -192,7 +193,7 @@ class Client { this.xhr.open('POST', 'http://127.0.0.1:8888'); this.xhr.withCredentials = true; 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})); } } diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js index 4d126b94..80fa099a 100644 --- a/ext/fg/js/frame.js +++ b/ext/fg/js/frame.js @@ -21,7 +21,7 @@ function registerKanjiLinks() { for (const link of [].slice.call(document.getElementsByClassName('kanji-link'))) { link.addEventListener('click', (e) => { 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) => { e.preventDefault(); 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) { - const {action, data} = e.data, handlers = { - 'disableAction': ({mode, index}) => { + const {action, params} = e.data, handlers = { + disableAction: ({mode, index}) => { const matches = document.querySelectorAll(`.action-link[data-index="${index}"][data-mode="${mode}"]`); matches[0].classList.add('disabled'); } }; - handlers[action](data); + if (handlers.hasOwnProperty(action)) { + handlers[action](params); + } } document.addEventListener('DOMContentLoaded', onDomContentLoaded, false); -- cgit v1.2.3