summaryrefslogtreecommitdiffstats
path: root/docshell/test/mochitest/file_bug1742865.sjs
blob: 950c30ecd28959fc91e10a21347557a393bffdea (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
69
70
71
72
73
74
75
76
77
Cu.importGlobalProperties(["URLSearchParams"]);

function handleRequest(request, response) {
  if (request.queryString == "reset") {
    setState("index", "0");
    response.setStatusLine(request.httpVersion, 200, "Ok");
    response.write("Reset");
    return;
  }

  let refresh = "";
  let index = Number(getState("index"));
  // index == 0 First load, returns first meta refresh
  // index == 1 Second load, caused by first meta refresh, returns second meta refresh
  // index == 2 Third load, caused by second meta refresh, doesn't return a meta refresh
  let query = new URLSearchParams(request.queryString);
  if (index < 2) {
    refresh = query.get("seconds");
    if (query.get("crossOrigin") == "true") {
      const hosts = ["example.org", "example.com"];

      let url = `${request.scheme}://${hosts[index]}${request.path}?${request.queryString}`;
      refresh += `; url=${url}`;
    }
    refresh = `<meta http-equiv="Refresh" content="${refresh}">`;
  }
  // We want to scroll for the first load, and check that the meta refreshes keep the same
  // scroll position.
  let scroll = index == 0 ? `scrollTo(0, ${query.get("scrollTo")});` : "";

  setState("index", String(index + 1));

  response.write(
    `<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="Cache-Control" content="no-cache">
  ${refresh}
  <script>
    window.addEventListener("pageshow", () => {
      ${scroll}
      window.top.opener.postMessage({
        commandType: "pageShow",
        commandData: {
          inputValue: document.getElementById("input").value,
          scrollPosition: window.scrollY,
        },
      }, "*");
    });
    window.addEventListener("message", ({ data }) => {
      if (data == "changeInputValue") {
        document.getElementById("input").value = "1234";
        window.top.opener.postMessage({
          commandType: "onChangedInputValue",
          commandData: {
            historyLength: history.length,
            inputValue: document.getElementById("input").value,
          },
        }, "*");
      } else if (data == "loadNext") {
        location.href += "&loadnext=1";
      } else if (data == "back") {
        history.back();
      }
    });
  </script>
</head>
<body>
<input type="text" id="input" value="initial"></input>
<div style='height: 9000px;'></div>
<p>
</p>
</body>
</html>`
  );
}