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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

37 lines
919 B

import { injectable } from 'tsyringe';
import { Message, valueOf } from '../shared/messages';
export type WebExtMessageSender = browser.runtime.MessageSender;
@injectable()
export default class MessageListener {
onWebMessage(
listener: (msg: Message, sender: Window) => void,
) {
window.addEventListener('message', (event: MessageEvent) => {
let sender = event.source;
if (!(sender instanceof Window)) {
return;
}
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) => {
return listener(valueOf(msg), sender);
},
);
}
}