file_bug346659.html (2126B)
1 <script> 2 function assert(cond, msg) { 3 console.assert(cond, msg); 4 if (!cond) { 5 (window.opener || window.parent).postMessage({ type: 'error', msg }, '*'); 6 throw new Error(msg); 7 } 8 } 9 10 function assert_if(when, cond, msg) { 11 if (when) assert(cond, msg); 12 } 13 14 addEventListener("load", () => { 15 const params = new URLSearchParams(location.search); 16 const testId = params.get("testId"); 17 const type = params.get("type"); 18 const method = params.get("method") || 'load'; 19 const childHost = params.get("childHost") || location.host; 20 21 assert(type == 'popup' || type == 'iframe', 'type must be popup or iframe'); 22 assert(method == 'write' || method == 'load', 'method must be write or load'); 23 assert(testId != null && !isNaN(testId), 'must provide testId'); 24 assert_if(method == 'write', !params.get("childHost"), 'childHost does not apply for method=write'); 25 26 let childURL = new URL(`bug346659-echoer.html?${testId}`, document.baseURI); 27 childURL.host = childHost; 28 29 if (method == 'write') { 30 childURL = new URL('about:blank'); 31 } 32 33 let childWindow; 34 if (type == 'popup') { 35 childWindow = window.open(childURL); 36 } else if (type == 'iframe') { 37 const iframe = document.createElement("iframe"); 38 iframe.src = childURL; 39 document.body.append(iframe); 40 childWindow = iframe.contentWindow; 41 } 42 childWindow.x = testId; 43 44 if (method == 'write') { 45 try { 46 childWindow.document.write(` 47 <script> 48 const testNum = decodeURIComponent(window.location.search.substring(1)); 49 (window.opener || window.parent).postMessage("${testId} - " + window.x, "*"); 50 window.close(); 51 </scr`+`ipt> 52 `) 53 } catch (e) { 54 if (e.name != "SecurityError" || e.code != 18) { 55 assert(false, `Unknown error ${e.name}: ${e.message}`); 56 } 57 // Security error on cross-site write() is fine 58 childWindow.close(); 59 } 60 } 61 }) 62 63 addEventListener("message", ({ data }) => { 64 // forward further to test 65 (window.opener || window.parent).postMessage(data, "*"); 66 // We expect to be done once we received a message 67 window.close(); 68 }) 69 </script>