*Support calling modules from the background page

This commit is contained in:
Derrick Hammer 2022-07-29 19:42:49 -04:00
parent fa97cba286
commit f394776a72
2 changed files with 24 additions and 2 deletions

View File

@ -55,6 +55,20 @@ function handleKernelMessage(event: MessageEvent) {
} }
let data = event.data.data; let data = event.data.data;
if (event.data.method === "kernelBridgeVersion") {
for (let [, port] of Object.entries(openPorts)) {
try {
(port as any).postMessage(event.data);
} catch {}
}
return;
}
if (event.origin !== "http://kernel.skynet") {
return;
}
if (event.data.method === "log") { if (event.data.method === "log") {
if (data.isErr === false) { if (data.isErr === false) {
console.log(data.message); console.log(data.message);
@ -118,6 +132,11 @@ function handleBridgeMessage(
return; return;
} }
if (data.method === "response") {
window.postMessage(data);
return;
}
if (data.method !== "queryUpdate") { if (data.method !== "queryUpdate") {
queries[data.nonce] = (response: any) => { queries[data.nonce] = (response: any) => {
if (portNonce in openPorts) { if (portNonce in openPorts) {

View File

@ -49,14 +49,17 @@ port.onMessage.addListener(handleBackgroundMessage);
// appeared which will need the auth status of the kernel repeated. // appeared which will need the auth status of the kernel repeated.
function handleVersion(data: any) { function handleVersion(data: any) {
// Send a message indicating that the bridge is alive. // Send a message indicating that the bridge is alive.
window.postMessage({ const message = {
nonce: data.nonce, nonce: data.nonce,
method: "response", method: "response",
err: null, err: null,
data: { data: {
version: "v0.2.0", version: "v0.2.0",
}, },
}); };
window.postMessage(message);
port.postMessage(message);
// Wait until the kernel auth status is known, then send a message with // Wait until the kernel auth status is known, then send a message with
// the kernel auth status. // the kernel auth status.