Jump to content

Wikipedia:WikiProject User scripts/Scripts/Compact Navigation

From Wikipedia, the free encyclopedia
// See [[Wikipedia talk:WikiProject User scripts/Scripts/Compact Navigation|Talk page]] for instructions<br>
// Also, please add [[:Wikipedia:WikiProject User scripts/Scripts/Compact Navigation]] as a comment to allow tracking of number of users <syntaxhighlight lang="Javascript">

function getElementUrl(id) {
    if (id) {
       return id.firstChild.getAttribute('href', 2);
    }
}

function getElementUrlText(id) {
    if (id) {
       return id.firstChild.innerHTML;
    }
}

if (mw.config.get('wgNamespaceNumber') >= 0)
addOnloadHook(function() {
    var pCactions = document.getElementById('p-cactions');

    var tabs = pCactions.getElementsByTagName('ul')[0];

    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');

    // Hide tabs
    if (caMain) caMain.style.display = 'none';
    if (caEdit) caEdit.style.display = 'none';
    if (caTalk) caTalk.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';

    // Create new tabs
    var newPage = addlimenu(tabs, getElementUrlText(caMain), 'new-page', getElementUrl(caMain), caTalk);
    newPage.className = caMain.className;
    var newTalk = addlimenu(tabs, getElementUrlText(caTalk), 'new-talk', getElementUrl(caTalk), caTalk);
    newTalk.className = caTalk.className;
    var newLog = addlimenu(tabs, 'actions', 'new-actions', getElementUrl(caHistory), caMove);

    // Check if page is editable
    if ( caEdit ) {
        edittxt = 'edit';
    } else {
        edittxt = 'view source';
    }

    // Actions menu items
    addPortletLink('new-actions', 'javascript:;', '');
    addPortletLink('new-actions', getElementUrl(caEdit), edittxt);
    if (caHistory) addPortletLink('new-actions', getElementUrl(caHistory), 'history');
    addPortletLink('new-actions', mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=Special:Log&page=' + mw.config.get('wgPageName'), 'page log');
    if (caMove) addPortletLink('new-actions', getElementUrl(caMove), 'move');
    if (caProtect) addPortletLink('new-actions', getElementUrl(caProtect), 'protect');
    if (caDelete) addPortletLink('new-actions', getElementUrl(caDelete), 'delete');

    // Add menu items for main page
    if( caMain.className.search('new') == 0 ) {
        addPortletLink('new-page', 'javascript:;', '');
        addPortletLink('new-page', getElementUrl(caMain), 'create');
    } else {
        addPortletLink('new-page', 'javascript:;', '');
        addPortletLink('new-page', getElementUrl(caMain) + '?action=edit', 'edit');
        addPortletLink('new-page', getElementUrl(caMain) + '?action=edit&section=0', edittxt + ' lead');
        addPortletLink('new-page', getElementUrl(caMain) + '?action=history', 'history');
        addPortletLink('new-page', getElementUrl(caMain) + '?diff=cur&oldid=prev', 'last');
    }
    addPortletLink('new-page', mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=Special:Log&page=' + mw.config.get('wgPageName'), 'page log');
    
    // If we're in Userspace then add additional items to show info
    // for this user
    if (mw.config.get('wgNamespaceNumber') == 2 || mw.config.get('wgNamespaceNumber') == 3) {
        if ( mw.config.get('wgTitle').indexOf("/") == -1 ) {
            uname = mw.config.get('wgTitle');
        } else {
            uname = mw.config.get('wgTitle').substring(0, mw.config.get('wgTitle').indexOf("/"));
        }
        addPortletLink('new-page', mw.config.get('wgServer') + "/wiki" + '/Special:Contributions/' + uname, 'contribs');
        if (caDelete) addPortletLink('new-page', mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=Special:DeletedContributions&target=' + uname, 'deleted contribs');
        addPortletLink('new-page', mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=Special:Log&user=' + uname, 'user log');
        addPortletLink('new-page', mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=Special:Log&type=block&page=User:' + uname, 'block log');
        if (caDelete) addPortletLink('new-page', mw.config.get('wgServer') + "/wiki" + '/Special:Blockip/' + uname, 'block');
    }

    // Add menu items for talk page
    if( caTalk.className.search('new') == 0 ) {
        addPortletLink('new-talk', 'javascript:;', '');
        addPortletLink('new-talk', getElementUrl(caTalk), 'create');
    } else {
        addPortletLink('new-talk', 'javascript:;', '');
        addPortletLink('new-talk', getElementUrl(caTalk) + '?action=edit', 'edit');
        addPortletLink('new-talk', getElementUrl(caTalk) + '?action=edit&section=0', 'edit lead');
        addPortletLink('new-talk', getElementUrl(caTalk) + '?action=edit&section=new', 'new section');
        addPortletLink('new-talk', getElementUrl(caTalk) + '?action=history', 'history');
        addPortletLink('new-talk', getElementUrl(caTalk) + '?diff=cur&oldid=prev', 'last');
    }
    addPortletLink('new-talk', mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=Special:Log&page=' + mw.config.get('wgPageName'), 'page log');
});

// </syntaxhighlight>