summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/navigation-redirect-to-http-iframe.html
blob: 40e27c630d29c694295374e83680da59944b08a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<script src="/common/get-host-info.sub.js"></script>
<script src="test-helpers.sub.js"></script>
<script>
var SCOPE = './redirect.py?Redirect=' + encodeURI('http://example.com');
var SCRIPT = 'navigation-redirect-to-http-worker.js';
var host_info = get_host_info();

navigator.serviceWorker.getRegistration(SCOPE)
  .then(function(registration) {
      if (registration)
        return registration.unregister();
    })
  .then(function() {
      return navigator.serviceWorker.register(SCRIPT, {scope: SCOPE});
    })
  .then(function(registration) {
      return new Promise(function(resolve) {
          registration.addEventListener('updatefound', function() {
              resolve(registration.installing);
            });
        });
    })
  .then(function(worker) {
      worker.addEventListener('statechange', on_state_change);
    })
  .catch(function(reason) {
      window.parent.postMessage({results: 'FAILURE: ' + reason.message},
                                host_info['HTTPS_ORIGIN']);
     });

function on_state_change(event) {
  if (event.target.state != 'activated')
    return;
  with_iframe(SCOPE, {auto_remove: false})
    .then(function(frame) {
        window.parent.postMessage(
            {results: frame.contentDocument.body.textContent},
            host_info['HTTPS_ORIGIN']);
      });
}
</script>