Switching proxy profiles should change the selected account, not add another onAuthRequired listener. Otherwise old closures can keep answering with the account from a previous selection.

Register one named listener at the service worker’s top level. Read the current account when an event arrives:

function answerProxyChallenge(details, reply) {
  if (!details.isProxy) {
    reply({});
    return;
  }
  chrome.storage.session.get('selectedProxy').then(({selectedProxy: p}) => {
    if (!p || details.challenger.host !== p.host ||
        details.challenger.port !== p.port) {
      reply({});
      return;
    }
    reply({authCredentials: {username: p.username, password: p.password}});
  }).catch(() => reply({cancel: true}));
}

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

This fragment assumes the extension already stores the selected HTTP proxy under selectedProxy, with a numeric port and its intended account. The manifest needs webRequest, webRequestAuthProvider, storage and the required host permissions. Proxy selection itself also needs the proxy permission.

The registration happens synchronously. Storage is read inside the callback, after Chrome has an installed listener to dispatch to. Registering only after an initial storage promise resolves creates a wake-up gap.

Add a rejection limit before shipping

The fragment shows registration and account selection. Add a bounded retry record for each request so a rejected password is not submitted indefinitely. Clean that record on completion or failure. Worker globals can disappear when the worker stops, so keep state needed across wakes in an appropriate extension storage area.

storage.session survives worker restarts within the browser session; it is not permanent account storage across browser restarts. Define what the extension does when no selected account is available.

The isProxy and challenger checks prevent an unrelated website challenge from receiving this account. If the browser never dispatches the event, listener cleanup alone cannot produce it: inspect the actual request, permissions and browser behavior separately.

Winndannde jowitiiɗe heen