about-blank-replacement-ping-frame.py (1425B)
1 def main(request, response): 2 if b'nested' in request.GET: 3 return ( 4 [(b'Content-Type', b'text/html')], 5 b'failed: nested frame was not intercepted by the service worker' 6 ) 7 8 return ([(b'Content-Type', b'text/html')], b""" 9 <!doctype html> 10 <html> 11 <body> 12 <script> 13 function nestedLoaded() { 14 parent.postMessage({ type: 'NESTED_LOADED' }, '*'); 15 } 16 </script> 17 <iframe src="?nested=true&ping=true" id="nested" onload="nestedLoaded()"></iframe> 18 <script> 19 // Helper routine to make it slightly easier for our parent to find 20 // the nested frame. 21 function nested() { 22 return document.getElementById('nested').contentWindow; 23 } 24 25 // This modifies the nested iframe immediately and does not wait for it to 26 // load. This effectively modifies the global for the initial about:blank 27 // document. Any modifications made here should be preserved after the 28 // frame loads because the global should be re-used. 29 let win = nested(); 30 if (win.location.href !== 'about:blank') { 31 parent.postMessage({ 32 type: 'NESTED_LOADED', 33 result: 'failed: nested iframe does not have an initial about:blank URL' 34 }, '*'); 35 } else { 36 win.navigator.serviceWorker.addEventListener('message', evt => { 37 if (evt.data.type === 'PING') { 38 evt.source.postMessage({ 39 type: 'PONG', 40 location: win.location.toString() 41 }); 42 } 43 }); 44 win.navigator.serviceWorker.startMessages(); 45 } 46 </script> 47 </body> 48 </html> 49 """)