mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-10 01:29:58 +00:00
39 lines
918 B
JavaScript
39 lines
918 B
JavaScript
"use strict";
|
|
|
|
const backgroundPort = browser.runtime.connect({
|
|
name: "shim"
|
|
});
|
|
|
|
backgroundPort.onMessage.addListener(message => {
|
|
const event = new CustomEvent("__castMessage", {
|
|
detail: JSON.stringify(message)
|
|
});
|
|
document.dispatchEvent(event);
|
|
});
|
|
|
|
let popupPort;
|
|
browser.runtime.onConnect.addListener(port => {
|
|
if (port.name === "popup") {
|
|
popupPort = port;
|
|
}
|
|
|
|
port.onMessage.addListener(message => {
|
|
const event = new CustomEvent("__castMessage", {
|
|
detail: JSON.stringify(message)
|
|
});
|
|
document.dispatchEvent(event);
|
|
})
|
|
});
|
|
|
|
document.addEventListener("__castMessageResponse", ev => {
|
|
const [ destination ] = ev.detail.subject.split(":/");
|
|
if (destination === "popup") {
|
|
if (popupPort) {
|
|
popupPort.postMessage(ev.detail);
|
|
}
|
|
return;
|
|
}
|
|
|
|
backgroundPort.postMessage(ev.detail);
|
|
});
|