User:Ipatrol/botswitch.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Ipatrol/botswitch. |
importScript("User:Ipatrol/box-X.js");
Botswitch = {};
Botswitch.switchacc = function(u,p)
{
var req = false;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.XMLHttpRequest) {
req = new XMLHttpRequest("XMLHTTP");
}
if (req)
{
//log out
req.open("GET", "http://en.wikipedia.org/w/index.php?title=Special:Userlogout&action=raw&ctype=text/javascript", false);
//req.send(null);
//log in
req.overrideMimeType('text/xml');
req.open("POST", "http://en.wikipedia.org/w/api.php?action=login&lgname=" + encodeURIComponent(u) + "&lgpassword=" + encodeURIComponent(p) + "&format=xml",false);
req.send(null)
//errors
var res = req.responseXML;
var error = res.evaluate("//login/@result"
, res, null, XPathResult.STRING_TYPE, null ).stringValue;
if (error == "EmptyPass" || error == "WrongPass")
{
Cookies.erase("botswitch_pass_"+botswitch_altacc);
alert("Wrong password, cookie deleted");
} else if (error == "NoName" || error == "Illegal" || error == "NotExists")
{
alert("Wrong username, did you set botswitch_altacc?")
} else if (error == "Throttled")
{
alert("You've logged in too many times in a short time.");
} else {
//success
if (wgUserName != botswitch_altacc)
{
var userlink = document.getElementById("pt-userpage");
var swnotice = document.createElement("li");
swnotice.id = "pt-switchnotice";
swnotice.style.textTransform = "none";
swnotice.innerHTML = " - switched to <a " + "href=" + "'/wiki/User:" + encodeURIComponent(botswitch_altacc) + "' title='My user page'>" + botswitch_altacc + "</a>";
userlink.parentNode.insertBefore(swnotice,userlink.nextSibling);
botswitch_altacc = wgUserName;
} else {
botswitch_altacc = botswitch_altacc_ori;
var swnotice = document.getElementById("pt-switchnotice");
if (swnotice)
{
swnotice.parentNode.removeChild(swnotice);
}
}
}
}
}
Botswitch.passOk = function(wind)
{
var pass = document.getElementById("pass").value;
var checked = document.getElementById("savepass").checked;
wind.close();
if (checked)
{
Cookies.create("botswitch_pass_"+botswitch_altacc,pass,30);
}
Botswitch.switchacc(botswitch_altacc,pass);
}
Botswitch.askPass = function()
{
var passwindow = new JWindow();
passwindow.setWidth(280);
passwindow.setHeight(140);
passwindow.setResizable(false);
passwindow.setTitle("Password");
passwindow.setLeft(window.innerWidth/2-140/2);
passwindow.setTop(window.innerHeight/2-280/2);
var passform = new JForm();
passform.addControl(new JForm.control("label","nameLabel","nameLabel",{label:"Username: "+botswitch_altacc,boundto:"uname"}));
passform.addElement(document.createElement("br"));
passform.addControl(new JForm.control("label","passLabel","passLabel",{label:"Password: ",boundto:"pass"}));
passform.addControl(new JForm.control("password","pass","pass",{}));
passform.addElement(document.createElement("br"));
passform.addControl(new JForm.control("checkbox","savepass","savepass"));
passform.addControl(new JForm.control("label","saveLabel","saveLabel",{label:" save password",boundto:"savepass"}));
passform.addElement(document.createElement("br"));
passform.addControl(new JForm.control("button","passok","passok",{label:"Ok",callback:{handler:function() { Botswitch.passOk(passwindow); }}}));
passwindow.addElement(passform.root);
passwindow.display();
}
Botswitch.init = function()
{
//get pass from cookie
var pass;
if ((pass = Cookies.read("botswitch_pass_"+botswitch_altacc)) == null || pass == "")
{
//no pass set
Botswitch.askPass();
} else {
//renew cookie
Cookies.create("botswitch_pass_"+botswitch_altacc,pass,30);
//switch account
Botswitch.switchacc(botswitch_altacc,pass);
}
}
Botswitch.insertlink = function()
{
var logoutlink = document.getElementById("pt-logout");
if (logoutlink)
{
var switchlink = document.createElement("li");
switchlink.id = "pt-switch";
switchlink.innerHTML = "<a "+ "href='javascript:Botswitch.init()'" + "title='Switch' style='cursor:pointer;'>Switch</a>";
logoutlink.parentNode.appendChild(switchlink);
}
}
if (typeof botswitch_altacc != "undefined")
{
botswitch_altacc_ori = botswitch_altacc;
addOnloadHook(Botswitch.insertlink);
}