chrome 扩展清单 V2 到 V3

chrome extension manifest V2 to V3

提问人:Qazi Muzammil 提问时间:11/4/2023 更新时间:11/4/2023 访问量:43

问:

我正在尝试将我的 chrome 扩展程序版本 2 转换为版本 3,但在转换 webRequest API 代码时遇到问题:

代码在 backgorund 中.js

chrome 扩展 V2 的代码:

chrome.webRequest.onBeforeSendHeaders.addListener(
    modifyRequestHeaders,
    {
        urls: ["<all_urls>"],
    },
    ["blocking", "requestHeaders", "extraHeaders"]
);
chrome.webRequest.onAuthRequired.addListener(
    function (e) {
        let parsedDataObject = JSON.parse(tripleEncodeDecode(localStorage.xb33, false));
        if (parsedDataObject[0] != "" && parsedDataObject[1] != "" && parsedDataObject[2] != "") {
            return {
                authCredentials: {
                    username: parsedDataObject[1],
                    password: parsedDataObject[2],
                },
            };
        }
    },
    {
        urls: ["<all_urls>"],
    },
    ["blocking"]
);
chrome.webRequest.onBeforeRequest.addListener(
    (details) => {
        const domain = extractDomainFromUrl(details.url);
        const encodedDomain = btoa(domain);
        if (hostsListArray.includes(details.url) && sessionStorage[encodedDomain] !== "") {
            return { redirectUrl: "https://softwebtuts.com/404" };
        }
    },
    { urls: ["<all_urls>"] },
    ["blocking"]
);

请帮帮我,提前致谢

我重写的代码,但它不起作用 😢

chrome.webRequest.onBeforeSendHeaders.addListener(
    (details) => {
        let parsedDataObject = JSON.parse(tripleEncodeDecode(localStorage.xb33, false));
        if (parsedDataObject[0] !== "" && parsedDataObject[1] !== "" && parsedDataObject[2] !== "") {
            const headers = details.requestHeaders;
            for (let i = 0; i < headers.length; i++) {
                if (headers[i].name.toLowerCase() === "authorization") {
                    headers[i].value = "Basic " + btoa(parsedDataObject[1] + ":" + parsedDataObject[2]);
                }
            }
            return { requestHeaders: headers };
        }
    },
    {
        urls: ["<all_urls>"],
    },
    ["blocking", "requestHeaders"]
);

google-chrome google-chrome-扩展程序 清单

评论

0赞 wOxxOm 11/5/2023
它在 ManifestV3 中不起作用,因为普通扩展不支持模式。您必须通过 declarativeNetRequest API 使用基于规则的方法。blocking

答: 暂无答案