cross-origin-iframe-prerender.html (2825B)
1 <!DOCTYPE html> 2 <script src="/common/utils.js"></script> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/common/get-host-info.sub.js"></script> 6 <script src="/speculation-rules/prerender/resources/utils.js"></script> 7 <script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script> 8 <body> 9 <script> 10 11 async function main() { 12 const iframe_uid = token(); 13 const iFrameChannel = new PrerenderChannel('iframe-channel',iframe_uid); 14 const crossOriginUrl = 15 new URL(`cross-origin-iframe-src-prerender.html?uid=${iframe_uid}`, 16 get_host_info()['HTTPS_REMOTE_ORIGIN'] + 17 window.location.pathname); 18 19 // Send the observed events back to the main test page. 20 const testChannel = new PrerenderChannel('test-channel'); 21 22 // Start loading a cross-origin iframe. The iframe messages us with the 23 // value of its document.prerendering, which should be true on load and 24 // then get to false. 25 const crossOriginIframe = document.createElement('iframe'); 26 crossOriginIframe.src = crossOriginUrl.href; 27 document.body.appendChild(crossOriginIframe); 28 // Wait iframe load to confirm prerenderingchange listener registration. 29 await new Promise((resolve, reject) => { 30 //window.addEventListener('message', (e) => { 31 iFrameChannel.addEventListener('message', (e) => { 32 if (e.data == 'onload') 33 resolve(); 34 else 35 reject('bad message: ' + e.data); 36 }, {once: true}); 37 }).catch( (error)=> { 38 testChannel.postMessage(error); 39 }); 40 41 // Async wait finishing prerendering in the iframe and report it. 42 new Promise((resolve, reject) => { 43 iFrameChannel.addEventListener('message', (e) => { 44 if (e.data == 'document.prerendering changes to false from true') 45 resolve(); 46 else 47 reject('bad message: ' + e.data); 48 }, {once:true}); 49 }).then(()=>{ 50 message = 'iframe prerender finished correctly.'; 51 }, (error)=>{ 52 message = error; 53 }).finally(()=>{ 54 iFrameChannel.close(); 55 testChannel.postMessage(message); 56 testChannel.close(); 57 }); 58 59 // Activate this page to activate the iframe too. 60 const prerenderChannel = new PrerenderChannel('prerender-channel'); 61 prerenderChannel.postMessage('readyToActivate'); 62 prerenderChannel.close(); 63 64 new PrerenderChannel('close').addEventListener('message', () => { 65 window.close(); 66 }); 67 } 68 69 // The main test page (cross-origin-iframe-prerender.https.html) loads the 70 // initiator page, then the initiator page will prerender itself with the 71 // `prerendering` parameter. 72 const params = new URLSearchParams(location.search); 73 if (!params.has('prerendering')) { 74 const rule_extras = {'target_hint': getTargetHint()}; 75 loadInitiatorPage(rule_extras); 76 } else { 77 main(); 78 } 79 </script> 80 </body>