2 <script src=
"../../resources/get-host-info.js"></script>
3 <script src=
"test-helpers.js?pipe=sub"></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();
10 window
.addEventListener('message', on_message
, false);
12 navigator
.serviceWorker
.getRegistration(SCOPE
)
13 .then(function(registration
) {
15 return registration
.unregister();
18 return navigator
.serviceWorker
.register(URL
, {scope
: SCOPE
});
20 .then(function(registration
) {
21 return new Promise(function(resolve
) {
22 registration
.addEventListener('updatefound', function() {
23 resolve(registration
.installing
);
27 .then(function(worker
) {
28 worker
.addEventListener('statechange', on_state_change
);
30 .catch(function(reason
) {
31 window
.parent
.postMessage({results
: 'FAILURE: ' + reason
.message
},
32 host_info
['HTTP_ORIGIN']);
35 function on_state_change(event
) {
36 if (event
.target
.state
!= 'activated')
38 var frame
= document
.createElement('iframe');
40 document
.body
.appendChild(frame
);
43 function on_message(e
) {
44 navigator
.serviceWorker
.getRegistration(SCOPE
)
45 .then(function(registration
) {
47 return registration
.unregister();
50 window
.parent
.postMessage(e
.data
, host_info
['HTTP_ORIGIN']);
52 .catch(function(reason
) {
53 window
.parent
.postMessage({results
: 'FAILURE: ' + reason
.message
},
54 host_info
['HTTP_ORIGIN']);
58 function get_query_params(url
) {
59 var search
= (new URL(url
)).search
;
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]);