summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/localStorage.html
blob: 6ba733a31db6407794c5e976362e1a13e42de598 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<h1>Here a tracker!</h1>
<script>

if (window.opener) {
  SpecialPowers.wrap(document).userInteractionForTesting();
  localStorage.foo = "opener" + Math.random();
  // Don't call window.close immediatelly. It can happen that adding the
  // "storage" event listener below takes more time than usual (it may need to
  // synchronously subscribe in the parent process to receive storage
  // notifications). Spending more time in the initial script can prevent
  // the "load" event from being fired for the window opened by "open and test".
  setTimeout(() => {
    window.close();
  }, 0);
}

if (parent) {
  window.onmessage = e => {
    if (e.data == "test") {
      let status;
      try {
        localStorage.foo = "value" + Math.random();
        status = true;
      } catch (e) {
        status = false;
      }

      parent.postMessage({type: "test", status }, "*");
      return;
    }

    if (e.data == "open") {
      window.open("localStorage.html");
      return;
    }

    if (e.data == "open and test") {
      let w = window.open("localStorage.html");
      w.addEventListener("load", _ => {
        let status;
        try {
          localStorage.foo = "value" + Math.random();
          status = true;
        } catch (e) {
          status = false;
        }

        parent.postMessage({type: "test", status }, "*");
      }, {once: true});
    }
  };

  window.addEventListener("storage", () => {
    let fromOpener = localStorage.foo.startsWith("opener");

    let status;
    try {
      localStorage.foo = "value" + Math.random();
      status = true;
    } catch (e) {
      status = false;
    }

    parent.postMessage({type: "test", status: status && fromOpener }, "*");
  });
}

</script>