tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_initialDocument_noLoadBlocking.js (2074B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // See bug 2003255
      7 add_task(async function test_extension_cannot_block_initial_load() {
      8  let extension = ExtensionTestUtils.loadExtension({
      9    manifest: {
     10      manifest_version: 2,
     11      name: "insert script to about:blank",
     12      version: "1.0",
     13      content_scripts: [
     14        {
     15          matches: ["<all_urls>"],
     16          js: ["content.js"],
     17          run_at: "document_start",
     18          all_frames: true,
     19          match_about_blank: true,
     20        },
     21      ],
     22    },
     23 
     24    files: {
     25      "content.js": function () {
     26        if (window.location.href != "about:blank#unique-hash") {
     27          return;
     28        }
     29 
     30        const script = document.createElement("script");
     31        script.src =
     32          "data:,(window.parent.postMessage(`script ${window.location.href}`))()";
     33        (document.documentElement || document).appendChild(script);
     34      },
     35    },
     36  });
     37 
     38  await extension.startup();
     39 
     40  const url = "https://example.com/";
     41  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url);
     42 
     43  await ContentTask.spawn(tab.linkedBrowser, [url], async function (url) {
     44    Assert.equal(content.location.href, url, "Correct content document");
     45 
     46    const scriptEvaluated = new Promise(
     47      res =>
     48        (content.onmessage = ({ data }) => {
     49          if (data == "script about:blank#unique-hash") {
     50            res();
     51          }
     52        })
     53    );
     54 
     55    let loaded = false;
     56    const iframe = content.document.createElement("iframe");
     57    iframe.onload = () => (loaded = true);
     58    // Let's set a unique hash in case there are other about:blank
     59    iframe.src = "about:blank#unique-hash";
     60    content.document.body.append(iframe);
     61    Assert.ok(loaded, "Load occurred synchronously");
     62 
     63    const extScript = iframe.contentDocument.querySelector("script");
     64    Assert.ok(!!extScript, "Extension inserted script synchronously");
     65 
     66    await scriptEvaluated;
     67  });
     68 
     69  BrowserTestUtils.removeTab(tab);
     70  await extension.unload();
     71 });