Jump to content

User:Vildricianus/monobook.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <pre><nowiki>

// unwatch links

addOnloadHook(function () {
    var query_prefix = "title=Special:Watchlist&action=submit&remove=1&id[]=";
    //var query_prefix = "action=unwatch&title=";

    if (window.location.href.indexOf("Special:Watchlist") < 0) return;
    if (window.location.href.indexOf("Special:Watchlist/edit") >= 0) return;

    var links = document.getElementById('content').getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
        if (links[i].href.substring(links[i].href.length-15) != '&action=history')
            continue;
        var unwatch = document.createElement('a');
        unwatch.href = "/w/index.php?" + query_prefix + encodeURIComponent(links[i].title);
        unwatch.title = "Unwatch "+links[i].title;
        unwatch.appendChild(document.createTextNode("unwatch"));
        links[i].parentNode.insertBefore(unwatch, links[i].nextSibling);
        // kluge to handle case where "diff" is unlinked:
        var delim = links[i].previousSibling;
        delim = (delim.nodeType == 3 ? delim.nodeValue : "");
        links[i].parentNode.insertBefore(document.createTextNode(delim.replace(/^.*diff/, "")), unwatch);
    }
});


// Add date and time to your monobook "personal menu" list at the very top of the page.
// Created by [[User:Mathwiz2020]]

// Indicate where you would like the time to appear:
// 1 is first (before username), 2 is second (before talk link), ... 7 is last (after log out link)
insertBeforeNum = 7;

// Do NOT edit below this line unless you're experiened in javascript
insertBeforeArr = new Array("","pt-userpage","pt-mytalk","pt-preferences","pt-watchlist","pt-mycontris","pt-logout","");
insertBefore = insertBeforeArr[insertBeforeNum];

function makeTime()
{
  var li = document.createElement( 'li' );
  li.id = 'pt-time';

  var mySpan = document.createElement( 'span' );
  mySpan.appendChild( document.createTextNode( 'date and time' ) );

  li.appendChild( mySpan );

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

  getTime();
}

if      ( window.addEventListener ) window.addEventListener ( 'load', makeTime, false );
else if ( window.attachEvent      ) window.attachEvent      ( 'onload', makeTime      );

function getTime()
{
    var time    = new Date();
    var date    = time.getUTCDate();
    var months  = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
        month   = months[time.getUTCMonth()];
    var year    = time.getUTCFullYear();
    var hours   = '0' + time.getUTCHours();
        hours   = hours.substr(hours.length-2, hours.length);
    var minutes = '0' + time.getUTCMinutes();
        minutes = minutes.substr(minutes.length-2, minutes.length);
    var seconds = '0' + time.getUTCSeconds();
        seconds = seconds.substr(seconds.length-2, seconds.length);
    var curTime = hours + ":" + minutes + ":" + seconds + ", " + date + " " + month + " " + year + " (UTC)";
    datePlace   = document.getElementById('pt-time').childNodes[0].childNodes[0];
                  datePlace.replaceData(0, datePlace.length, curTime);
    doTime      = window.setTimeout("getTime()", 1000);
}

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

// add LI link
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;
}

// addLink
function addLink(where, url, name, id, title, key, after){
    //* where is the id of the toolbar where the button should be added;
    //   i.e. one of "p-cactions", "p-personal", "p-navigation", or "p-tb".
    //
    //* url is the URL which will be called when the button is clicked.
    //   javascript: urls can be used to do more complex things.
    //
    //* name is what will appear as the name of the button.
    //
    //* id is the id of the button; it's best to define one.  
    //   Use a prefix to make sure its unique. Optional.
    //
    //* title is the tooltip title that gives a longer description 
    //   of the button; if you define a accesskey, mention it here. Optional.
    //
    //* key is the char you want for the accesskey. Optional.
    //
    //* after is the id of the button you want to follow this one. Optional.
    //
    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);
    var tabs = document.getElementById(where).getElementsByTagName('ul')[0];
    if(after) {
	tabs.insertBefore(li,document.getElementById(after));
    } else {
	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;
}

