gpt4 book ai didi

javascript - 如何在 Firefox 扩展中捕获特定的 xmlhttp 请求?

转载 作者:行者123 更新时间:2023-12-01 01:16:34 24 4
gpt4 key购买 nike

我正在编写一个 Firefox 扩展,想要捕获发送到某个 url 的请求。

我正在向事件添加一个监听器,browser.webRequest.onCompleted来自后台脚本。问题是,只有当我添加 <all_urls> 时,监听器才会被激发。添加监听器时,在 list 的权限部分和过滤器中的 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"]
}
}

这是我得到的: screenshot

但是如果我将 list 更改为:

"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.

因此您需要在 list 的“权限”中添加“https://watch.euw.lolesports.com/*”

关于javascript - 如何在 Firefox 扩展中捕获特定的 xmlhttp 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com