User:RoyBoy/monobook.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

If a message on your talk page led you here, please be wary of who left it. The code below could contain malicious content capable of compromising your account; if your account appears to be compromised, it will be blocked. If you are unsure whether the code is safe, you can ask at the appropriate village pump.
// Big thanks to User:JesseW for leading me to:
// [[Wikipedia:WikiProject_User_scripts/Techniques#Onload_Structure]]
// and to User:Sasquatch for the if(document.get ... code
// References:
// http://en.wikipedia.org/wiki/MediaWiki:Monobook.js
// User:JesseW/monobook.js --- User:Drini/monobook.js
//
// RoyBoy - Working version done: Nov 2 2005 @ 7:44 PM EST
// Shortens edit, protect, talk and delete tabs
//
// Nov 10 2005: Added contrib and block tab, with default other time
//
// Optimized for 800x600 res. used with CDVF, see at:
// http://www.flickr.com/photos/34345738@N00/62015752/

// <nowiki>

//////////////////////////////////////////
// Tabs by Korath
// returns <li><a href="url">name</a></li>
/////////////////////////////////////////
function addlilink(url, name)
{
  var na = document.createElement('a');
  na.setAttribute('href', url);

  var txt = document.createTextNode(name);
  na.appendChild(txt);

  var li = document.createElement('li');
  li.appendChild(na);
  return li;
}

// appends msg to the currently-editted page, sets the summary to summ,
// and marks or unmarks the Watch this page checkbox according to watch.
function edit_summary_watch(msg, summ, watch)
{
  var f = document.editform, t = f.wpTextbox1;
  if (t.value.length > 0)
    t.value += '\n';
  t.value += msg;
  f.wpSummary.value = summ;
}

function test()
{
  var f = document.editform, t = f.wpTextbox1;
  if (t.value.length > 0)
    t.value += '\n';
  t.value += "{{subst:" + "test" + "}} -" + " " + "~" + "~" + "~" + "~";
  f.wpSummary.value = "test warning";
}

function testn(number)
{
  var page = prompt("Vandalism to which article?")
  var f = document.editform, t = f.wpTextbox1;
  if (t.value.length > 0)
    t.value += '\n';
  t.value += "{{subst:" + "test" + number + "-n|" + page + "}} -" + " " + "~" + "~" + "~" + "~";
  if (t.value > 2)
    f.wpSummary.value = "Vandalism to [[" + page + "]] - warning " + number;
  else   
    f.wpSummary.value = "test [[" + page + "]]";
}

function blankwarn(number)
{
  var page = prompt("Vandalism to which article?")
  var f = document.editform, t = f.wpTextbox1;
  if (t.value.length > 0)
    t.value += '\n';
  t.value += "{{subst:" + "test2a-n|" + page + "}} -" + " " + "~" + "~" + "~" + "~";
  f.wpSummary.value = "t2a [[" + page + "]]";
}

function bvn(number)
{
  var page = prompt("Vandalism to which article?")
  var f = document.editform, t = f.wpTextbox1;
  if (t.value.length > 0)
    t.value += '\n';
  t.value += "{{subst:" + "Blatantvandal|" + page + "}} -" + " " + "~" + "~" + "~" + "~";
  f.wpSummary.value = "bv [[" + page + "]]";
}

