summaryrefslogtreecommitdiffstats
path: root/testing/talos/talos/tests/tabpaint/tabpaint.html
blob: 0ab7242304dab63b887c65008c121b572ac6af07 (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
<html>
<head>
<script>

function init() {
  let targetURL = new URL(window.location.href);
  let i = targetURL.pathname.lastIndexOf("/");
  targetURL.pathname = `${targetURL.pathname.substring(0, i)}/target.html`;

  window.addEventListener("TabPaint:FinalResults", (event) => {
    let { fromParent, fromContent } = event.detail;

    tpRecordTime([fromParent, fromContent].join(","), 0, "tabpaint-from-parent, tabpaint-from-content");
  }, {once: true});

  window.addEventListener("TabPaint:OpenFromContent", (event) => {
    let target = document.getElementById("target");

    //win.performance.now() + win.performance.timing.navigationStart gives the UNIX timestamp.
    let now = window.performance.now() + window.performance.timing.navigationStart;

    target.href = `${targetURL.href}?${now}`;
    target.click();
  });

  async function tryPing() {
    let pingPromise = new Promise(resolve => {
      window.addEventListener("TabPaint:Pong", resolve, {once: true});
      dispatchEvent(new CustomEvent("TabPaint:Ping", {bubbles: true}));
    });
    let timeoutPromise = new Promise((resolve, reject) => setTimeout(reject, 500));

    try {
      await Promise.race([pingPromise, timeoutPromise]);
    } catch (e) {
      return tryPing();
    }
    return null;
  }

  tryPing().then(() => {
    dispatchEvent(new CustomEvent("TabPaint:Go", {
      bubbles: true,
      detail: {
        target: targetURL.href,
      },
    }));
  });
}
</script>
</head>
<body onload="init();">
  Hello, Talos!

  <a href="#" id="target" target="_blank">I'll open a new tab</a>
</body>
</html>