DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (ba237def08d5)

VCS Links

Line Code
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
<!DOCTYPE HTML>
<html>
<head>
  <title>Tabs executeScript Test</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body>

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

add_task(async function testExecuteScript() {
  const BASE = "http://mochi.test:8888/tests/mobile/android/components/extensions/test/mochitest/";
  const URL = BASE + "file_iframe_document.html";

  let win = window.open(URL);
  await new Promise(resolve => win.addEventListener("load", resolve, {once: true}));

  async function background() {
    try {
      let [tab] = await browser.tabs.query({active: true, currentWindow: true});
      // TODO bug 1565536: tab.active is broken in GeckoView.
      browser.test.assertEq(undefined, tab, "currentWindow's tab is not active (bug 1565536)");
      [tab] = await browser.tabs.query({currentWindow: true});

      let frames = await browser.webNavigation.getAllFrames({tabId: tab.id});

      browser.test.log(`FRAMES: ${frames[1].frameId} ${JSON.stringify(frames)}\n`);
      await Promise.all([
        browser.tabs.executeScript({
          code: "42",
        }).then(result => {
          browser.test.assertEq(1, result.length, "Expected one callback result");
          browser.test.assertEq(42, result[0], "Expected callback result");
        }),

        browser.tabs.executeScript({
          file: "script.js",
          code: "42",
        }).then(result => {
          browser.test.fail("Expected not to be able to execute a script with both file and code");
        }, error => {
          browser.test.assertTrue(/a 'code' or a 'file' property, but not both/.test(error.message),
                                  "Got expected error");
        }),

        browser.tabs.executeScript({
          file: "script.js",
        }).then(result => {
          browser.test.assertEq(1, result.length, "Expected one callback result");
          browser.test.assertEq(undefined, result[0], "Expected callback result");
        }),

        browser.tabs.executeScript({
          file: "script2.js",
        }).then(result => {
          browser.test.assertEq(1, result.length, "Expected one callback result");
          browser.test.assertEq(27, result[0], "Expected callback result");
        }),

        browser.tabs.executeScript({
          code: "location.href;",
          allFrames: true,
        }).then(result => {
          browser.test.assertTrue(Array.isArray(result), "Result is an array");

          browser.test.assertEq(2, result.length, "Result has correct length");

          browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "First result is correct");
          browser.test.assertEq("http://mochi.test:8888/", result[1], "Second result is correct");
        }),

        browser.tabs.executeScript({
          code: "location.href;",
          allFrames: true,
          matchAboutBlank: true,
        }).then(result => {
          browser.test.assertTrue(Array.isArray(result), "Result is an array");

          browser.test.assertEq(3, result.length, "Result has correct length");

          browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "First result is correct");
          browser.test.assertEq("http://mochi.test:8888/", result[1], "Second result is correct");
          browser.test.assertEq("about:blank", result[2], "Thirds result is correct");
        }),

        browser.tabs.executeScript({
          code: "location.href;",
          runAt: "document_end",
        }).then(result => {
          browser.test.assertEq(1, result.length, "Expected callback result");
          browser.test.assertEq("string", typeof result[0], "Result is a string");

          browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "Result is correct");
        }),

        browser.tabs.executeScript({
          code: "window",
        }).then(result => {
          browser.test.fail("Expected error when returning non-structured-clonable object");
        }, error => {
          browser.test.assertEq("<anonymous code>", error.fileName, "Got expected fileName");
          browser.test.assertEq("Script '<anonymous code>' result is non-structured-clonable data",
                                error.message, "Got expected error");
        }),

        browser.tabs.executeScript({
          code: "Promise.resolve(window)",
        }).then(result => {
          browser.test.fail("Expected error when returning non-structured-clonable object");
        }, error => {
          browser.test.assertEq("<anonymous code>", error.fileName, "Got expected fileName");
          browser.test.assertEq("Script '<anonymous code>' result is non-structured-clonable data",
                                error.message, "Got expected error");
        }),

        browser.tabs.executeScript({
          file: "script3.js",
        }).then(result => {
          browser.test.fail("Expected error when returning non-structured-clonable object");
        }, error => {
          const expected = /Script '.*script3.js' result is non-structured-clonable data/;
          browser.test.assertTrue(expected.test(error.message), "Got expected error");
          browser.test.assertTrue(error.fileName.endsWith("script3.js"), "Got expected fileName");
        }),

        browser.tabs.executeScript({
          frameId: Number.MAX_SAFE_INTEGER,
          code: "42",
        }).then(result => {
          browser.test.fail("Expected error when specifying invalid frame ID");
        }, error => {
          browser.test.assertEq(`Frame not found, or missing host permission`,
                                error.message, "Got expected error");
        }),

        browser.tabs.create({url: "http://example.net/", active: false}).then(async tab => {
          await browser.tabs.executeScript(tab.id, {
            code: "42",
          }).then(result => {
            browser.test.fail("Expected error when trying to execute on invalid domain");
          }, error => {
            browser.test.assertEq(`Missing host permission for the tab`,
                                  error.message, "Got expected error");
          });

          await browser.tabs.remove(tab.id);
        }),

        browser.tabs.executeScript({
          code: "Promise.resolve(42)",
        }).then(result => {
          browser.test.assertEq(42, result[0], "Got expected promise resolution value as result");
        }),

        browser.tabs.executeScript({
          code: "location.href;",
          runAt: "document_end",
          allFrames: true,
        }).then(result => {
          browser.test.assertTrue(Array.isArray(result), "Result is an array");

          browser.test.assertEq(2, result.length, "Result has correct length");

          browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "First result is correct");
          browser.test.assertEq("http://mochi.test:8888/", result[1], "Second result is correct");
        }),

        browser.tabs.executeScript({
          code: "location.href;",
          frameId: frames[0].frameId,
        }).then(result => {
          browser.test.assertEq(1, result.length, "Expected one result");
          browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), `Result for frameId[0] is correct: ${result[0]}`);
        }),

        browser.tabs.executeScript({
          code: "location.href;",
          frameId: frames[1].frameId,
        }).then(result => {
          browser.test.assertEq(1, result.length, "Expected one result");
          browser.test.assertEq("http://mochi.test:8888/", result[0], "Result for frameId[1] is correct");
        }),

        browser.tabs.create({url: "http://example.com/"}).then(async tab => {
          let result = await browser.tabs.executeScript(tab.id, {code: "location.href"});

          browser.test.assertEq("http://example.com/", result[0], "Script executed correctly in new tab");

          await browser.tabs.remove(tab.id);
        }),

        // This currently does not work on Android.
        /*
        browser.tabs.create({url: "about:blank"}).then(async tab => {
          const result = await browser.tabs.executeScript(tab.id, {code: "location.href", matchAboutBlank: true});
          browser.test.assertEq("about:blank", result[0], "Script executed correctly in new tab");
          await browser.tabs.remove(tab.id);
        }),
        */

        new Promise(resolve => {
          browser.runtime.onMessage.addListener(message => {
            browser.test.assertEq("script ran", message, "Expected runtime message");
            resolve();
          });
        }),
      ]);

      browser.test.notifyPass("executeScript");
    } catch (e) {
      browser.test.fail(`Error: ${e} :: ${e.stack}`);
      browser.test.notifyFail("executeScript");
    }
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      "permissions": ["http://mochi.test/", "http://example.com/", "webNavigation"],
    },

    background,

    files: {
      "script.js": function() {
        browser.runtime.sendMessage("script ran");
      },

      "script2.js": "27",

      "script3.js": "window",
    },
  });

  await extension.startup();

  await extension.awaitFinish("executeScript");

  await extension.unload();

  win.close();
});
</script>

</body>
</html>