User:Kotepho/monobook.js

// function getElementsByClass(searchClass,node,tag) { // Function from http://www.dustindiaz.com/getelementsbyclass/ var classElements = new Array; if ( node == null ) node = document; if ( tag == null ) tag = '*'; var els = node.getElementsByTagName(tag); var elsLen = els.length; var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); for (i = 0, j = 0; i < elsLen; i++) { if ( pattern.test(els[i].className) ) { classElements[j] = els[i]; j++; } }  return classElements; }

function getParamValue(paramName) { var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)'); var h=document.location; var m; if (m=cmdRe.exec(h)) { try { return decodeURI(m[1]); } catch (someError) {} } return null; };

function substitute(data,cmdBody) { // alert('sub\nfrom: '+cmdBody.from+'\nto: '+cmdBody.to+'\nflags: '+cmdBody.flags); var fromRe=RegExp(cmdBody.from, cmdBody.flags); return data.replace(fromRe, cmdBody.to); };

function execCmds(data, cmdList) { for (var i=0; i<cmdList.length; ++i) { data=cmdList[i].action(data, cmdList[i]); } return data; }

function parseCmd(str) { // returns a list of commands if (!str.length) return []; var p=false; switch (str.charAt(0)) { case 's': p=parseSubstitute(str); break; case 'j': p=parseJavascript(str); break; default: return false; } if (p) return [p].concat(parseCmd(p.remainder)); return false; };

function unEscape(str, sep) { return str.split('\\\\').join('\\') .split('\\'+sep).join(sep) .split('\\n').join('\n'); };

function runJavascript(data, argWrapper) { // flags aren't used (yet)

// from the user's viewpoint, // data is a special variable may appear inside code // and gets assigned the text in the edit box

// alert('eval-ing '+argWrapper.code);

return eval(argWrapper.code); };

function parseJavascript(str) { // takes a string like j/code/;othercmds and parses it

var tmp,code,flags;

if (str.length<3) return false; var sep=str.charAt(1); str=str.substring(2); tmp=skipOver(str,sep); if (tmp) { code=tmp.segment.split('\n').join('\\n'); str=tmp.remainder; } else return false;

flags=''; if (str.length) { tmp=skipOver(str,';') || skipToEnd(str, ';'); if (tmp) {flags=tmp.segment; str=tmp.remainder; } }

return { action: runJavascript, code: code, flags: flags, remainder: str }; };

function parseSubstitute(str) { // takes a string like s/a/b/flags;othercmds and parses it

var from,to,flags,tmp;

if (str.length<4) return false; var sep=str.charAt(1); str=str.substring(2);

tmp=skipOver(str,sep); if (tmp) { from=tmp.segment; str=tmp.remainder; } else return false;

tmp=skipOver(str,sep); if (tmp) { to=tmp.segment; str=tmp.remainder; } else return false;

flags=''; if (str.length) { tmp=skipOver(str,';') || skipToEnd(str, ';'); if (tmp) {flags=tmp.segment; str=tmp.remainder; } } return {action: substitute, from: from, to: to, flags: flags, remainder: str};

};

function skipOver(str,sep) { var endSegment=findNext(str,sep); if (endSegment<0) return false; var segment=unEscape(str.substring(0,endSegment), sep); return {segment: segment, remainder: str.substring(endSegment+1)}; }

function skipToEnd(str,sep) { return {segment: str, remainder: ''}; }

function findNext(str, ch) { for (var i=0; i<str.length; ++i) { if (str.charAt(i)=='\\') i+=2; if (str.charAt(i)==ch) return i; } return -1; };

function runOnLoad(f) { if (window.addEventListener) { window.addEventListener("load",f,false); } else if (window.attachEvent) { window.attachEvent("onload",f); } else { window._old_popup_autoedit_onload = window.onload; window.onload = function { window._old_popup_autoedit_onload; f; } } };

window.autoEdit=function { if (window.autoEdit.alreadyRan) return false; window.autoEdit.alreadyRan=true; var cmdString=getParamValue('autoedit'); if (cmdString) { try { var editbox=document.editform.wpTextbox1; } catch (dang) { return; } var cmdList=parseCmd(cmdString); var input=editbox.value; var output=execCmds(input, cmdList); editbox.value=output; }

var summary=getParamValue('autosummary'); if (summary) document.editform.wpSummary.value=summary;

var minor=getParamValue('autominor'); if (minor) { switch (minor) { case '1': case 'yes': case 'true': document.editform.wpMinoredit.checked=true; break; case '0': case 'no': case 'false': document.editform.wpMinoredit.checked=false; } }

var btn=getParamValue('autoclick'); if (btn) { if (document.editform && document.editform[btn]) { var headings=document.getElementsByTagName('h1'); if (headings) { var div=document.createElement('div'); var button=document.editform[btn]; div.innerHTML='The "' + button.value +          '" button has been automatically clicked.' + ' Please wait for the next page to load. '; document.title='('+document.title+')'; headings[0].parentNode.insertBefore(div, headings[0]); button.click; }   } else { alert('autoedit.js\n\nautoclick: could not find button "'+ btn +'".'); }   } };

runOnLoad(autoEdit);

function addlilink(tabs, url, name, id, title, key){ var na = document.createElement('a'); na.href = url; na.appendChild(document.createTextNode(name)); var li = document.createElement('li'); if(id) li.id = id; li.appendChild(na); tabs.appendChild(li); if(id) {       if(key && title) {           ta[id] = [key, title]; }       else if(key) {           ta[id] = [key, '']; }       else if(title) {           ta[id] = ['', title]; }   }    // re-render the title and accesskeys from existing code in wikibits.js    akeytt; return li; }

function addTab(url, name, id, title, key){ var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0]; return addlilink(tabs, url, name, id, title, key) };

function addToolboxLink(url, name, id){ var tb = document.getElementById('p-tb').getElementsByTagName('ul')[0]; addlilink(tb, url, name, id); }

function addNavLink(url, name, id, title, key){ var tb = document.getElementById('p-navigation').getElementsByTagName('ul')[0]; addlilink(tb, url, name, id, title, key); } function doORFUD { var title = document.getElementById('ca-edit').getElementsByTagName('a')[0].href; title = title.substring(title.indexOf('title=') + 6, title.lastIndexOf('&action=edit')); //document.title.substr(0, document.title.lastIndexOf(' - Wikipedia, the free')); location.assign("/w/index.php?title=" + title + "&action=edit&autoedit=s/^/\\n/&autosummary=Tagged%20as%20an%20orphaned%20fair%20use%20image&autoclick=wpSave"); //document.location = "document.title.substr(0, document.title.lastIndexOf(' - Wikipedia, the free') + "?action=edit&autoedit=s/^/\n\n/&autosummary=Tagged%20as%20an%20orphaned%20fair%20use%20image&autoclick=wpSave"; // document.editform.wpTextbox1.value = '{' + '{' + 'subst:orfud' + '}' + '}\n' + document.editform.wpTextbox1.value; //  document.editform.wpSummary.value = 'orfud'; //  document.editform.wpMinoredit.checked = true; //  document.editform.submit; }

function doNSD { //window.location.assign(mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=" + wgPageName + "&action=edit&autoedit=s/^/\\n/&autosummary=This%20image%20%lacks%20%information%20about%its%20source&autoclick=wpSave"); alert("NSD edit: " + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=" + wgPageName + "&action=edit&autoedit=s/^/\\n/&autosummary=This%20image%20%lacks%20%information%20about%its%20source&autoclick=wpSave"); //Find first uploader //var uploaders = getElementsByClass('special',null,'ul'); //user1 = user1[0].getElementsByTagName('a')[2].innerHTML; var uploader = ""; var elements = document.getElementsByTagName('ul'); for (i = 0; i < elements.length; i++) { if(elements[i].className == "special") { var elms2 = elements[i].childNodes; alert(elms2.length / 2 + "uploaders"); alert("top " + elms2[0].childNodes[3].childNodes[0].data); for(y = 2; y < elms2.length; y = y + 2) { alert(y + " " + elms2[y].childNodes[7].childNodes[0].data); }      if(elms2.length > 2) { uploader = elms2[elms2.length - 2].childNodes[7].childNodes[0].data; }      else { uploader = elms2[elms2.length - 2].childNodes[3].childNodes[0].data; }      //uploader = elements[i].childNodes[elements[i].childNodes.length - 1 -1].childNodes[3].childNodes[0].data; //uploader = elements[i].childNodes[elements[i].childNodes.length - 1].childNodes[5].title; //alert("Num childs of special: " + elements[i].childNodes.length + " " + elements[i].childNodes[1].childNodes.length); //alert("Last -2 child: " + elements[i].childNodes[elements[i].childNodes.length - 1 - 1].childNodes.length + " " + elements[i].childNodes[elements[i].childNodes.length - 1 - 1].childNodes[5].title); } }  if(uploader != "") { //window.open(mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=" + "User_talk:" + uploader + "&action=edit&autoedit=s/$/\\n/&autosummary=Image%20source%20" + wgPageName + "&autoclick=wpSave"); alert("Uploader: " + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=" + "User_talk:" + uploader + "&action=edit&autoedit=s/$/\\n/&autosummary=Image%20source%20" + wgPageName + "&autoclick=wpSave"); } else { alert ("No uploader found!"); } }

function addORFUD { addTab("javascript:doORFUD", "orfud", "ca-orfud", "orfud", "s"); akeytt; }

function addNSDs { addTab("javascript:doNSD", "nsd", "ca-nsd", "nsd", "k"); //addTab("javascript:doNLD", "nld", "ca-nld", "nld", "l"); //addTab("javascript:doNSLD", "nsld", "ca-nsld", "nsld", ";"); akeytt; }

$(function { //if (document.title.indexOf("Image:") != -1 && document.title.indexOf("Editing ") != 0) {  if(wgNamespaceNumber == 6 && document.title.indexOf("Editing ") != 0) {    addORFUD;    addNSDs;  }  addNavLink("http://en.wikipedia.org/wiki/Special:Random/image", "Random image", "ca-randomimage", "randimg", "z");  akeytt;  //var pagetitleRe=/[^:]*:\/\/en\.wikipedia\.org\/(wiki\/|w\/index\.php\?title=)([^&?#]*)/;  //ptitle = pagetitleRe.exec(decodeURI(location.href))[2].split('_').join(' ');  //ptitle =  // if this is a user, show the logs for the user rather than the page if( wgNamespaceNumber == 2 || wgNamespaceNumber == 3 ) {  regDropSubpages = /[User|User_talk]:([^&?\/]*)[\/]?.*/;  user = regDropSubpages.exec(wgPageName)[1];  userurl = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=Special%3ALog&user=" +  user;  addlilink(document.getElementById('p-tb').getElementsByTagName('ul')[0], userurl, "User log", "pt-userlogs"); } else if(wgNamespaceNumber == -1) { // don't display link for special pages return; }  url = "http://en.wikipedia.org/w/index.php?title=Special%3ALog&page=" + wgPageName; tabs = document.getElementById('p-tb').getElementsByTagName('ul')[0]; l = addlilink(tabs, url, "Logs", "pt-logs");

//alert(wgTitle);

});

document.write(' '); //