Skip to content

Commit

Permalink
Update for MV3 and Chrome compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dotproto committed Jul 18, 2024
1 parent 91c8d7e commit d7641d9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
18 changes: 10 additions & 8 deletions stored-credentials/auth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Polyfill the "browser" global in Chrome.
globalThis.browser ??= chrome;

let target = "https://httpbin.org/basic-auth/*";

Expand All @@ -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(
Expand Down
11 changes: 8 additions & 3 deletions stored-credentials/manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -15,7 +15,8 @@
},

"background": {
"scripts": ["storage.js", "auth.js"]
"scripts": ["storage.js", "auth.js"],
"service_worker": "sw.js"
},

"options_ui": {
Expand All @@ -25,7 +26,11 @@
"permissions": [
"webRequest",
"webRequestBlocking",
"storage",
"webRequestAuthProvider",
"storage"
],

"host_permissions": [
"https://httpbin.org/basic-auth/*"
]
}
3 changes: 3 additions & 0 deletions stored-credentials/options/options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Polyfill the "browser" global in Chrome.
globalThis.browser ??= chrome;

const usernameInput = document.querySelector("#username");
const passwordInput = document.querySelector("#password");

Expand Down
3 changes: 3 additions & 0 deletions stored-credentials/storage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Polyfill the "browser" global in Chrome.
globalThis.browser ??= chrome;

/*
Default settings. Initialize storage to these values.
*/
Expand Down
2 changes: 2 additions & 0 deletions stored-credentials/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Import and synchronously execute other JavaScript files.
importScripts("storage.js", "auth.js");

0 comments on commit d7641d9

Please sign in to comment.