summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_contentscript_in_background.js
blob: 15c5b30542ca97abc199c4b5fe992d96c0e82341 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const server = createHttpServer({ hosts: ["example.com"] });
server.registerPathHandler("/dummyFrame", (request, response) => {
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/html; charset=utf-8", false);
  response.write("");
});

add_task(async function content_script_in_background_frame() {
  // Test loads http: frame in background page.
  allow_unsafe_parent_loads_when_extensions_not_remote();

  async function background() {
    const FRAME_URL = "http://example.com:8888/dummyFrame";
    await browser.contentScripts.register({
      matches: ["http://example.com/dummyFrame"],
      js: [{ file: "contentscript.js" }],
      allFrames: true,
    });

    let f = document.createElement("iframe");
    f.src = FRAME_URL;
    document.body.appendChild(f);
  }

  function contentScript() {
    browser.test.log(`Running content script at ${document.URL}`);
    browser.test.sendMessage("done_in_content_script");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["http://example.com/*"],
    },
    files: {
      "contentscript.js": contentScript,
    },
    background,
  });
  await extension.startup();
  await extension.awaitMessage("done_in_content_script");
  await extension.unload();

  revert_allow_unsafe_parent_loads_when_extensions_not_remote();
});