Improved messaging
This commit is contained in:
parent
ac012b26b3
commit
059b2eb4f2
@ -17,6 +17,10 @@
|
||||
*/
|
||||
|
||||
|
||||
function findTerm(text, callback) {
|
||||
window.chrome.runtime.sendMessage({action: 'define', text: text}, callback);
|
||||
function sendMessage(action, data, callback) {
|
||||
window.chrome.runtime.sendMessage({action: action, data: data}, callback);
|
||||
}
|
||||
|
||||
function findTerm(text, callback) {
|
||||
sendMessage('findTerm', {term: text}, callback);
|
||||
}
|
||||
|
@ -53,4 +53,7 @@ function onMouseDown(e) {
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener('mousedown', onMouseDown, false);
|
||||
|
||||
(() => {
|
||||
window.addEventListener('mousedown', onMouseDown, false);
|
||||
})();
|
||||
|
@ -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)
|
||||
);
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user