Change how getUrl message is handled

This commit is contained in:
toasted-nutbread 2019-12-20 13:15:26 -05:00
parent cab2a39981
commit e14bd75a4f

View File

@ -231,15 +231,29 @@ class EventDispatcher {
* Default message handlers
*/
(() => {
function onMessage({action}, sender, callback) {
switch (action) {
case 'getUrl':
callback({url: window.location.href});
break;
const yomichan = (() => {
class Yomichan {
constructor() {
this._messageHandlers = new Map([
['getUrl', this._onMessageGetUrl.bind(this)]
]);
chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
}
_onMessage({action, params}, sender, callback) {
const handler = this._messageHandlers.get(action);
if (typeof handler !== 'function') { return false; }
const result = handler(params, sender);
callback(result);
return false;
}
chrome.runtime.onMessage.addListener(onMessage);
_onMessageGetUrl() {
return {url: window.location.href};
}
}
return new Yomichan();
})();