User:Fred Gandt/globalWatchlist.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.
( function () {
	function FetchProjects() {
		var req;
		if ( window.XMLHttpRequest ) {
			req = new XMLHttpRequest();
		} else {
			req = new ActiveXObject( "Microsoft.XMLHTTP" );
		}
		req.onreadystatechange = function() {
			if ( ( req.readyState == 4 ) && ( req.status == 200 ) ) {
				var str = req.responseText,
					pos = str.indexOf( "wikitable sortable mw-centralauth-wikislist" ) + 43;
				str = str.substr( pos );
				pos = str.indexOf( "<tbody>" );
				str = str.substr( pos );
				pos = str.indexOf( "</tbody>" );
				str = str.substr( 0, pos );
				MakeList( str );
			}
		}
		req.open( "GET", "/wiki/Special:CentralAuth/" + mw.config.get( "wgUserName" ), true );
		req.send();
	}
	
	function MakeList( s ) {
		var link,
			pos = 0,
			project,
			ul = document.getElementById( "fg-global-watchlists" );
		while ( ( pos = s.indexOf( "<tr>" ) ) != -1 ) {
			s = s.substr( pos + 4 );
			pos = s.indexOf( "href=" ) + 6;
			s = s.substr( pos );
			pos = s.indexOf( "\" " );
			link = s.substr( 0, pos );
			pos = link.lastIndexOf( "/" );
			link = link.substr( 0, pos );
			pos = link.lastIndexOf( "." );
			project = link.substr( 2, ( pos -2 ) );
			ul.innerHTML += "<li><a href=\"" + link + "/Special:Watchlist\" title=\"" + link + "/Special:Watchlist\">" + project + "</a></li>";
		}
	}
	
	function Init() {
		if ( mw.config.get( "wgPageName" ) == "Special:Watchlist" ) {
			var left_nav_panel = document.getElementById( "mw-panel" ),
				nav_list_wrap = document.createElement( "div" );
			nav_list_wrap.setAttribute( "class", "portal expanded" );
			left_nav_panel.appendChild( nav_list_wrap );
			nav_list_wrap.innerHTML = "<h5>Watchlists</h5><div class=\"body\" style=\"display:block;\"><ul id=\"fg-global-watchlists\"></ul></div>";
			FetchProjects();
		}
	}
	
	jQuery( document ).ready( Init );
} () );