Chrome webRequest不再支持Manifest V3,如何重写代码使其支持Manifest V3?

k4aesqcs  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(304)

我需要更新这个代码,以便它在Manifest v3.On搜索我发现“Chrome.webRequest”现在在Manifest v3中不支持。在文档中,它说要使用“Chrome”,但是如何将它与“onAuthRequired”和“onBeforeSendHeaders.addListener”一起使用

Proxy.prototype.setProxy = function (
        proxyAddress,
        proxyUsername,
        proxyPassword
      ) {
        if (proxyAddress === undefined || proxyAddress.trim() === "") {
          setProxySettings(config())
            .then(() => {
              debugProxySettings();
            })
            .catch((error) => {
              console.error("Error setting proxy:", error);
            });
        } else {
          setProxySettings(config(proxyAddress))
            .then(() => {
              if (proxyAddress !== "" && proxyUsername !== "") {
                if (chrome.webRequest.onAuthRequired) {
                  chrome.webRequest.onAuthRequired.addListener(
                    function (details) {
                      return {
                        authCredentials: {
                          username: proxyUsername.trim(),
                          password: proxyPassword.trim(),
                        },
                      };
                    },
                    { urls: ["<all_urls>"] },
                    ["blocking"]
                  );
                } else {
                  chrome.webRequest.onBeforeSendHeaders.addListener(
                    function (details) {
                      return credentialsToHeader(
                        details,
                        proxyUsername.trim(),
                        proxyPassword.trim()
                      );
                    },
                    { urls: ["<all_urls>"] },
                    ["blocking", "requestHeaders"]
                  );
                }
              }
              debugProxySettings(proxyUsername, proxyPassword);
            })
            .catch((error) => {
              console.error("Error setting proxy:", error);
            });
        }
      };
mlnl4t2r

mlnl4t2r1#

将此权限添加到清单文件
manifest.json

"permissions": [
  "webRequest",
  "webRequestAuthProvider"
]

backgroung.js

function authRequired() {
  function callbackFn(details, callback) {
    const response = {
      authCredentials: {
        username: username,
        password: password,
      },
    }
    callback(response)
  }
}

chrome.webRequest.onAuthRequired.addListener(
  callbackFn,
  { urls: ['<all_urls>'] },
  ['asyncBlocking']
)
}

这个对我很有效

相关问题