// adds various tabs to call the above
function add_tabs()
{
  var c1 = document.getElementById('column-one');
  var tabs = c1.getElementsByTagName('div')[0].getElementsByTagName('ul')[0];

  // Only add contribs tab for "User talk:" but not while Editing
  if (document.title.indexOf("User") != -1 && document.title.indexOf("Editing User talk:"))
    {
      // use the "edit this page" tab to get already-tidied url
      var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
      // cut everything up to "title=" from the start and everything past "&action=edit" from the end
      editlk = editlk.substring(editlk.indexOf('title=') + 6, editlk.lastIndexOf('&action=edit'));
      editlk = editlk.substring(editlk.indexOf(':') + 1);
      var slloc = editlk.indexOf('/');
      if (slloc > 0)
        editlk = editlk.substring(0, slloc);

      tabs.appendChild(addlilink('/wiki/Special:Contributions/' + editlk, 'c'));
  }

  // Only add for pages with "Editing User talk:" somewhere in the title
  if (document.title.indexOf("Editing User talk:") != -1)
    {
      //change move > m, hist > h, pro > p, del > d for more room
      if(document.getElementById('ca-talk'))
        document.getElementById('ca-talk').firstChild.innerHTML = 't';
      if(document.getElementById('ca-edit'))
        document.getElementById('ca-edit').firstChild.innerHTML = 'e';
      if(document.getElementById('ca-move'))
        document.getElementById('ca-move').firstChild.innerHTML = 'm';
      if(document.getElementById('ca-history'))
        document.getElementById('ca-history').firstChild.innerHTML = 'h';
      if(document.getElementById('ca-protect'))
        document.getElementById('ca-protect').firstChild.innerHTML = 'p';
      if(document.getElementById('ca-delete'))
        document.getElementById('ca-delete').firstChild.innerHTML = 'd';

      //change "user page" tab to "anon" to put them in their place, HA!
      if(document.getElementById('ca-nstab-user'))     
        document.getElementById('ca-nstab-user').firstChild.innerHTML = 'anon';

      //tabs.appendChild(addlilink('javascript:edit_summary_watch("{{" + "subst:anon}} -" + " " + "~" + "~" + "~" + "~", "{{" + "anon}}", false, 0)',"Anon"));
      //tabs.appendChild(addlilink('javascript:edit_summary_watch("{{" + "subst:welcome}} -" + " " + "~" + "~" + "~" + "~", "Welcome!", false, 0)',"Welcome!"));
     
      tabs.appendChild(addlilink('javascript:test()',"1"));
      //tabs.appendChild(addlilink('javascript:testn(2)',"t2"));
      tabs.appendChild(addlilink('javascript:blankwarn(2)',"2a"));
      //tabs.appendChild(addlilink('javascript:testn(3)',"t3"));
      //tabs.appendChild(addlilink('javascript:testn(4)',"t4"));

      tabs.appendChild(addlilink('javascript:bvn(1)',"bv"));

      tabs.appendChild(addlilink('javascript:edit_summary_watch(":{{" + "subst:test5}} -" + " " + "~" + "~" + "~" + "~", "Blocked user!", true, 1)',"5"));
    }
	
if (document.title.indexOf("Editing Wikipedia:Articles for deletion") != -1)
    {
        tabs.appendChild(addlilink('javascript:closeafd("keep", "")', 'Keep', ''));
        tabs.appendChild(addlilink('javascript:closeafd("delete", "")', 'Delete', ''));
        tabs.appendChild(addlilink('javascript:closeafd(prompt("Result?"), "")', 'Other', ''));
    }
}

function addEditSection0() {
  ta['ca-edit-0'] = ['', 'Edit the zeroth section of this page'];
  if (!document.getElementById) return;
  x = document.getElementById('ca-edit');
  if(!x) return;
  y = document.createElement('LI');
  y.id = 'ca-edit-0';
  if (x.className == 'selected') {
    if (/&action=edit&section=0$/.test(window.location.href)) {
      x.className = 'istalk';
      y.className = 'selected';
    } else {
      x.className = 'selected istalk';
    }
  } else if (x.className == 'selected istalk') {
    if (/&action=edit&section=0$/.test(window.location.href)) {
      x.className = 'istalk';
      y.className = 'selected istalk';
    } else {
      y.className = 'istalk';
    }
  } else {
    y.className = x.className;
    x.className = 'istalk';
  }
  z = document.createElement('A');
  if (x.children) {
    z.href = x.children[0].href + '&section=0';
    z.appendChild(document.createTextNode('0'));
    y.appendChild(z);
    document.getElementById('p-cactions').children[1].children[0].insertBefore(y,x.nextSibling);
  } else {
    z.href = x.childNodes[0].href + '&section=0';
    z.appendChild(document.createTextNode('0'));
    y.appendChild(z);
    document.getElementById('p-cactions').childNodes[3].childNodes[1].insertBefore(y,x.nextSibling);
  }
}

function shortTabs() {
  if(document.getElementById('ca-edit'))
    document.getElementById('ca-edit').firstChild.innerHTML = 'edit';

  if(document.getElementById('ca-history'))
    document.getElementById('ca-history').firstChild.innerHTML = 'hist';

  if(document.getElementById('ca-protect'))
    document.getElementById('ca-protect').firstChild.innerHTML = 'pro';

  if(document.getElementById('ca-unprotect'))
    document.getElementById('ca-unprotect').firstChild.innerHTML = 'unp';

  if(document.getElementById('ca-talk'))
    document.getElementById('ca-talk').firstChild.innerHTML = 'talk';

  if(document.getElementById('ca-delete'))
    document.getElementById('ca-delete').firstChild.innerHTML = 'del';

  if(document.getElementById('ca-unwatch'))
    document.getElementById('ca-unwatch').firstChild.innerHTML = 'unw';

  if(document.getElementById('ca-watch'))
    document.getElementById('ca-watch').firstChild.innerHTML = 'w';

  if(document.getElementById('ca-move'))
    document.getElementById('ca-move').firstChild.innerHTML = 'm';
}

// Adds a "blocklog" tab and fills in the username field on Special:Blockip, if a "&faketarget=username" is present.
function do_blockip_stuff()
{
  // focus on Reason field
  document.getElementsByName('wpBlockReason')[0].focus();

  // Look for a &faketarget= for the username/ip
  var l = location.search.substring(1).split('&');
  var target = '';
  for (var i = 0; i < l.length; ++i)
    {
      var n = l[i].indexOf('=');
      if (l[i].substring(0, n) == 'faketarget')
	{
	  target = l[i].substring(n + 1);
	  break;
	}
    }

  if (target == '')
    return;

  // put account name in "IP Address/username" field
  var addr = document.getElementsByName('wpBlockAddress')[0];
  addr.value = unescape(target);

  // insert a default ban time
  var defaultTime = document.getElementsByName('wpBlockOther')[0];
  defaultTime.value = "33 hours";

  // add "blocklog" tab
  var c1 = document.getElementById('column-one');
  var tabs = c1.getElementsByTagName('div')[0].getElementsByTagName('ul')[0];
  tabs.appendChild(addlilink('/w/index.php?title=Special%3ALog&type=block&user=&page=User%3A' + target, 'blocklog'));
}

// Adds "block" and "blocklog" tabs to User: and User talk: pages.
function add_block_tab()
{
  var c1 = document.getElementById('column-one');
  var tabs = c1.getElementsByTagName('div')[0].getElementsByTagName('ul')[0];

  // use the "edit this page" tab to get already-tidied url
  var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
  // cut everything up to "title=" from the start and everything past "&action=edit" from the end
  editlk = editlk.substring(editlk.indexOf('title=') + 6, editlk.lastIndexOf('&action=edit'));
  editlk = editlk.substring(editlk.indexOf(':') + 1);
  var slloc = editlk.indexOf('/');
  if (slloc > 0)
    editlk = editlk.substring(0, slloc);

  // add "block" tab
  tabs.appendChild(addlilink('/w/index.php?title=Special%3ABlockip&faketarget=' + editlk, 'b'));
  // add count tab
  tabs.appendChild(addlilink('http://tools.wikimedia.de/~interiot/cgi-bin/Tool1/wannabe_kate?username=' + editlk + '&site=en.wikipedia.org', 'cnt'));

  // To open the block page and block log simultaneously, replace the above line with:
  // tabs.appendChild(addlilink('javascript:blockpage_and_log("' + editlk + '")', 'b'));
  // and uncomment the blockpage_and_log() function above.

  // add "blocklog" tab
  //tabs.appendChild(addlilink('/w/index.php?title=Special%3ALog&type=block&user=&page=User%3A' + editlk, 'bl'));

}

//  Add a "Kate" link to your monobook "personal menu" list at the very
//  top of the page.
//
//  Indicate where you would like "Kate" to appear:
//    pt-userpage, pt-mytalk, pt-preferences,
//    pt-watchlist, pt-mycontris, pt-logout
//

function addlilink2(tabs, url, name, id)
{
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    return li;
}

function addPortletLink(portlet, href, text, id, tooltip, accesskey, nextnode) {
	var node = document.getElementById(portlet);
	if ( !node ) return null;
	node = node.getElementsByTagName( "ul" )[0];
	if ( !node ) return null;

	var link = document.createElement( "a" );
	link.appendChild( document.createTextNode( text ) );
	link.href = href;

	var item = document.createElement( "li" );
	item.appendChild( link );
	if ( id ) item.id = id;

	if ( accesskey ) {
		link.setAttribute( "accesskey", accesskey );
		tooltip += " ["+accesskey+"]";
	}
	if ( tooltip ) {
		link.setAttribute( "title", tooltip );
	}
	if ( accesskey && tooltip ) {
		updateTooltipAccessKeys( new Array( link ) );
	}

	if ( nextnode && nextnode.parentNode == node )
		node.insertBefore( item, nextnode );
	else
		node.appendChild( item );  // IE compatibility (?)

	return item;
}

function addToolBoxLinks()
{
    // use the "edit this page" tab to get user name
    var user = document.getElementById('ca-edit').getElementsByTagName('ul')[0].href;
    // cut everything up to "title=" from the start and everything past "&action=edit" from the end
    user = user.substring(user.indexOf('title=') + 6, user.lastIndexOf('&action=edit'));

    var url = 'http://countervandalism.org/Count.php?username=' + user + '&submit=Count';
    var name = 'Count';
    var id = 'toolbox_count';

    //var tb = document.getElementById('p-navigation').getElementsByTagName('ul')[0];
    addPortletLink('p-tb', url, name, id);
}
 
gsKateInsertBefore = 'pt-mycontris'; // leave blank to append after "logout"

function KateLink()
  {
    // use the "edit this page" tab to get user name
    var user = document.getElementById('ca-edit').getElementsByTagName('ul')[0].href;
    // cut everything up to "title=" from the start and everything past "&action=edit" from the end
    user = user.substring(user.indexOf('title=') + 6, user.lastIndexOf('&action=edit'));
    user = user.substring(user.indexOf(':') + 1);
  
    var li = document.createElement( 'li' );
    li.id = 'ca-editcount';
  
    //not-working
    //var a = document.createElement( 'a' );
    //a.appendChild( document.createTextNode( 'count' ) ); // eh, the css makes the text lowercase
    //a.href = 'http://tools.wikimedia.de/~interiot/cgi-bin/contribution_tree?user=' + user;

    //working but changes to wikicode forces a change
    //var a = document.createElement( 'a' );
    //a.appendChild( document.createTextNode( 'count' ) ); // eh, the css makes the text lowercase
    //a.href = 'http://en.wikipedia.org/wiki/User:Interiot/Tool2/code.js?username=' + user;

    var a = document.createElement( 'a' );
    a.appendChild( document.createTextNode( 'count' ) ); // eh, the css makes the text lowercase
    a.href = 'http://countervandalism.org/Count.php?username=' + user + '&submit=Count';

    li.appendChild( a );
  
    a.appendChild(document.createTextNode('ec'));
    y.appendChild(z);
    document.getElementById('p-cactions').childNodes[3].childNodes[1].insertBefore(y,x.nextSibling);

    //if ( ! gsKateInsertBefore ) // append to end (right) of list
    //{
    //   document.getElementById( 'pt-logout' ).parentNode.appendChild( li );
    //}
    //else
    //{
    //    var before = document.getElementById( gsKateInsertBefore );
    //    before.appendChild( li, before );
    //}
  }

// ***** Window Load ******

window.onload = Main;

function Main()
{
  shortTabs();
  add_tabs();

  //add section 0 if not Editing a User talk page
  if (document.title.indexOf("Editing User talk:") != 0)
    addEditSection0();

  //block tab on all user pages
  if (document.title.indexOf("User") != -1)
    add_block_tab(); 

  //block ip stuff if on Block user page
  if (document.title.indexOf("Block user") != -1)
    do_blockip_stuff();

  if (document.title.indexOf("User") != -1)
    addToolBoxLinks(); //KateLink(); //count menu option on user pages
}

// </nowiki>

// Filter changes live
// [[User:Lupin/recent2.js]] - please include this line
document.write('<script type="text/javascript" src="' 
             + 'http://en.wikipedia.org/w/index.php?title=User:Lupin/recent2.js' 
             + '&action=raw&ctype=text/javascript&dontcountme=s"></script>');