diff --git a/stored-credentials/auth.js b/stored-credentials/auth.js index e4ffda1d..a1ab6beb 100644 --- a/stored-credentials/auth.js +++ b/stored-credentials/auth.js @@ -1,3 +1,5 @@ +// Polyfill the "browser" global in Chrome. +globalThis.browser ??= chrome; let target = "https://httpbin.org/basic-auth/*"; @@ -14,27 +16,27 @@ function completed(requestDetails) { } } -function provideCredentialsAsync(requestDetails) { - // If we have seen this request before, - // then assume our credentials were bad, +async function provideCredentialsAsync(requestDetails, asyncCallback) { + // If we have seen this request before, then assume our credentials were bad, // and give up. if (pendingRequests.indexOf(requestDetails.requestId) != -1) { console.log("bad credentials for: " + requestDetails.requestId); return {cancel: true}; - + } else { pendingRequests.push(requestDetails.requestId); console.log("providing credentials for: " + requestDetails.requestId); - // we can return a promise that will be resolved - // with the stored credentials - return browser.storage.local.get(null); + // We can respond asynchronously by calling asyncCallback and providing the + // authentication credentials. + const {authCredentials} = await browser.storage.local.get("authCredentials"); + asyncCallback({authCredentials}); } } browser.webRequest.onAuthRequired.addListener( provideCredentialsAsync, {urls: [target]}, - ["blocking"] + ["asyncBlocking"] ); browser.webRequest.onCompleted.addListener( diff --git a/stored-credentials/manifest.json b/stored-credentials/manifest.json index 65a15c8e..1429284a 100644 --- a/stored-credentials/manifest.json +++ b/stored-credentials/manifest.json @@ -1,6 +1,6 @@ { "description": "Performs basic authentication by supplying stored credentials.", - "manifest_version": 2, + "manifest_version": 3, "name": "stored-credentials", "version": "2.0", "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/stored-credentials", @@ -15,7 +15,8 @@ }, "background": { - "scripts": ["storage.js", "auth.js"] + "scripts": ["storage.js", "auth.js"], + "service_worker": "sw.js" }, "options_ui": { @@ -25,7 +26,11 @@ "permissions": [ "webRequest", "webRequestBlocking", - "storage", + "webRequestAuthProvider", + "storage" + ], + + "host_permissions": [ "https://httpbin.org/basic-auth/*" ] } diff --git a/stored-credentials/options/options.js b/stored-credentials/options/options.js index bb0477d6..80b7d64f 100644 --- a/stored-credentials/options/options.js +++ b/stored-credentials/options/options.js @@ -1,3 +1,6 @@ +// Polyfill the "browser" global in Chrome. +globalThis.browser ??= chrome; + const usernameInput = document.querySelector("#username"); const passwordInput = document.querySelector("#password"); diff --git a/stored-credentials/storage.js b/stored-credentials/storage.js index 847d1af4..c994332c 100644 --- a/stored-credentials/storage.js +++ b/stored-credentials/storage.js @@ -1,3 +1,6 @@ +// Polyfill the "browser" global in Chrome. +globalThis.browser ??= chrome; + /* Default settings. Initialize storage to these values. */ diff --git a/stored-credentials/sw.js b/stored-credentials/sw.js new file mode 100644 index 00000000..fd40c51b --- /dev/null +++ b/stored-credentials/sw.js @@ -0,0 +1,2 @@ +// Import and synchronously execute other JavaScript files. +importScripts("storage.js", "auth.js");