Last active
August 7, 2016 06:01
-
-
Save ifree/0385f5a3a47e22046151 to your computer and use it in GitHub Desktop.
foxyproxy vimperator plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ==VimperatorPlugin== | |
* @name foxyproxy.js | |
* @description foxyproxy helper | |
* @minVersion 0.1pre | |
* @author Frei Zhang | |
* @version 0.1.0 | |
* ==/VimperatorPlugin== | |
* | |
* Usage: | |
* :foxyproxy init load Foxyproxy settings | |
* :foxyproxy change <proxy> switch to <proxy> | |
* EOM | |
*/ | |
(function() { | |
function loadSettings(){ | |
if (typeof(FoxyProxy) == "undefined") | |
liberator.echo("Please install FoxyProxy Basic 2.6+, Standard 3.6+, or Plus 4.6+"); | |
else if (FoxyProxy.apiDisabled) | |
liberator.echo("I cannot inspect FoxyProxy because the FoxyProxy API is disabled on your system."); | |
else { | |
FoxyProxy.getProxyConfigs({ | |
success: function(proxyConfigs) { | |
let configs = proxyConfigs.getAll(); | |
liberator.globalVariables.proxy_config = | |
configs.map(function(config){ | |
liberator.log({"id" : config.id, "name" : config.name}); | |
return {"id" : config.id, "name" : config.name}; | |
}); | |
liberator.echo("successful load Foxypory settings."); | |
}, | |
error: function(msg) { | |
liberator.echo("Error: " + msg); | |
}, | |
rejected: function rejectedByUser() { | |
liberator.echo("Please allow me to read/write FoxyProxy settings. Please try again and click the 'Allow' button."); | |
} | |
}); | |
} | |
} | |
commands.addUserCommand(["foxyproxy", "fp"], 'Foxyproxy settings', function (args) { | |
let isInit = args["init"], | |
changeTarget = args["change"]; | |
if(isInit){ | |
loadSettings(); | |
}else if(changeTarget){ | |
if(liberator.globalVariables.proxy_config){ | |
let proxies = liberator.globalVariables.proxy_config | |
.filter(function(itm){ | |
return itm.name == changeTarget; | |
}); | |
if(proxies.length > 0){ | |
ownerGlobal.foxyproxy.fp.setMode(proxies[0].id, true); | |
}else{ | |
ownerGlobal.foxyproxy.fp.setMode(changeTarget, true);//direct set | |
} | |
ownerGlobal.foxyproxy.updateViews(true);//refresh icon | |
} | |
liberator.echo("proxy changed."); | |
}else{ | |
liberator.echo("invalid option. use fp init, or fp change <proxy>"); | |
} | |
}, { | |
options : [[["change", "change proxy to "], commands.OPTION_STRING, | |
function(){return true;}, | |
function(context){ | |
if(liberator.globalVariables.proxy_config) | |
{ | |
return liberator.globalVariables.proxy_config.map( | |
function(itm){ | |
return [itm.name, '']; | |
} | |
).concat([["patterns", 'auto detect proxy'] | |
, ["disabled", 'disable proxy']]); | |
} | |
return null; | |
} | |
], | |
[["init", "init foxyproxy settings"], commands.OPTION_NOARG] | |
], | |
argCount : 0 | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment