summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/mochitest/test_ext_exclude_include_globs.html
blob: 640522b40ab3670ec32b14515eb1a552c05fa29f (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for content script</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

add_task(function* test_contentscript() {
  function background() {
    browser.runtime.onMessage.addListener(([script], sender) => {
      browser.test.sendMessage("run", {script});
      browser.test.sendMessage("run-" + script);
    });
    browser.test.sendMessage("running");
  }

  function contentScriptAll() {
    browser.runtime.sendMessage(["all"]);
  }
  function contentScriptIncludesTest1() {
    browser.runtime.sendMessage(["includes-test1"]);
  }
  function contentScriptExcludesTest1() {
    browser.runtime.sendMessage(["excludes-test1"]);
  }

  let extensionData = {
    manifest: {
      content_scripts: [
        {
          "matches": ["http://example.org/", "http://*.example.org/"],
          "exclude_globs": [],
          "include_globs": ["*"],
          "js": ["content_script_all.js"],
        },
        {
          "matches": ["http://example.org/", "http://*.example.org/"],
          "include_globs": ["*test1*"],
          "js": ["content_script_includes_test1.js"],
        },
        {
          "matches": ["http://example.org/", "http://*.example.org/"],
          "exclude_globs": ["*test1*"],
          "js": ["content_script_excludes_test1.js"],
        },
      ],
    },
    background,

    files: {
      "content_script_all.js": contentScriptAll,
      "content_script_includes_test1.js": contentScriptIncludesTest1,
      "content_script_excludes_test1.js": contentScriptExcludesTest1,
    },

  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);

  let ran = 0;
  extension.onMessage("run", ({script}) => {
    ran++;
  });

  yield Promise.all([extension.startup(), extension.awaitMessage("running")]);
  info("extension loaded");

  let win = window.open("http://example.org/");
  yield Promise.all([extension.awaitMessage("run-all"), extension.awaitMessage("run-excludes-test1")]);
  win.close();
  is(ran, 2);

  win = window.open("http://test1.example.org/");
  yield Promise.all([extension.awaitMessage("run-all"), extension.awaitMessage("run-includes-test1")]);
  win.close();
  is(ran, 4);

  yield extension.unload();
  info("extension unloaded");
});
</script>

</body>
</html>