User:Kaldari/mark-disambigs.js

window.markDisambigsGadget = { version: 1, category: 'Category:All disambiguation pages', pageLoaded: false, dataLoaded: false, disambig: {}, uniqueLinks: 0, callbacks: [], linkPrefix: document.location.protocol + "//" + document.location.hostname + mw.config.get( 'wgArticlePath' ).replace( '$1', '' ),

init: function { var that = this;

if ( mw.config.get( 'wgAction' ) != 'submit' ) { jQuery( document ).ready( function {				if ( that.dataLoaded ) {					that.markLinks;				} else {					that.pageLoaded = true;				}			} );

// Fetch links categories using generator var request = { action: 'query', titles: mw.config.get( 'wgPageName' ), prop: 'categories', cllimit: 'max', gpllimit: 'max', generator: 'links' };

this.callApi( request, function( data ) {				that.processResponse( data, request );			} ); } else { // Find all displayed links and then ask for categories jQuery( document ).ready( function {				that.pageLoaded = true;				var titles = {};				var links = mw.util.$content[0].getElementsByTagName( 'a' );

for ( var i = 0; i < links.length; i++ ) { var title = that.extractTitle( links[i].href );

if ( title != null ) { titles[title] = 1; }				}

var t = []; for ( var title in titles ) { t.push( title ); }

var request = { action: 'query', titles: t.join( '|' ), prop: 'categories', cllimit: 'max' };

that.callApi( request, function( data ) {					that.processResponse( data, request );				} ); } );		}	},

extractTitle: function( link ) { if ( link.substring( 0, this.linkPrefix.length ) != this.linkPrefix ) { return null; }

return decodeURIComponent( link.substring( this.linkPrefix.length ).replace( /_/g, ' ' ) ).replace( /#.*$/, '' ); },

callApi: function( request, callback ) { request.format = 'json'; request.requestid = new Date.getTime;

jQuery.post( mw.util.wikiScript( 'api' ), request, callback, 'json' ); },

isDisambig: function( categories ) { for ( var key in categories ) { if ( categories[key].title == this.category ) { return true; }		}		return false; },

processResponse: function( data, request ) { if ( !data.query ) { return; }

for ( var pageid in data.query.pages ) { var page = data.query.pages[pageid]; if ( page.categories && this.isDisambig( page.categories ) ) { if ( this.disambig[page.title] ) continue;

this.disambig[page.title] = true; this.uniqueLinks++; }		}

if ( data['query-continue'] && data['query-continue']['categories'] ) { request['clcontinue'] = data['query-continue']['categories']['clcontinue']; var that = this; this.callApi( request, function( data ) {				that.processResponse( data, request );			} ); } else if ( this.pageLoaded ) { this.markLinks; } else { this.dataLoaded = true; }	},

markLinks: function { this.dataLoaded = true; this.pageLoaded = true;

if ( this.uniqueLinks ) { var links = mw.util.$content[0].getElementsByTagName( 'a' );

for ( var i = 0; i < links.length; i++ ) { var link = links[i]; var title = this.extractTitle( link.href );

if ( title != null && this.disambig[title] ) { if ( link.parentNode && link.parentNode.className.match( /\bdisambig\b/ ) ) { continue; }

jQuery( link ).addClass( 'mw-disambig' ); }			}		}		for ( var i in this.callbacks ) { var callback = this.callbacks[i]; callback( this ); this.callbacks = []; }	},	/**	 * Register a function to be called, when the links were marked. */	addCallback: function( callback ) { if ( this.pageLoaded && this.dataLoaded ) { callback( this ); } else { this.callbacks.push( callback ); }	} };

if ( mw.config.get( 'wgNamespaceNumber' ) >= 0 && jQuery.inArray( mw.config.get( 'wgAction' ), ['submit', 'view', 'purge'] ) > -1 && mw.util.getParamValue( 'printable' ) !== 'yes' ) { markDisambigsGadget.init; }