Skip to content

Instantly share code, notes, and snippets.

@advait
Created September 21, 2017 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save advait/1b657c683dc8e30b6de7c0e17baabd16 to your computer and use it in GitHub Desktop.
Save advait/1b657c683dc8e30b6de7c0e17baabd16 to your computer and use it in GitHub Desktop.
//var bkg = chrome.extension.getBackgroundPage();
var calls = {};
// Shows settings on install.
chrome.runtime.onInstalled.addListener(function(details) {
if(details.reason && (details.reason === 'install') || (details.reason === 'update')){
chrome.tabs.create({url: "options.html"});
}
});
// Show settings when clicking on the icon.
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.create({url: "options.html"}, function(tab) {
console.log('options loaded');
});
});
/* Notifications */
chrome.notifications.onShowSettings.addListener(function(){
chrome.tabs.create({url: "options.html"});
});
chrome.notifications.onClicked.addListener(function(id){
if(id === NOTIFICATION_ID){
chrome.tabs.create({url: "options.html"});
}
});
/* Core */
chrome.webRequest.onAuthRequired.addListener(
function(details) {
var locked = isLocked();
var idstr = details.requestId.toString();
if(details.isProxy === true && !locked){
console.log('AUTH - ' + details.requestId);
//console.log(JSON.stringify(details));
if(!(idstr in calls)){
calls[idstr] = 0;
}
calls[idstr] = calls[idstr] + 1;
var retry = parseInt(localStorage["proxy_retry"]) || DEFAULT_RETRY_ATTEMPTS || 5;
if(calls[idstr] >= retry){
lock();
chrome.notifications.create(NOTIFICATION_ID, {
'type': 'basic',
'iconUrl': 'icon_locked_128.png',
'title': 'Proxy Auto Auth error',
'message': 'A lot of Proxy Authentication requests have been detected. There is probably a mistake in your credentials. For your safety, the extension has been temporary locked. To unlock it, click the save button in the options.',
'isClickable': true,
'priority': 2
}, function(id){
//console.log('notification callback');
});
calls = {};
return({
cancel : true
});
}
var login = localStorage["proxy_login"];
var password = localStorage["proxy_password"];
if (login && password && !locked){
return({
authCredentials : {
'username' : login,
'password' : password
}
});
}
}
},
{urls: ["<all_urls>"]},
["blocking"]
);
@arilewis93
Copy link

This is an extremely useful extension! thanks so much! could you add supports for adding the username and password from cmd, I found the following https://stackoverflow.com/questions/8725845/is-there-any-way-to-get-command-line-parameters-in-google-chrome-extension/9381181#9381181
chrome.windows.onCreated.addListener(function (window) { chrome.tabs.query({}, function (tabs) { var args = { username: null, password: null }, argName, regExp, match; for (argName in args) { regExp = new RegExp(argName + "=([^\&]+)") match = regExp.exec(tabs[0].url); if (!match) return; args[argName] = match[1]; } console.log(JSON.stringify(args)); }); });

this would mean that using cmd to open chrome to a URL for example chrome "http://127.0.0.1:0/?username=42&password=hello" would, as I understand - autofill the username and password fields. we use this extension to automatically authenticate our clients with our proxy so saving them having to enter the username and password into chrome would be helpful. and if it could also save them it would be a great help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment