User talk:Bob/script/addlimenu.js

Overview

edit

This script gives you the ability to add on-click drop-down menus to the p-cactions (tabs) in the monobook skin.

Usage

edit

Importing the scripts

edit

You'll need to import the library by adding the following to your monobook.js:

importScript(User:Bob/script/addlimenu.js);

Also, some CSS is needed. Add the following to your monobook.css:

@import "http://en.wikipedia.org/w/index.php?title=User:Bob/script/addlimenu.css&action=raw&ctype=text/css";

Adding a menu

edit
 
An LI menu in action

Adding a menu is easy — use the function addLiMenu(), which accepts the arguments tabText and position, and returns the <li> (tab) element on which items will be attached. The second argument is optional.

To add items to the menu, use the addPortletLink() function:

var menu = addLiMenu('this is a menu');
addPortletLink(menu.id, "http://en.wikipedia.org/wiki/Main_Page", "Main Page");
addPortletLink(menu.id, "http://en.wikipedia.org/wiki/Talk:Main_Page", "Main Page Talk");

Useful scripts

edit

Get page name and talk page name

edit

Sets the non-talk page to the variable thisPage, and the talk page to thisTalk, and the current one of the two to currentPage.

var namespaces = new Array("",           "Talk:",           "User:",     "User_talk:",
                           "Wikipedia:", "Wikipedia_talk:", "Image:",    "Image_talk:",
                           "MediaWiki:", "MediaWiki_talk:", "Template:", "Template_talk:",
                           "Help:",      "Help_talk:",      "Category:", "Category_talk:",
                           "Portal:",    "Portal_talk:");

/* set the page and talk page names for use throughout the page */
if(wgNamespaceNumber % 2 == 0) { // if it's not a talk page
  var thisPage = namespaces[wgNamespaceNumber] + wgTitle;
  var thisTalk = namespaces[wgNamespaceNumber + 1] + wgTitle;
  thisPage = encodeURIComponent(thisPage);
  thisTalk = encodeURIComponent(thisTalk);
  var currentPage = thisPage;
} else {
  var thisPage = namespaces[wgNamespaceNumber - 1] + wgTitle;
  var thisTalk = namespaces[wgNamespaceNumber] + wgTitle;
  thisPage = encodeURIComponent(thisPage);
  thisTalk = encodeURIComponent(thisTalk);
  var currentPage = thisTalk;
}

Page/talk page menus

edit
   var pCactions = document.getElementById('p-cactions');

   var caMain = pCactions.getElementsByTagName('li')[0];
   var caTalk = pCactions.getElementsByTagName('li')[1];
   var caEdit = document.getElementById('ca-edit');
   var caView = document.getElementById('ca-viewsource');
   var caHistory = document.getElementById('ca-history');
   var caMove = document.getElementById('ca-move');
   var caWatch = document.getElementById('ca-watch');
   var caUnwatch = document.getElementById('ca-unwatch');
   var caProtect = document.getElementById('ca-protect');
   var caDelete = document.getElementById('ca-delete');
   var caAdd = document.getElementById('ca-addsection');

   var editText = (caView) ? 'view source' : 'edit';

   // Hide tabs
   if (caMain) caMain.style.display = 'none';
   if (caEdit) caEdit.style.display = 'none';
   if (caTalk) caTalk.style.display = 'none';
   if (caView) caView.style.display = 'none';
   if (caHistory) caHistory.style.display = 'none';
   if (caMove)    caMove.style.display = 'none';
   if (caProtect) caProtect.style.display = 'none';
   if (caDelete)  caDelete.style.display = 'none';
   if (caAdd)     caAdd.style.display = 'none';

   if (!caWatch) caWatch = caUnwatch;

   var pageText = getElementUrlText(caMain);
   var talkText = 'discussion'; // getElementUrlText(caTalk);

   /* page menu */
   var newPage = addLiMenu(pageText,caWatch);

   if( caMain.className.search('new') == 0 ) {
       addPortletLink(newPage.id,wgServer+wgScript+'?title='+thisPage+'&action=edit','create');
       newPage.className += " red";
       newPage.firstChild.nextSibling.className += " red";
   } else {
       addPortletLink(newPage.id,wgServer+wgScript+'?title='+thisPage,'view '+pageText);
       addPortletLink(newPage.id,wgServer+wgScript+'?title='+thisPage+'&action=edit',editText);
       addPortletLink(newPage.id,wgServer+wgScript+'?title='+thisPage+'&action=history','history');
       addPortletLink(newPage.id,wgServer+wgScript+'?diff=cur&oldid=prev&title='+thisPage,'last');
       if (caMove)    addPortletLink(newPage.id,wgServer+'/wiki/Special:MovePage/'+ thisPage, 'move');
       if (caProtect) addPortletLink(newPage.id,wgServer+wgScript+'?title='+thisPage+'&action=protect', 'protect');
       if (caDelete)  addPortletLink(newPage.id,wgServer+wgScript+'?title='+thisPage+'&action=delete', 'delete');
   }
   addPortletLink(newPage.id, wgServer + wgScript + '?title=Special:Log&page=' + thisPage, 'page log');
   if(caMain.className.search(/selected/i) != -1) newPage.className += ' selected';

   /* talk menu */
   var newTalk = addLiMenu(talkText,caWatch);

   if( caTalk.className.search('new') == 0 ) {
       addPortletLink(newTalk.id,wgServer+wgScript+'?title='+thisTalk+'&action=edit','create');
       newTalk.className += " red";
       newTalk.firstChild.nextSibling.className += " red";
   } else {
       addPortletLink(newTalk.id,wgServer+wgScript+'?title='+thisTalk,'view '+talkText);
       addPortletLink(newTalk.id,wgServer+wgScript+'?title='+thisTalk+'&action=edit','edit');
       addPortletLink(newTalk.id,wgServer+wgScript+'?title='+thisTalk+'&action=edit&section=new','add comment');
       addPortletLink(newTalk.id,wgServer+wgScript+'?title='+thisTalk+'&action=history','history');
       addPortletLink(newTalk.id,wgServer+wgScript+'?diff=cur&oldid=prev&title='+thisTalk,'last');
       if (caMove)    addPortletLink(newTalk.id,wgServer+'/wiki/Special:MovePage/'+ thisTalk, 'move');
       if (caProtect) addPortletLink(newTalk.id,wgServer+wgScript+'?title='+thisTalk+'&action=protect', 'protect');
       if (caDelete)  addPortletLink(newTalk.id,wgServer+wgScript+'?title='+thisTalk+'&action=delete', 'delete');
   }
   addPortletLink(newTalk.id, wgServer + wgScript + '?title=Special:Log&page=' + thisTalk, 'page log');
   if(caTalk.className.search(/selected/i) != -1) newTalk.className += ' selected';

User info

edit

Creates a 'user info' menu on user/user talk pages.

    /* if we're in the userspace, add more info */
    if (wgNamespaceNumber == 2 || wgNamespaceNumber == 3) {
      if ( wgTitle.indexOf("/") == -1 ) {
        var uname = wgTitle;
      } else {
        var uname = wgTitle.substring(0, wgTitle.indexOf("/"));
      }
 
      var userMenu = addLiMenu('user info');
        addPortletLink(userMenu.id,wgServer+wgScript+'?title=Special:PrefixIndex&from='+uname+'/&namespace=2', 'userspace');
        addPortletLink(userMenu.id, 'http://tools.wikimedia.de/~interiot/cgi-bin/Tool1/wannabe_kate?site=en.wikipedia.org&username='+uname, 'edit count');
        addPortletLink(userMenu.id,wgServer+'/wiki/Special:Contributions/'+uname,'contribs');
        addPortletLink(userMenu.id,wgServer+wgScript+'?title=Special:DeletedContributions&target='+uname,'deleted contribs');
        addPortletLink(userMenu.id, wgServer+wgScript+'?title=Special:Log&user='+uname,'user log');
        addPortletLink(userMenu.id, wgServer+wgScript+'?title=Special:Log&type=block&user='+uname,'block log');
        if (caDelete) addPortletLink(userMenu.id,wgServer+"/wiki"+'/Special:Blockip/'+uname,'block');
    }

Compatibility

edit

Browsers

edit

Currently this script properly works only in Firefox. It's a bit buggy in Safari, but fails epically in Internet Explorer.

Skins

edit

This script is currently only for the monobook skin, but its compatibility may be extended in the future (and/or upon request).