// addPurge
addOnloadHook( function (){
    var x = document.getElementById('ca-history');
    if(!x) return;
    if(x.children) x = x.children[0].href;
    else x = x.childNodes[0].href;
    addLink("p-cactions", x.replace(/=history/, "=purge"), 'purge', 'ca-purge', 'Purge the internal cache for this page', 0);
});

// addRawText
addOnloadHook( function (){
    var x = document.getElementById('ca-history');
    if(!x) return;
    if(x.children) x = x.children[0].href;
    else x = x.childNodes[0].href;
    addLink("p-cactions", x.replace(/=history/, "=raw"), 'raw', 'ca-raw', 'Raw text for this page', 0);
});

// addLastDiff
addOnloadHook(function () {
    z=document.getElementById("content").childNodes;
    for (var n=0;n<z.length;n++) { 
      if (z[n].className=="firstHeading") {
        var pname=z[n].textContent;
      }
    }
    var l=addTab("http://en.wikipedia.org/w/index.php?title=" + pname + "&diff=cur&oldid=prev", 'last', '');
    l.lastChild.title="Show most recent diff";
});


// adds a 'logs for this page' link to the navigation bar
// if the page is a user's page, talk page or subpage, the link will go to logs for the user instead
// if the page is a special page, then no link is displayed

