Update for MV3 and Chrome compatibility

This commit is contained in:
Simeon Vincent
2024-07-18 15:46:43 -07:00
parent 91c8d7e708
commit d7641d9261
5 changed files with 26 additions and 11 deletions

View File

@@ -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(

View File

@@ -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/*"
]
}

View File

@@ -1,3 +1,6 @@
// Polyfill the "browser" global in Chrome.
globalThis.browser ??= chrome;
const usernameInput = document.querySelector("#username");
const passwordInput = document.querySelector("#password");

View File

@@ -1,3 +1,6 @@
// Polyfill the "browser" global in Chrome.
globalThis.browser ??= chrome;
/*
Default settings. Initialize storage to these values.
*/

2
stored-credentials/sw.js Normal file
View File

@@ -0,0 +1,2 @@
// Import and synchronously execute other JavaScript files.
importScripts("storage.js", "auth.js");