简体   繁体   中英

How do I intercept and modify HTTP Auth requests from a Chrome extension?

Based on the documentation I have written the following code as a background script:

chrome.webRequest.onAuthRequired.addListener(
    function (details, callback) {
        console.log('onAuthRequired', details);
        callback({
            authCredentials: {username: "alpha", password: "beta"}
        });
    },
    {urls: ['<all_urls>']},
    ['asyncBlocking']
);
chrome.webRequest.onBeforeRequest.addListener(
    function(details, callback) {
        console.log('onBeforeRequest', details);
    },
    {urls: ['<all_urls>']}
);

The onBeforeRequest callback works, but the onAuthRequired callback does not. I do not seem to get 'onBeforeRequest' printed to the console as I expect. What is missing?

My intention is simply to automatically provide auth credentials for a specific domain. A working example of this would suffice as an answer.

您的代码看起来不错,但你需要使用webRequestBlocking的权限除了普通webRequest许可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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