summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/fullscreen/open_and_focus_helper.html
blob: 088e27896507ef580a7675fbd1ea1a7d1fe64acf (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
<!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8'>
</head>
<body>
  <script>
    const MY_ORIGIN = window.location.origin;
    const CROSS_ORIGIN = "https://example.org";

    // Creates an iframe with message channel to trigger window open and focus
    window.createIframe = function(id, crossOrigin = false) {
      return new Promise(resolve => {
        const origin = crossOrigin ? CROSS_ORIGIN : MY_ORIGIN;
        let iframe = document.createElement("iframe");
        iframe.id = id;
        iframe.src = origin + window.location.pathname;
        iframe.onload = () => resolve(iframe);
        document.body.appendChild(iframe);
      });
    }

    window.sendMessage = function(destWin, msg) {
      return new Promise(resolve => {
        let channel = new MessageChannel();
        channel.port1.onmessage = resolve;
        destWin.postMessage(msg, "*", [channel.port2]);
      });
    }

    window.onMessage = function(event) {
      let canReply = event.ports && !!event.ports.length;
      if(event.data === "open") {
        window.popup = window.open('https://example.com', '', 'top=0,height=1, width=300');
        if (canReply) event.ports[0].postMessage('opened');
      } else if(event.data === "focus") {
        window.popup.focus();
        if (canReply) event.ports[0].postMessage('focused');
      }
    }
    window.addEventListener('message', window.onMessage);
  </script>
</body>
</html>