addOnloadHook(function () {

 // get page title
 var pagetitleRe=/[^:]*:\/\/en\.wikipedia\.org\/(wiki\/|w\/index\.php\?title=)([^&?#]*)/;
 ptitle = pagetitleRe.exec(decodeURI(location.href))[2].split('_').join(' ');
 
 // if this is a user, show the logs for the user rather than the page
 if( (window.location.href.indexOf("User:") != -1) || (window.location.href.indexOf("User_talk:") != -1) ) {
  regDropSubpages = /[User|User_talk]:([^&?\/]*)[\/]?.*/;
  user = regDropSubpages.exec(ptitle)[1];
  url = "http://en.wikipedia.org/w/index.php?title=Special%3ALog&user=" + user;
 } else if(window.location.href.indexOf("Special:") != -1) {
  // don't display link for special pages
  return;
 } else {
  url = "http://en.wikipedia.org/w/index.php?title=Special%3ALog&page=" + ptitle;
 }
 
 tabs = document.getElementById('p-tb').getElementsByTagName('ul')[0];
 l = addlilink(tabs, url, "Logs", "pt-logs");

});

// Google
addOnloadHook(function () {  // add onload handler using code from wikibits.js
    var title;
    if (!(title = document.getElementById('t-whatlinkshere') )) return;
    if (!(title = title.getElementsByTagName('a')[0] )) return;
    if (!(title = title.href )) return;
    if (!(title = title.replace(/^.*title=Special:Whatlinkshere&target=/, '') )) return;
    if (!(title = title.replace(/^(Talk|User|Wikipedia|Image|MediaWiki|Template|Help|Category|Portal)(_talk)?:/, '') )) return;
    if (!(title = title.replace(/("|%22)/g, '') )) return;
    if (!(title = title.replace(/_/g, '%20') )) return;

 // Google
    addLink('p-tb', 'http://www.google.com/search?hl=en&q=%22'+title+'%22',
            'Google search', 't-googlesearch', 'Search Google for "'+decodeURIComponent(title)+'"', '', null);
});


// EDIT SECTION 0
 function addEditSection0() {
  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].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].insertBefore(y,x.nextSibling);
   }
 }

 if (document.title.indexOf("Editing ") == -1) {
  addOnloadHook(addEditSection0);
 }


// PERSONAL TOOLBAR EXPANSION - from Mike
 function addTools() {
  var myprefs = document.getElementById('pt-preferences'); 
  var newpt;
  var newa;

  newpt = document.createElement('li');
  newa = document.createElement('a');
  newa.href='/wiki/User:Vildricianus/monobook.css' ;
  newa.appendChild(document.createTextNode('css'));
  newpt.appendChild(newa);
  myprefs.parentNode.insertBefore(newpt, myprefs);

  newpt = document.createElement('li');
  newa = document.createElement('a');
  newa.href='/wiki/User:Vildricianus/monobook.js' ;
  newa.appendChild(document.createTextNode('js'));
  newpt.appendChild(newa);
  myprefs.parentNode.insertBefore(newpt, myprefs);

  newpt = document.createElement('li');
  newa = document.createElement('a');
  newa.href='/wiki/WP:SB' ;
  newa.appendChild(document.createTextNode('sb'));
  newpt.appendChild(newa);
  myprefs.parentNode.insertBefore(newpt, myprefs);

  newpt = document.createElement('li');
  newa = document.createElement('a');
  newa.href='/wiki/Special:Recentchanges' ;
  newa.appendChild(document.createTextNode('rc'));
  newpt.appendChild(newa);
  myprefs.parentNode.insertBefore(newpt, myprefs);

  newpt = document.createElement('li');
  newa = document.createElement('a');
  newa.href='/wiki/Special:Randompage' ;
  newa.appendChild(document.createTextNode('rand'));
  newpt.appendChild(newa);
  myprefs.parentNode.insertBefore(newpt, myprefs);

}

// EDITBAR EXPANSION - from [email protected]

 window.AddMyOwnButton=function(tbar, caption, href, imgsrc) {
   var ba = document.createElement('a');
   ba.href = href;
   var img = document.createElement('img');
   img.src = imgsrc;
   img.alt = caption;
   ba.appendChild(img);
   tbar.appendChild(ba);
 }

 window.myOwnEditButtons=function() {
  var tbdiv=document.getElementById('toolbar');
  if ( tbdiv ) {
    tbdiv.appendChild(document.createTextNode(' '));

    // {{,}}
    AddMyOwnButton(tbdiv, 'txt2predef', 'javascript:insertTags(\'{{\',\'}}\',\'\');', 'http://republico.estv.ipv.pt/~nmct/wikipedia/icons/button_topredef.png');

    // [[wikt:,|]]
    AddMyOwnButton(tbdiv, 'pedia', 'javascript:insertTags(\'[[wikt:\',\'|]]\',\'\');', 'http://upload.wikimedia.org/wikipedia/commons/7/7f/Button_link_to_Wikipedia.png');

    // <tt>,</tt>
    AddMyOwnButton(tbdiv, '<tt></tt>', 'javascript:insertTags(\'<tt>\',\'</tt>\',\'\');', 'http://republico.estv.ipv.pt/~nmct/wikipedia/icons/button_tt.png');

    // <small>
    AddMyOwnButton(tbdiv, '<small></small>', 'javascript:insertTags(\'<small>\',\'</small>\',\'\');', 'http://republico.estv.ipv.pt/~nmct/wikipedia/icons/button_small.png');

    // <strike>
    AddMyOwnButton(tbdiv, '<s></s>', 'javascript:insertTags(\'<s>\',\'</s>\',\'\');', 'http://upload.wikimedia.org/wikipedia/en/c/c9/Button_strike.png');

    // <includeonly>
    AddMyOwnButton(tbdiv, '<includeonly></includeonly>', 'javascript:insertTags(\'<includeonly>\',\'</includeonly>\',\'\');', 'http://users.skynet.be/fb242895/Button_includeonly.PNG');

    // <noinclude>
    AddMyOwnButton(tbdiv, '<noinclude></noinclude>', 'javascript:insertTags(\'<noinclude>\',\'</noinclude>\',\'\');', 'http://users.skynet.be/fb242895/Button_noinclude.PNG');

    // [[Category:,]]
    AddMyOwnButton(tbdiv, 'category', 'javascript:insertTags(\'[\[Category:\',\']]\',\'\');', 'http://upload.wikimedia.org/wikipedia/commons/1/11/Button_category.png');

  }
}


// NAVBOX EXPANSION - from Connel
 // add "favourites" to navigation box
 function addToNav() {
  var pagetitle = document.getElementById('p-cactions').getElementsByTagName('ul').item(0).getElementsByTagName('li').item(0).firstChild.href;

  var lastnav = document.getElementById('n-sitesupport');
  var newnav;
  var newa;
  var tidyname = document.getElementById('ca-history').getElementsByTagName('a')[0].href;
  tidyname = tidyname.substring(tidyname.indexOf('title=') + 6, tidyname.lastIndexOf('&action=history'));
  tidyname = tidyname.substring(tidyname.indexOf(':') + 1);
  var slloc = tidyname.indexOf('/');
  if(slloc > 0) tidyname = tidyname.substring(0, slloc);

 // get page title
  var pagetitle = document.getElementById('content').getElementsByTagName('h1').item(0).firstChild.nodeValue;
  if (pagetitle.search(/Search/) != -1) {
    pagetitle = document.getElementById('contentSub').firstChild.nodeValue;
    pagetitle = pagetitle.replace(/For query /g, "");
    pagetitle = pagetitle.replace(/\"/g, "");
  }
  pagetitle = pagetitle.replace(/Editing /g, "");
  var pageexternal = pagetitle.replace(/\ /g, "_");
  var pageext =      pagetitle.replace(/\ /g, "+");

 // All pages starting from this page
 newnav = document.createElement('li');
 newa = document.createElement('a');
 newa.href='/wiki/Wikipedia:Template messages';
 newa.appendChild(document.createTextNode('Templates'));
 newnav.appendChild(newa);
 lastnav.parentNode.appendChild(newnav);

 // Interiot's What links here?
 newnav = document.createElement('li');
 newa = document.createElement('a');
 newa.href='http://tools.wikimedia.de/~interiot/cgi-bin/queries/whatlinkshere?article=' + pagetitle + '&dbname=enwiki';
 newa.appendChild(document.createTextNode('What links here?'));
 newnav.appendChild(newa);
 lastnav.parentNode.appendChild(newnav);

 // All pages starting from this page
 newnav = document.createElement('li');
 newa = document.createElement('a');
 newa.href='/wiki/Special:Allpages/' + pagetitle;
 newa.appendChild(document.createTextNode('All pages'));
 newnav.appendChild(newa);
 lastnav.parentNode.appendChild(newnav);

 // Listusers
 newnav = document.createElement('li');
 newa = document.createElement('a');
 newa.href='/w/index.php?title=Special:Listusers&username=' + tidyname;
 newa.appendChild(document.createTextNode('List users'));
 newnav.appendChild(newa);
 lastnav.parentNode.appendChild(newnav);

 // Editcountitis
 newnav = document.createElement('li');
 newa = document.createElement('a');
 newa.href='http://en.wikipedia.org/wiki/User:Interiot/Tool2/code.js?username=' + tidyname;
 newa.appendChild(document.createTextNode('Edit count'));
 newnav.appendChild(newa);
 lastnav.parentNode.appendChild(newnav);

 // Wikt
 newnav = document.createElement('li');
 newa = document.createElement('a');
 newa.href='http://en.wiktionary.org/wiki/Special:Search/' + pagetitle;
 newa.appendChild(document.createTextNode('Wiktionary'));
 newnav.appendChild(newa);
 lastnav.parentNode.appendChild(newnav);

}

function reformatMyPage() {
   addTools();
   addToNav();
}

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

myAddOnload(myOwnEditButtons);
myAddOnload(reformatMyPage);


//Interiot's javascript edit counter
if (document.title.indexOf('User:Interiot/Tool2/code.js') != -1) {
  document.write('<scr' + 'ipt type="text/javascript" src="' 
    + 'http://en.wikipedia.org/w/index.php?title=User:Interiot/Tool2/code.js' 
    + '&action=raw&ctype=text/javascript&dontcountme=s"></scr'
             + 'ipt>'); }

// </nowiki></pre>