User:Yair rand/HighlightUncreated.js

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.
mw.hook( 'wikipage.content' ).add( function ( content ) {
	var namespaces = mw.config.get( 'wgFormattedNamespaces' ),
		links = {},
		pages = [],
		selector = 'a' + [
			'[href^="#"]',
			'.external',
			'.extiw',
			'.image',
			'.internal',
			'.mw-redirect',
			'.mw-userlink',
			'.mw-usertoollinks-talk',
			'.mw-file-description',
			'.new',
			// DiscussionTools extension
			'.ext-discussiontools-init-replylink-reply',
			'.ext-discussiontools-init-timestamplink',
			// ProofreadPage extension
			'.quality0',
			'.quality1',
			'.quality2',
			'.quality3',
			'.quality4',
		].map( function ( c ) {
			return ':not(' + c + ')';
		} ).join( '' );
	$( content )
	.find( selector )
	.each( function () {
		try {
			var title = decodeURIComponent( this.href.split( '/wiki/' )[1].split( '#' )[0].replace( /_/g, ' ' ) );
			if ( title.indexOf( ':' ) !== -1 ) {
				for ( var i = -1; i < 16; i += 2 ) {
					if ( title.indexOf( namespaces[ i ] + ':' ) === 0 ) {
						return;
					}
				}
			}
			if ( links[ title ] ) {
				links[ title ].push( this );
			} else {
				links[ title ] = [ this ];
				pages.push( title );
			}
		} catch (e) { }
	} );
	for( ; pages.length; ) {
		$.ajax( {
			url: '//www.wikidata.org/w/api.php',
			data: {
				'format': 'json',
				'action': 'wbgetentities',
				'sites': mw.config.get( 'wgDBname' ),
				'titles': pages.splice( 0, 50 ).join( '|' ),
				'props': 'info',
			},
			dataType: 'jsonp',
			success: function ( data ) {
				if ( data.success ) {
					for ( var ent = data.entities, i = -1; ent[ i ]; i-- ) {
						var plinks = links[ ent[ i ].title ];
						if ( !plinks ) { continue; }
						for ( var ii = plinks.length; ii-- ; ) {
							plinks[ ii ].style.backgroundColor = '#E0E0FF';
						}
					}
				}
			}
		} );
	}
} );