Skip to content

Instantly share code, notes, and snippets.

@gavinhungry gavinhungry/index.js
Last active May 31, 2019

Embed
What would you like to do?
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": ["index.js"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.