User:Ipatrol/botswitch.js

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 " + botswitch_altacc + "";

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 = "Switch";

logoutlink.parentNode.appendChild(switchlink);

}

}

if (typeof botswitch_altacc != "undefined")

{

botswitch_altacc_ori = botswitch_altacc;

addOnloadHook(Botswitch.insertlink);

}