Change how getUrl message is handled
This commit is contained in:
parent
cab2a39981
commit
e14bd75a4f
@ -231,15 +231,29 @@ class EventDispatcher {
|
|||||||
* Default message handlers
|
* Default message handlers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(() => {
|
const yomichan = (() => {
|
||||||
function onMessage({action}, sender, callback) {
|
class Yomichan {
|
||||||
switch (action) {
|
constructor() {
|
||||||
case 'getUrl':
|
this._messageHandlers = new Map([
|
||||||
callback({url: window.location.href});
|
['getUrl', this._onMessageGetUrl.bind(this)]
|
||||||
break;
|
]);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
_onMessageGetUrl() {
|
||||||
|
return {url: window.location.href};
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener(onMessage);
|
return new Yomichan();
|
||||||
})();
|
})();
|
||||||
|
Loading…
Reference in New Issue
Block a user