简体   繁体   English

Chrome 扩展:onCompleted 侦听器多次触发

[英]Chrome extension : onCompleted listener firing multiple times

This is my very first question here.这是我在这里的第一个问题。 I'm developing a basic Chrome extension with the following code.我正在使用以下代码开发一个基本的 Chrome 扩展。

manifest.json清单.json

{
    "manifest_version": 3,
    "name": "Name",
    "description": "Description",
    "version": "1.0",
    "commands": {},
    "background": {
        "service_worker":"background.js"
    },
    "action": {},
    "permissions": [
        "webRequest"
    ],
    "host_permissions": [
        "https://*/*"
    ]
}

background.js背景.js

function logURL(requestDetails) {
    if (requestDetails.url.indexOf("&userproof")>0) {
        console.log(`Loading URL: ${requestDetails.url}`);

        fetch(requestDetails.url)
        .then(response => response.json())
        .then(data => console.log("data::" + data));
    }
    return true;
}


chrome.webRequest.onCompleted.addListener(
    logURL,
    {urls: ["https://website/folder1/folder2/*"]},
    ['responseHeaders']
);

The extension is installed in developer mode from a folder, and shows no error upon loading or activation.该扩展程序是从文件夹以开发人员模式安装的,加载或激活时不会显示任何错误。

Any hint or support is more than welcome, Imrahjel.任何提示或支持都非常受欢迎,Imrahjel。

When I go into DevTools and open the service_workers panel, I see my listener behaving as needed, I mean it triggers when a HTTP request containing "&userproof" appears.当我 go 进入 DevTools 并打开 service_workers 面板时,我看到我的侦听器按需要运行,我的意思是它在出现包含“&userproof”的 HTTP 请求时触发。

The issue is that rather than firing one time, it fires dozens of time, eventually until the never if I wait long enough.问题是,它不是开火一次,而是开火几十次,如果我等待足够长的时间,最终会一直开火。

I have absolutely no idea with this listener is triggered more than one time, as the targeted HTTP request appears only once in the DevTools console.我完全不知道这个侦听器被触发了不止一次,因为目标 HTTP 请求只在 DevTools 控制台中出现一次。

I'm actually working on Debian 11 with Brave as a browser.我实际上正在使用 Brave 作为浏览器来处理 Debian 11。 Also tested with standard Google Chrome with no difference.还使用标准 Google Chrome 进行了测试,没有任何区别。

Well,出色地,

thanks to @wOxxOm recommendation to look into requestDetails.initiator, I finally made it work !感谢@wOxxOm建议查看 requestDetails.initiator,我终于成功了!

For those interested, here is how I managed, it's all about adapting one line in the code given in the original post.对于那些感兴趣的人,这就是我的管理方式,所有这些都是关于调整原始帖子中给出的代码中的一行。

You have to replace the following line您必须替换以下行

if (requestDetails.url.indexOf("&userproof")>0) {

with this one有了这个

if ((requestDetails.url.indexOf("&userproof")>0) && requestDetails.initiator.indexOf("www.website.com")>0)) {

With this modification, because fetching from within an extension is not showing the same initiator value, the fetch() will run only for requests originated by "www.website.com".通过此修改,因为从扩展中获取数据不会显示相同的启动器值,所以 fetch() 将仅针对“www.website.com”发起的请求运行。

I'm really new here and wonder how to grant wOxxom a deserved upvote or +rep, so in case one can tell me, I would be grateful as the solution is clearly from him.我在这里真的很新,想知道如何授予wOxxom应得的赞成票或 +rep,所以如果有人能告诉我,我将不胜感激,因为解决方案显然来自他。

Thanks, Imrahjel.谢谢,Imrahjel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM