MediaWiki:Gadget-SimilarTitles.js: differenze tra le versioni
m (Inlineo utilizzo della config, sono solo 4 utilizzi) |
m (una versione importata) |
(Nessuna differenza)
|
Versione attuale delle 22:40, 30 ago 2020
/**
* Similar titles
*
* When creating a new page, it shows a list of similar titles, to avoid duplicates.
*
* @author [[w:it:Utente:Valerio Bozzolan]] and contributors
* @license [[GNU GPL v3+]] or [[CC BY SA 4.0]] at your opinion
*/
window.similarTitlesOpts = {
// Where to put the tip
container: '#contenitore-titoli-simili', // Vedi [[Template:Pagina inesistente]]
// Header of the tip
preContainer: "<p><b>Verifica però che le seguenti voci non siano quello che stavi cercando:</b></p>",
// Footer of the tip
postContainer: "",
// Max length of the page quote
maxQuote: 100,
// How many similar pages to be shown
maxResults: 5
};
$( function () {
if (
'edit' !== mw.config.get( 'wgAction' ) ||
0 !== mw.config.get( 'wgNamespaceNumber' ) ||
0 !== mw.config.get( 'wgArticleId' )
) {
return;
}
var data = {
action: 'opensearch',
search: mw.config.get( 'wgPageName' ),
profile: 'normal',
limit: window.similarTitlesOpts.maxResults
};
mw.loader.using( 'mediawiki.api' )
.done( function () {
( new mw.Api() ).get( data )
.done( function ( results ) {
var s = results[0];
var titles = results[1];
var quotes = results[2];
var urls = results[3];
if( titles.length ) {
$container = $( window.similarTitlesOpts.container );
$container.append( window.similarTitlesOpts.preContainer );
for( var i = 0; i < titles.length; i++ ) {
var title = titles[ i ];
var quote = quotes[ i ];
var url = urls [ i ];
$p = $('<p>').append(
$('<a>').attr( 'href', url )
.text( title )
.attr( 'target', '_blank' )
);
if( quote ) {
var quoteLimit = window.similarTitlesOpts.maxQuote;
if( quote.length > quoteLimit ) {
quote = quote.substring( 0, quoteLimit) + '…';
}
$p.append( ' – «' + quote + '»' );
}
$container.append( $p );
}
$container.append( window.similarTitlesOpts.postContainer );
}
} )
.fail( function ( e ) {
mw.log.error( 'SimilarTitles: errore nella chiamata API: ' + e );
} );
} )
.fail( function () {
mw.log.error( 'Impossibile avviare l\'accessorio SimilarTitles.' );
} );
} );