重定向到火狐中的扩展资源

Redirecting to extension resource in firefox

本文关键字:扩展 资源 火狐 重定向      更新时间:2023-09-26

出于开发目的,我正在尝试构建一个扩展,将所有与正则表达式匹配的请求重定向到特定页面。问题是Firefox API似乎没有做chrome.webRequest.onBeforeSendHeaders.addListener文档中宣传的内容。以下是扩展的简化版本:

清单.js

{
    "applications": {
      "gecko": {
        "id": "addon@example.com",
          "strict_min_version": "42.0",
          "strict_max_version": "50.*",
          "update_url": "https://example.com/updates.json"
      }
    },
    "name": "Developer",
    "version": "0.1.26",
    "manifest_version": 2,
    "description": "A script useful for development.",
    "icons": {"16": "logo16.png",
              "48": "logo48.png",
              "128": "logo128.png"},
    "background": {
    "scripts": ["background.js"]
    },
    "web_accessible_resources": ["hello.html"],
    "permissions": [
        "activeTab",
        "webRequest",
        "webRequestBlocking"
    ]
}

背景.js

// I found somewhere that onBeforeSendHeader it should work but it doesn't.
chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    var redirect;
    if (details.url.match(/example'.com/)) {
      redirect = chrome.extension.getURL("hello.html");
      console.log("Redirecting:", details.url, "->", redirect);
      return {redirectUrl: redirect};
      }
    console.log("Requesting:",details.url);
  }, {urls: [
    "<all_urls>"
  ]}, ["blocking"]);

你好.html

<html>
    <head>It works</head>
    <body>And it's not apache!</body>
</html>

简而言之,它将从example.com获取的任何内容重定向到扩展资源hello.html

所以我去 about:config 并将security.fileuri.strict_origin_policy设置为 false .然后我转到about:debugging并加载扩展。然后我打开浏览器控制台Tools -> Web Developer -> Browser Console.最后我去 example.com。我应该得到hello.html的内容,但我什么也没得到(白屏),在浏览器控制台中我得到:

Redirecting: "http://example.com/" -> "moz-extension://ce33a9b5-2c20-ed41-b8aa-f52143783c38/hello.html"
Security Error: Content at http://example.com/ may not load or link to file:///path/to/extension/hello.html.

我需要出于个人发展目的的扩展,所以我不介意更改about:config

编辑:如果我将重定向URL更改为网络上的某些内容并onBeforeReqeuest onBeforeSendHeaders一切正常:

chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    var redirect;
    if (details.url.match(/example'.com/)) {
      redirect = "https://www.google.com"; // chrome.extension.getURL("hello.html");
      console.log("Redirecting:", details.url, "->", redirect);
      return {redirectUrl: redirect};
      }
    console.log("Requesting:",details.url);
  }, {urls: [
    "<all_urls>"
  ]}, ["blocking"]);

Edit2:抱歉,这将是一个Web扩展(尽管我认为从有一个manifest.json文件而不是install.rdf文件这一事实中可以明显看出)。此外,addListener部分的BeforeRequest文档指出:

返回:webRequest.BlockingResponse。如果在"extraInfoSpec"参数中指定了"blocking",则事件侦听器应返回此类型的对象。

然后在 BlockingResponse 文档中:

重定向网址可选 字符串。仅用作对 onBeforeRequest 和 onHeadersReceived 事件的响应。如果设置,则阻止原始请求 从发送/完成,而是重定向到给定的 URL。 允许重定向到非 HTTP 方案(如数据):)。重 定向 由重定向操作启动,使用原始请求方法 重定向,但有一个例外:如果重定向是在 onHeadersReceived 阶段,然后重定向将使用 获取方法。

这对我适用于chrome和Firefox。

    let { tabId } = details;
    let redirectUrl = chrome.extension.getURL('hello.html');
    if(navigator.userAgent.toLowerCase().indexOf("firefox") > -1) {
        chrome.tabs.update(tabId, {
            url: redirectUrl
        })
        return {
            cancel: true
        }
    } else return { redirectUrl }