mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-11 10:09:59 +00:00
Restructure shim/bridge initialization steps
This commit is contained in:
@@ -1,12 +1,37 @@
|
||||
"use strict";
|
||||
|
||||
document.addEventListener("__castMessageResponse", ev => {
|
||||
browser.runtime.sendMessage(ev.detail);
|
||||
})
|
||||
|
||||
browser.runtime.onMessage.addListener(message => {
|
||||
const event = new CustomEvent("__castMessage", {
|
||||
detail: JSON.stringify(message)
|
||||
});
|
||||
document.dispatchEvent(event);
|
||||
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 => {
|
||||
if (ev.detail.destination === "popup") {
|
||||
if (popupPort) {
|
||||
popupPort.postMessage(ev.detail);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
backgroundPort.postMessage(ev.detail);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user