10

i'm trying to intercept the proxy authorization inside a chrome extension. Following the answer to here: Domain Authorization in Chrome Extension and reading the docs here my code looks like this:

chrome.webRequest.onAuthRequired.addListener(
    function(details, callbackFn) {
        console.log("onAuthRequired!", details, callbackFn);
        //callback({
        //    authCredentials: {username: "1", password: "__TestUse"}
        //});
    },
    {urls: ["<all_urls>"]}
);

The problem is that callbackFn is undefined but should be a function.

Anyone got some ideas why callbackFn is undefined. As I read the docs I'm doing it right ..

Community
  • 1
  • 1
ashiso
  • 325
  • 1
  • 2
  • 10

1 Answers1

16

The code works, I just forgot to add another parameter ['asyncBlocking']. This code works just fine:

chrome.webRequest.onAuthRequired.addListener(
    function(details, callbackFn) {
        console.log("onAuthRequired!", details, callbackFn);
        callbackFn({
            authCredentials: {username: "1", password: "__TestUser"}
        });
    },
    {urls: ["<all_urls>"]},
    ['asyncBlocking']
);

Note that you need the webRequest and webRequestAuthProvider permissions for Manifest V3 (available as of Chrome 108): https://developer.chrome.com/docs/extensions/reference/webRequest/#event-onAuthRequired

slhck
  • 36,575
  • 28
  • 148
  • 201
ashiso
  • 325
  • 1
  • 2
  • 10
  • how does this work anyway in manifest v3? asynBlocking and blocking throws error, as blocking should be now in declarativeNetRequest. – Marcus Jason Jul 22 '21 at 05:55
  • stay with v2 for now https://bugs.chromium.org/p/chromium/issues/detail?id=1135492 – herb Apr 08 '22 at 12:03
  • Note that as of v108 Chrome supports a new permission `webRequestAuthProvider`: https://developer.chrome.com/docs/extensions/reference/webRequest/#event-onAuthRequired – slhck Jul 03 '23 at 09:58