Skip to content

Instantly share code, notes, and snippets.

@gavinhungry
Last active May 27, 2022 22:58
Show Gist options
  • Save gavinhungry/5373f7de495a820f4a49eea202354840 to your computer and use it in GitHub Desktop.
Save gavinhungry/5373f7de495a820f4a49eea202354840 to your computer and use it in GitHub Desktop.
Auto Basic Auth
(() => {
/*
chrome.storage.local.set({
credentials: {
'example.com': {
username: 'foo',
password: '***'
}
}
});
*/
chrome.storage.local.get(['credentials'], ({ credentials = {} }) => {
const REQS = new Set();
setInterval(() => {
REQS.clear();
}, 60000);
let onAuthRequired = ({ challenger, requestId }) => {
let hostKey = Object.keys(credentials).find(hostKey => {
return (
challenger.host === hostKey ||
challenger.host.endsWith(`.${hostKey}`)
);
});
if (!hostKey) {
return;
}
if (REQS.has(requestId)) {
return {
cancel: true
};
};
REQS.add(requestId);
return {
authCredentials: credentials[hostKey]
};
};
chrome.webRequest.onAuthRequired.addListener(onAuthRequired, {
urls: ['https://*/*']
}, ['blocking']);
});
})();
{
"name": "Auto Basic Auth",
"description": "Auto-login with basic auth",
"author": "Gavin Lloyd <gavinhungry@gmail.com>",
"manifest_version": 2,
"version": "1.2.0",
"permissions": ["webRequest", "webRequestBlocking", "storage", "https://*/*"],
"background": {
"persistent": true,
"scripts": ["auto-basic-auth.js"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment