fetch-mixed-content-iframe.html (2146B)
1 <!DOCTYPE html> 2 <script src="/common/get-host-info.sub.js"></script> 3 <script src="test-helpers.sub.js?pipe=sub"></script> 4 <script> 5 var params = get_query_params(location.href); 6 var SCOPE = 'fetch-mixed-content-iframe-inscope-to-' + params['target'] + '.html'; 7 var URL = 'fetch-rewrite-worker.js'; 8 var host_info = get_host_info(); 9 10 window.addEventListener('message', on_message, false); 11 12 navigator.serviceWorker.getRegistration(SCOPE) 13 .then(function(registration) { 14 if (registration) 15 return registration.unregister(); 16 }) 17 .then(function() { 18 return navigator.serviceWorker.register(URL, {scope: SCOPE}); 19 }) 20 .then(function(registration) { 21 return new Promise(function(resolve) { 22 registration.addEventListener('updatefound', function() { 23 resolve(registration.installing); 24 }); 25 }); 26 }) 27 .then(function(worker) { 28 worker.addEventListener('statechange', on_state_change); 29 }) 30 .catch(function(reason) { 31 window.parent.postMessage({results: 'FAILURE: ' + reason.message}, 32 host_info['HTTPS_ORIGIN']); 33 }); 34 35 function on_state_change(event) { 36 if (event.target.state != 'activated') 37 return; 38 var frame = document.createElement('iframe'); 39 frame.src = SCOPE; 40 document.body.appendChild(frame); 41 } 42 43 function on_message(e) { 44 navigator.serviceWorker.getRegistration(SCOPE) 45 .then(function(registration) { 46 if (registration) 47 return registration.unregister(); 48 }) 49 .then(function() { 50 window.parent.postMessage(e.data, host_info['HTTPS_ORIGIN']); 51 }) 52 .catch(function(reason) { 53 window.parent.postMessage({results: 'FAILURE: ' + reason.message}, 54 host_info['HTTPS_ORIGIN']); 55 }); 56 } 57 58 function get_query_params(url) { 59 var search = (new URL(url)).search; 60 if (!search) { 61 return {}; 62 } 63 var ret = {}; 64 var params = search.substring(1).split('&'); 65 params.forEach(function(param) { 66 var element = param.split('='); 67 ret[decodeURIComponent(element[0])] = decodeURIComponent(element[1]); 68 }); 69 return ret; 70 } 71 </script>