MediaWiki:Gadget-mark-disambigs.js

Z Wikiźródeł, wolnej biblioteki

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
window.markDisambigsGadget = {
	version: 15,
	category: 'Kategoria:Strony ujednoznaczniające',

	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 ) {
					if ( titles.hasOwnProperty( title ) ) {
						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.hasOwnProperty( key ) ) {
				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 ) {
			if ( data.query.pages.hasOwnProperty( pageid ) ) {
				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' );
			this.disambig['Wikipedia:Strona ujednoznaczniająca'] = false;

			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.text == 'inne znaczenia tego określenia' ) {
						continue;
					}

					if ( link.parentNode && link.parentNode.className.match( /\bdisambig\b/ ) ) {
						continue;
					}

					jQuery( link ).addClass( 'mw-disambig' );
				}
			}
		}
		for ( var i in this.callbacks ) {
			if ( this.callbacks.hasOwnProperty( i ) ) {
				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();
}