interOriginFrame.js (1387B)
1 let parentLocation = ""; 2 3 // The first time this gets called in a page, the location of the parent 4 // should be passed in. This will be used as the target origin argument 5 // for the postMessage call for all subsequent calls to postMsg(). 6 function postMsg(message, newParentLocation) { 7 if (newParentLocation) { 8 parentLocation = newParentLocation; 9 } else if (parentLocation == "") { 10 throw new Error("Failed to pass in newParentLocation"); 11 } 12 13 parent.postMessage(message, parentLocation); 14 } 15 16 window.addEventListener("message", onMessageReceived); 17 18 function onMessageReceived(event) { 19 if (event.data == "step") { 20 var performed = false; 21 try { 22 performed = doStep(); 23 } catch (ex) { 24 postMsg("FAILURE: exception threw at " + location + ":\n" + ex); 25 finishTest(); 26 } 27 28 if (performed) { 29 postMsg("perf"); 30 } 31 32 return; 33 } 34 35 if (parent) { 36 postMsg(event.data); 37 } 38 } 39 40 function ok(a, message) { 41 if (!a) { 42 postMsg("FAILURE: " + message); 43 } else { 44 postMsg(message); 45 } 46 } 47 48 function is(a, b, message) { 49 if (a != b) { 50 postMsg("FAILURE: " + message + ", expected " + b + " got " + a); 51 } else { 52 postMsg(message + ", expected " + b + " got " + a); 53 } 54 } 55 56 function todo(a, b, message) { 57 postMsg("TODO: " + message + ", expected " + b + " got " + a); 58 } 59 60 function finishTest() { 61 postMsg("done"); 62 return false; 63 }