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

问题描述 投票:0回答:1

我正在写一个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模式激发?

javascript google-chrome-extension firefox-webextensions
1个回答
2
投票

Quoting MDN

要拦截页面加载的资源(例如图像,脚本或样式表),扩展必须具有资源的主机权限以及请求资源的主页。例如,如果“https://developer.mozilla.org”中的页面从“https://mdn.mozillademos.org”加载图像,则扩展名必须具有两个主机权限才能拦截图像请求。

Quoting Chrome API documentation

从Chrome 72开始,只有当请求具有对请求的URL和请求发起者的主机权限时,扩展才能拦截请求。

所以你需要在manifest的"https://watch.euw.lolesports.com/*"中添加"permissions"

© www.soinside.com 2019 - 2024. All rights reserved.