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.
// Go secure
//if( mw.config.get('wgServer') != 'https://secure.wikimedia.org' ){
//    if( mw.config.get('wgServer') == 'http://en.wikipedia.org' ){
//        window.location = 'https://secure.wikimedia.org/wikipedia/en' + window.location.pathname //+ window.location.search;
//    }
//}

mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Happy-melon/global.css&action=raw&ctype=text/css', 'text/css');

//Useful scripts
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Anomie/linkclassifier.js&action=raw&ctype=text/js'); // Linkback: [[User:Anomie/linkclassifier.js]]
importStylesheetURI('//en.wikipedia.org/w/index.php?title=User:Happy-melon/linkclassifier.css&action=raw&ctype=text/css'); //Linkback: [[User:Anomie/linkclassifier.css]]

mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Happy-melon/previewtemplatelastmod.js&action=raw&ctype=text/css'); // Linkback: [[User:Anomie/previewtemplatelastmod.js]]

//Extra links in the sidebar
addOnloadHook( function(){
    if (mw.config.get('wgNamespaceNumber') >= 0){
    mw.util.addPortletLink('p-tb', 
                   mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=Special:Log&page='+mw.config.get('wgPageName'),
                   'Page logs', 't-pagelog', 
                   'View logs for this page', '', 
                   document.getElementById('t-upload'))
    }
    if ( mw.config.get('wgNamespaceNumber') == 2 || mw.config.get('wgNamespaceNumber') == 3 ){
    mw.util.addPortletLink('p-tb', 
                   mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=Special:Listusers&limit=1&username='+mw.config.get('wgTitle').split('/')[0],
                   'User rights', 't-userrights', 
                   'View rights for this user', '', 
                   document.getElementById('t-upload'))
    }

    if (mw.config.get('wgNamespaceNumber') > 0){
    mw.util.addPortletLink('p-tb', 
                   mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=Special:Prefixindex/'+mw.config.get('wgPageName')+'/&limit=100',
                   'Subpages', 't-prefix', 
                   'View subpages of this page', '', 
                   document.getElementById('t-upload'))
    }

    mw.util.addPortletLink('p-navigation', 
                   'https://bugzilla.wikimedia.org',
                   'Bugzilla', 'n-bugzilla', 
                   'Go to Bugzilla', '', 
                   document.getElementById('n-currentevents'));
    mw.util.addPortletLink('p-navigation', 
                   'http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/',
                   'SVN', 'n-svn',  
                   'Browse SVN', '',  
                   document.getElementById('n-currentevents'));

    mw.util.addPortletLink('p-navigation', 
                   mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=User:Happy-melon/vector.css',
                   'Vector.css', 'n-vectorcss', 
                   'My vector.css', '', 
                   document.getElementById('n-currentevents'));
    mw.util.addPortletLink('p-navigation', 
                   mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=User:Happy-melon/vector.js',
                   'Vector.js', 'n-vectorjs', 
                   'My vector.js', '', 
                   document.getElementById('n-currentevents'));
    if( mw.config.get('wgTitle') == 'Happy-melon/vector.js' || mw.config.get('wgTitle') == 'Happy-melon/vector .css' ) {
        var js  = document.getElementById('n-vectorjs').getElementsByTagName('a')[0]
        var css = document.getElementById('n-vectorcss').getElementsByTagName('a')[0]
        js.setAttribute( 'href', mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=User:Happy-melon/global.js');
        css.setAttribute('href', mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=User:Happy-melon/global.css');
        js.innerHTML  = 'Global.js';
        css.innerHTML = 'Global.css';
    }
    if( document.getElementById('n-randompage') ){
        document.getElementById('n-randompage')
                .parentNode.insertBefore(
                       document.getElementById('n-recentchanges'),
                       document.getElementById('n-randompage')
        );
    }
})


/* Hide things in the edit window */

if (mw.config.get('wgAction') == "edit"){
    function cleanEditWindow(){
        document.getElementById('siteSub').style.cssText = 'display: none';
    }
    addOnloadHook(cleanEditWindow);
}

function testBorders () {
    if (document.getElementById('bodyContent').className=="css-borders") {
        document.getElementById('bodyContent').className=("");
    } else {
        document.getElementById('bodyContent').className=("css-borders")
    }
}

//Add a 'null edit' tab
if(mw.config.get('wgNamespaceNumber') != -1 && mw.config.get('wgArticleId') != 0) { 
    addOnloadHook( function() {
        mw.util.addPortletLink('p-cactions', 
                       mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&action=edit&nulledit=true',
                       'null',
                       'p-null',
                       'Null edit this page');
    }
)}
if( mw.config.get('wgAction') == 'edit' && inurl('nulledit') == 'true') {
    addOnloadHook( function() {
        document.getElementById('wpSave').click();
    }
)}
 
function inurl(p) {
    var re = RegExp('[&?]' + p + '=([^&]*)');
    var matches;
    if (matches = re.exec(document.location)) {
        try { 
            return decodeURI(matches[1]);
        } catch (e) { }
    }
    return null;
}

//Don't prompt for an edit summary when a preloaded one is given
if( mw.config.get('wgAction') == 'edit' && inurl('summary') ) {
    addOnloadHook( function() {
        var tag   = document.createElement("input")
        tag.type  = "hidden";
        tag.value = "1";
        tag.name  = "wpIgnoreBlankSummary";
        summary = document.getElementById('wpSummary');
        summary.parentNode.insertBefore(tag, summary.nextSibling);
    }
)}

//Add a "select all" button to advanced search
if (mw.config.get('wgCanonicalSpecialPageName') == "Search" ) { addOnloadHook(function () {
    var advsearch = document.getElementById("powersearch").getElementsByTagName("fieldset")[0];
    var nsbuttons = advsearch.getElementsByTagName("input");

    var newbuttons = document.createElement("p");
    newbuttons.style.clear = "both";
    advsearch.insertBefore( newbuttons, advsearch.getElementsByTagName("hr")[0] );

    var toggleAllButton      = document.createElement("input");
    toggleAllButton.type     = "checkbox";
    toggleAllButton.value    = "0";
    toggleAllButton.id       = "mw-search-selectall";
    toggleAllButton.onchange  = function () {
        if( toggleAllButton.checked ) {
            setting = true;
            toggleAllLabel.innerHTML = "Unselect all";
        } else {
            setting = false;
            toggleAllLabel.innerHTML = "Select all";
        }
        for( var i = 0; i < nsbuttons.length; i++ ) {
            var check = nsbuttons[i];
            if( check.type != "checkbox" || check.id.search("mw-search-ns") == -1 ) {
                continue;
            }
            check.checked = setting;
        }
    };

    var toggleAllLabel       = document.createElement("label");
    toggleAllLabel.setAttribute( "for" , "mw-search-selectall");
    toggleAllLabel.innerHTML = "Select all";

    newbuttons.appendChild( toggleAllButton );
    newbuttons.appendChild( toggleAllLabel  );
})}

//Add class to #bodyContent when it's an editconflict
if( mw.config.get('wgAction') == 'submit' ){ addOnloadHook(function(){
    if( document.getElementById('editconflict-notice') ||
        document.getElementById('firstHeading').innerHTML.match(/^Edit conflict/)
      ){
        var c = document.getElementById('bodyContent').className +" editconflict";
        document.getElementById('bodyContent').className = c;
    }
})}

//Stop me editing pages I've blacklisted myself from.
//Blacklist is loaded from GreaseMonkey for privacy, which then removes this banner.
if( mw.config.get('wgAction') == 'edit'){ addOnloadHook( function(){
    var warn = document.createElement("div");
    warn.setAttribute("class","fmbox fmbox-warning error");
    warn.setAttribute("id","nogreasemonkey-warning");
    warn.innerHTML = "The BlacklistPages script has not loaded.  Be careful not to edit blacklisted pages!" ;
    document.getElementById('bodyContent').insertBefore( 
        warn , 
        document.getElementById('bodyContent').childNodes[2]
    );
})}

//Change the "edit" link on the diff screen to edit the section if possible.
var el_req = new XMLHttpRequest();
var el_section = '';
var el_link = '';
if( mw.config.get('wgAction') == 'view' ){ addOnloadHook( function(){
    if( !document.getElementsByClassName('diff') || document.getElementById('differences-nextlink') ) return;

    try{
        var bar = document.getElementById('mw-diff-ntitle1');
        var links = bar.getElementsByTagName('a');
        for( var i=0 ; i<links.length ; i++ ){
            if( links[i].innerHTML == 'edit' ){
                el_link = links[i];
            }
        }
        if( !el_link ) return;

        var summary = document.getElementById('mw-diff-ntitle3')
                              .getElementsByClassName('comment')[0];
        var autosummary = summary.getElementsByClassName('autocomment')[0];
        el_section = autosummary.childNodes[1].nodeValue.slice(0,-2);

        el_req.onreadystatechange = doEditLinkChange;
        el_req.open( "GET", 
                  mw.config.get('wgServer') + '/w/index.php?action=view&title=' + mw.config.get('wgPageName'), 
                  true);
        el_req.send(null);

    } catch(err) {
        return;
    }
})}

function doEditLinkChange() {
    if( el_req.readyState == 4 ) {
        var parser = new DOMParser();
        var page = parser.parseFromString( el_req.responseText, "text/xml" );
        var headers = [];
        for( var i=2 ; i<=5 ; i++ ){
            var newheaders = page.getElementsByTagName( 'h'+String(i) )
            for( var j=0 ; j<newheaders.length ; j++ ){
                 headers.push(newheaders[j]);
            }
        }
        for( var i=0 ; i<headers.length ; i++ ){
            try{
                var title = headers[i].getElementsByClassName('mw-headline')[0].innerHTML;
                if( title == el_section ){
                    var link2 = headers[i].getElementsByClassName('editsection')[0]
                                          .getElementsByTagName('a')[0];
                    var target = link2.href;
                    var regex = /&section=(\d+)/;
                    var sectionnumber = regex.exec(target)[1];
                    el_link.setAttribute('href', el_link.getAttribute('href')+'&section='+sectionnumber);
                    break;
                }
            } catch(err) {
                continue;
            }
        }
    }
}