summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/fullscreen/open_and_focus_helper.html
blob: 06d1800714eb6b886cbb07919eaa6100a595cedc (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
<!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8'>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
</head>
<body>
  <input></input><br>
  <a href="https://example.com" target="test">link</a>
  <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.openedWindow = window.open('https://example.com' + window.location.pathname);
        if (canReply) event.ports[0].postMessage('opened');
      } else if(event.data === "openpopup") {
        window.openedWindow = window.open('https://example.com' + window.location.pathname, 'test', 'top=0,height=1, width=300');
        if (canReply) event.ports[0].postMessage('popupopened');
      } else if(event.data === "focus") {
        window.openedWindow.focus();
        if (canReply) event.ports[0].postMessage('focused');
      } else if(event.data === "elementfocus") {
        document.querySelector("input").focus();
        if (canReply) event.ports[0].postMessage('elementfocused');
      } else if(event.data === "clicklink") {
        synthesizeMouseAtCenter(document.querySelector("a"), {});
        if (canReply) event.ports[0].postMessage('linkclicked');
      }
    }
    window.addEventListener('message', window.onMessage);
  </script>
</body>
</html>