如何在 Firefox 扩展中捕获特定的 xmlhttprequests?

How can I capture specific xmlhttprequests in Firefox extension?

我正在编写一个 firefox 扩展,并希望捕获发送到某个 url 的请求。

我正在从后台脚本向事件添加侦听器,browser.webRequest.onCompleted。问题是,如果我在清单的权限部分添加 <all_urls> 并在添加侦听器时在过滤器中添加 urls 选项,则侦听器只会被触发。

src/background.js:

function saveData(result) {
    console.log(result);
}

browser.webRequest.onCompleted.addListener(
    saveData,
    {
        urls: ["<all_urls>"],
        types: ['xmlhttprequest']
    });

Manifest.json:

{
    "manifest_version": 2,
    "name": "LolEsports Extension",
    "version": "1.0.0",

    "permissions": [
        "<all_urls>",
        "webRequest",
        "webRequestBlocking",
        "storage"
    ],

    "background": {
        "scripts": ["src/background.js"]
    }
}

这是我得到的:

但是如果我将清单更改为:

"permissions": [
     "https://prod-relapi.ewp.gg/persisted/gw/*",
     "webRequest",
     "webRequestBlocking",
     "storage"
 ]

并在 background.js 中:

browser.webRequest.onCompleted.addListener(
    saveData,
    {
        urls: ["https://prod-relapi.ewp.gg/persisted/gw/*"],
        types: ['xmlhttprequest']
    });

控制台中未显示任何内容。我错过了什么让听众被特定的 url 模式激发??

Quoting MDN:

To intercept resources loaded by a page (such as images, scripts, or stylesheets), the extension must have the host permission for the resource as well as for the main page requesting the resource. For example, if a page at "https://developer.mozilla.org" loads an image from "https://mdn.mozillademos.org", then an extension must have both host permissions if it is to intercept the image request.

Quoting Chrome API documentation:

Starting from Chrome 72, an extension will be able to intercept a request only if it has host permissions to both the requested URL and the request initiator.

因此您需要在清单的 "permissions" 中添加 "https://watch.euw.lolesports.com/*"