Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for MV3 and Chrome compatibility #565

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] <no-undef> reported by reviewdog 🐶
'importScripts' is not defined.

Loading