pycmd relay example

This commit is contained in:
Luc Mcgrady 2025-10-03 02:14:24 +01:00
parent 69097ebb50
commit b6e07f5780
No known key found for this signature in database
GPG key ID: 4F3D7A0B17CC3D9C
2 changed files with 20 additions and 9 deletions

View file

@ -34,3 +34,8 @@ addEventListener("message", (e) => {
const base = document.createElement("base"); const base = document.createElement("base");
base.href = "/"; base.href = "/";
document.head.appendChild(base); document.head.appendChild(base);
function pycmd(cmd: string) {
window.parent.postMessage({ type: "pycmd", value: cmd }, "*");
}
globalThis.pycmd = pycmd;

View file

@ -19,26 +19,32 @@ export function setupReviewer(iframe: HTMLIFrameElement) {
} }
function onReady() { function onReady() {
// TODO This should probably be a "ready" command now that it is part of the actual reviewer,
// Currently this depends on the reviewer component mounting after the bottom-reviewer which it should but seems hacky.
// Maybe use a counter with a counter.subscribe($counter == 2 then call("ready"))
bridgeCommand("bottomReady"); bridgeCommand("bottomReady");
iframe.contentWindow?.postMessage({ type: "nightMode", value: true }, "*"); iframe.contentWindow?.postMessage({ type: "nightMode", value: true }, "*");
} }
iframe?.addEventListener("load", onReady); iframe?.addEventListener("load", onReady);
/* addEventListener("message", (e) => { addEventListener("message", (e) => {
switch (e.data.type) { switch (e.data.type) {
case "ready": case "pycmd": {
// TODO This should probably be a "ready" command now that it is part of the actual reviewer, const cmd = e.data.value as string;
// Currently this depends on the reviewer component mounting after the bottom-reviewer which it should but seems hacky. if (cmd.startsWith("play:")) {
// Maybe use a counter with a counter.subscribe($counter == 2 then call("ready")) bridgeCommand(e.data.value);
bridgeCommand("bottomReady"); } else {
iframe.contentWindow?.postMessage({ type: "nightMode", value: true }); console.error("pycmd command is either invalid or forbidden:", cmd);
}
break; break;
default: }
default: {
console.warn(`Unknown message type: ${e.data.type}`); console.warn(`Unknown message type: ${e.data.type}`);
break; break;
} }
}); */ }
});
globalThis._showAnswer = updateHtml; globalThis._showAnswer = updateHtml;
globalThis._showQuestion = showQuestion; globalThis._showQuestion = showQuestion;