User:Rezonansowy/mark-disambigs.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Rezonansowy/mark-disambigs. This user script seems to have an accompanying .css page at User:Rezonansowy/mark-disambigs.css. |
window.markDisambigsGadget = {
version: 15,
category: 'Category: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 ) {
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();
}