User:Nx/Scripts/Botswitch.js
From RationalWiki
Note: After saving, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
importScript("User:Nx/Scripts/Shared.js"); Botswitch = {}; Botswitch.switchacc = function(u,p) { var req = false; if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } if (req) { //log out req.open("GET", "http://rationalwiki.com/wiki/index.php?title=Special:Userlogout&action=raw&ctype=text/javascript", false); //req.send(null); //log in req.overrideMimeType('text/xml'); req.open("POST", "http://rationalwiki.com/wiki/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); }