summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_chrome_ext_contentscript_data_uri.html
blob: 42950c50ec1e3c002aa452be92130d9802e26fc7 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html>
<head>
  <title>Test content script matching a data: URI</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script src="head.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
</head>
<body>

<script>
"use strict";

add_task(async function test_contentscript_data_uri() {
  const target = ExtensionTestUtils.loadExtension({
    files: {
      "page.html": `<!DOCTYPE html>
        <meta charset="utf-8">
        <iframe id="inherited" src="data:text/html;charset=utf-8,inherited"></iframe>
      `,
    },
    background() {
      browser.test.sendMessage("page", browser.runtime.getURL("page.html"));
    },
  });

  const scripts = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["webNavigation"],
      content_scripts: [{
        all_frames: true,
        matches: ["<all_urls>"],
        run_at: "document_start",
        css: ["all_urls.css"],
        js: ["all_urls.js"],
      }],
    },
    files: {
      "all_urls.css": `
        body { background: yellow; }
      `,
      "all_urls.js": function() {
        document.body.style.color = "red";
        browser.test.assertTrue(location.protocol !== "data:",
                                `Matched document not a data URI: ${location.href}`);
      },
    },
    background() {
      browser.webNavigation.onCompleted.addListener(({url, frameId}) => {
        browser.test.log(`Document loading complete: ${url}`);
        if (frameId === 0) {
          browser.test.sendMessage("tab-ready", url);
        }
      });
    },
  });

  await target.startup();
  await scripts.startup();

  // Test extension page with a data: iframe.
  const page = await target.awaitMessage("page");

  // Hold on to the tab by the browser, as extension loads are COOP loads, and
  // will break WindowProxy references.
  let win = window.open();
  const browserFrame = win.browsingContext.embedderElement;
  win.location.href = page;

  await scripts.awaitMessage("tab-ready");
  win = browserFrame.contentWindow;
  is(win.location.href, page, "Extension page loaded into a tab");
  is(win.document.readyState, "complete", "Page finished loading");

  const iframe = win.document.getElementById("inherited").contentWindow;
  is(iframe.document.readyState, "complete", "iframe finished loading");

  const style1 = iframe.getComputedStyle(iframe.document.body);
  is(style1.color, "rgb(0, 0, 0)", "iframe text color is unmodified");
  is(style1.backgroundColor, "rgba(0, 0, 0, 0)", "iframe background unmodified");

  // Test extension tab navigated to a data: URI.
  const data = "data:text/html;charset=utf-8,also-inherits";
  win.location.href = data;

  await scripts.awaitMessage("tab-ready");
  win = browserFrame.contentWindow;
  is(win.location.href, data, "Extension tab navigated to a data: URI");
  is(win.document.readyState, "complete", "Tab finished loading");

  const style2 = win.getComputedStyle(win.document.body);
  is(style2.color, "rgb(0, 0, 0)", "Tab text color is unmodified");
  is(style2.backgroundColor, "rgba(0, 0, 0, 0)", "Tab background unmodified");

  win.close();
  await target.unload();
  await scripts.unload();
});

</script>

</body>
</html>