beaverjs
Version:
An abstraction around WebExtension messaging
31 lines • 1.11 kB
JavaScript
import { BaseEventHandler } from './BaseEventHandler';
import { Destination } from './destination';
import { listen } from './listeners';
export class ContextEventHandler extends BaseEventHandler {
emitBackground(key, data) {
return this.emit(key, data, Destination.Background);
}
emitContent(key, data) {
return this.emit(key, data, Destination.Content);
}
deserialize(serialized) {
return { data: serialized.data, type: serialized.event };
}
sendEvent(serialized, destination) {
destination !== null && destination !== void 0 ? destination : (destination = Destination.Passthrough);
postMessage({ data: serialized, destination }, '*');
}
serialize(type, data) {
return { event: type, data };
}
setup() {
listen({
thisActor: 'context',
listenFrom: new Set(['content']),
onMessage: (msg, sender) => {
this.handleSerialized(msg.data, sender);
},
});
}
}
//# sourceMappingURL=ContextEventHandler.js.map