mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
Update for MV3 and Chrome compatibility
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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/*"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Polyfill the "browser" global in Chrome.
|
||||
globalThis.browser ??= chrome;
|
||||
|
||||
const usernameInput = document.querySelector("#username");
|
||||
const passwordInput = document.querySelector("#password");
|
||||
|
||||
|
||||
@@ -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
2
stored-credentials/sw.js
Normal file
@@ -0,0 +1,2 @@
|
||||
// Import and synchronously execute other JavaScript files.
|
||||
importScripts("storage.js", "auth.js");
|
||||
Reference in New Issue
Block a user