summaryrefslogtreecommitdiffstats
path: root/devtools/client/styleeditor/test/iframe_with_service_worker.html
blob: b66e1e74a8828947e57ed4b23fd24302db6434d5 (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
<!DOCTYPE html>
<meta charset="utf-8">

Iframe loading a stylesheet via a service worker
<script>
"use strict";

function waitForActive(swr) {
  const sw = swr.installing || swr.waiting || swr.active;
  return new Promise(resolve => {
    if (sw.state === "activated") {
      resolve(swr);
      return;
    }
    sw.addEventListener("statechange", function onStateChange() {
      if (sw.state === "activated") {
        sw.removeEventListener("statechange", onStateChange);
        resolve(swr);
      }
    });
  });
}

navigator.serviceWorker.register("iframe_service_worker.js", {scope: "."})
  .then(registration => waitForActive(registration))
  .then(() => {
    const link = document.createElement("link");
    link.setAttribute("rel", "stylesheet");
    link.setAttribute("type", "text/css");
    link.setAttribute("href", "sheet.css");
    document.documentElement.appendChild(link);
  });
</script>