diff options
Diffstat (limited to 'ext/jp/api.js')
-rw-r--r-- | ext/jp/api.js | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/ext/jp/api.js b/ext/jp/api.js index 3f15056a..95c2f7e3 100644 --- a/ext/jp/api.js +++ b/ext/jp/api.js @@ -17,11 +17,22 @@ */ +function onFindTerm({term}) { + return window.trans.findTerm(term); +} + function onMessage(request, sender, callback) { - switch (request.action.toLowerCase()) { - case 'define': - callback(window.trans.findTerm(request.text)); - break; + const {action, data} = request; + + const handler = { + findTerm: onFindTerm + }[action]; + + if (handler !== null) { + const result = handler(data); + if (callback !== null) { + callback(result); + } } } @@ -33,7 +44,8 @@ function onMessage(request, sender, callback) { kanjidic: 'jp/data/kanjidic.json' }; - window.trans = new Translator(res, function() { - chrome.runtime.onMessage.addListener(onMessage); - }); + window.trans = new Translator( + res, + () => chrome.runtime.onMessage.addListener(onMessage) + ); })(); |