User:Incnis Mrsi/short-section-links.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
var ARROW_HEADING = /^→(.+)/;

function ProcessAElements (comments, blue) {
    for(var i = 0, n = comments.length; i < n; i++) {
        var el = comments[i];
        var ch = el.childNodes;
        if (!ch) { ch = el.children; }
        var Match;
        for (var j = 0; j < ch.length; j++) {
    	    if (
        	    (ch[j].tagName == 'A')
        	&&	( Match = ARROW_HEADING.exec(ch[j].innerHTML) )
	        ) {
    	    	var autoc = document.createElement( "span" );
        		autoc.setAttribute('class', "autocomment" );
        		autoc.innerHTML = Match[1];
        		el.insertBefore(autoc, ch[j+1]);
        		ch[j].innerHTML = '→';
        		if ( blue ) {  // fighting the new—December 4—MediaWiki perversion
        			var pare = ch[j].parentNode;
//        			ch[j].setAttribute( 'class', "" );
        			if ( pare.getAttribute('class') == "autocomment" ) {
        				// go out of the container
        				pare.parentNode.insertBefore(ch[j], pare);
        			};
        		};
        	}
        }
    }
};

function ClassicalSectionLinks () {
    ProcessAElements( document.getElementsByClassName( "autocomment" ), 1 );
    ProcessAElements( document.getElementsByClassName( "comment" ), 0 );
};

$( document ).on( 'load', ClassicalSectionLinks );
mw.hook( 'wikipage.content' ).add( ClassicalSectionLinks );