A fork of https://github.com/ueokande/vim-vixen
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
880 B
33 lines
880 B
6 years ago
|
import { Message, valueOf } from '../shared/messages';
|
||
|
|
||
|
export type WebMessageSender = Window | MessagePort | ServiceWorker | null;
|
||
|
export type WebExtMessageSender = browser.runtime.MessageSender;
|
||
|
|
||
|
export default class MessageListener {
|
||
|
onWebMessage(
|
||
|
listener: (msg: Message, sender: WebMessageSender) => void,
|
||
|
) {
|
||
|
window.addEventListener('message', (event: MessageEvent) => {
|
||
|
let sender = event.source;
|
||
|
let message = null;
|
||
|
try {
|
||
|
message = JSON.parse(event.data);
|
||
|
} catch (e) {
|
||
|
// ignore unexpected message
|
||
|
return;
|
||
|
}
|
||
|
listener(message, sender);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
onBackgroundMessage(
|
||
|
listener: (msg: Message, sender: WebExtMessageSender) => any,
|
||
|
) {
|
||
|
browser.runtime.onMessage.addListener(
|
||
|
(msg: any, sender: WebExtMessageSender) => {
|
||
|
listener(valueOf(msg), sender);
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
}
|