1import { sendWasmModule } from "./helpers.mjs"; 2 3// This is done for the window.open() case. For <iframe>s we use the 4// <iframe> element's load event instead. 5const usp = new URLSearchParams(location.search); 6if (usp.has("send-loaded-message")) { 7 opener.postMessage("loaded", "*"); 8} 9 10window.onmessage = async (e) => { 11 // These could come from the parent, opener, or siblings. 12 if (e.data.constructor === WebAssembly.Module) { 13 e.source.postMessage("WebAssembly.Module message received", "*"); 14 } 15 16 // These could come from the parent or opener. 17 if (e.data.command === "set document.domain") { 18 document.domain = e.data.newDocumentDomain; 19 e.source.postMessage("document.domain is set", "*"); 20 } else if (e.data.command === "get originAgentCluster") { 21 e.source.postMessage(self.originAgentCluster, "*"); 22 } 23 24 // These only come from the parent. 25 if (e.data.command === "send WASM module") { 26 const destinationFrameWindow = parent.frames[e.data.indexIntoParentFrameOfDestination]; 27 const whatHappened = await sendWasmModule(destinationFrameWindow); 28 parent.postMessage(whatHappened, "*"); 29 } else if (e.data.command === "access document") { 30 const destinationFrameWindow = parent.frames[e.data.indexIntoParentFrameOfDestination]; 31 try { 32 destinationFrameWindow.document; 33 parent.postMessage("accessed document successfully", "*"); 34 } catch (e) { 35 parent.postMessage(e.name, "*"); 36 } 37 } else if (e.data.command === "access location.href") { 38 const destinationFrameWindow = parent.frames[e.data.indexIntoParentFrameOfDestination]; 39 try { 40 destinationFrameWindow.location.href; 41 parent.postMessage("accessed location.href successfully", "*"); 42 } catch (e) { 43 parent.postMessage(e.name, "*"); 44 } 45 } else if (e.data.command === "access frameElement") { 46 if (frameElement === null) { 47 parent.postMessage("null", "*"); 48 } else if (frameElement?.constructor?.name === "HTMLIFrameElement") { 49 parent.postMessage("frameElement accessed successfully", "*"); 50 } else { 51 parent.postMessage("something wierd happened", "*"); 52 } 53 } 54 55 // We could also receive e.data === "WebAssembly.Module message received", 56 // but that's handled by await sendWasmModule() above. 57}; 58 59window.onmessageerror = e => { 60 e.source.postMessage("messageerror", "*"); 61}; 62 63document.body.textContent = location.href; 64