script-factory.js (1189B)
1 // This creates a serialized <script> element that is useful for blob/data/srcdoc-style tests. 2 function createScript(sameOrigin, crossOrigin, type="parent", id="", useDispatcher=false) { 3 return `const data = { id: "${id}", 4 opener: !!window.opener, 5 origin: window.origin, 6 sameOriginNoCORPSuccess: false, 7 crossOriginNoCORPFailure: false }; 8 function record(promise, token, expectation) { 9 return promise.then(() => data[token] = expectation, () => data[token] = !expectation); 10 } 11 12 const records = [ 13 record(fetch("${crossOrigin}/common/blank.html", { mode: "no-cors" }), "crossOriginNoCORPFailure", false) 14 ]; 15 16 if ("${sameOrigin}" !== "null") { 17 records.push(record(fetch("${sameOrigin}/common/blank.html", { mode: "no-cors" }), "sameOriginNoCORPSuccess", true)); 18 } 19 20 Promise.all(records).then(() => { 21 // Using BroadcastChannel is useful for blob: URLs, which are always same-origin 22 if ("${type}" === "channel") { 23 const bc = new BroadcastChannel("${id}"); 24 bc.postMessage(data); 25 } else if (${useDispatcher}) { 26 send("${id}", JSON.stringify(data)); 27 } else { 28 window.${type}.postMessage(data, "*"); 29 } 30 });`; 31 }