代码无缘无故地无限重复

Code repeating itself infinitively for no apparent reason

我试图制作一个在弹出窗口中显示信息的扩展。它通过向 URL 发出 GET 请求来获取此信息,我通过查看来自某个网​​页的请求来获取该信息。为了测试,我只打印了请求的主体。

chrome.webRequest.onCompleted.addListener(
    logURL,
    {urls: ["<all_urls>"]}
);

function logURL(details) {
    var url = details.url;
    if (url.startsWith('https://www.whatever.com/api/')) {
        httpGet(url);
    }
}

function httpGet(apiURL) {   
    let data;
    function setData(dt) {
        data = dt;
    }   
    fetch(apiURL)
        .then(response => response.json()).then( json => setData(json))
        .catch(error => console.error(error))
        .finally(() => {
            console.log("Data received 1 --> ", data);
            data = null;
    });
}

但是,它会无限期地打印正文。我不知道为什么。我想知道如何修复它。

因为每次 webrequest 完成时,它都会调用 logURL,后者调用 httpGet 并启动一个新的 webrequest,后者完成并调用 logURL,等等。

回调 logURL 在请求完成时调用。这将永远持续下去,因为在完成一个请求后,您正在发出另一个请求