User:Ricordisamoa/AlreadySection.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.
/* <nowiki>
* Warns users trying to create a new section with an already-used title
* @author [[User:Ricordisamoa]]
*/
$( function () {
if ( mw.config.get( 'wgAction' ) !== 'edit' || mw.util.getParamValue( 'section' ) !== 'new' ) {
return;
}
mw.loader.using( 'jquery.tipsy', function () {
new mw.Api().get( {
action: 'query',
format: 'json',
formatversion: 2,
titles: mw.config.get( 'wgPageName' ),
prop: 'revisions',
rvlimit: 1,
rvprop: 'content'
} )
.done( function ( d ) {
d = d.query.pages;
if ( d === undefined || d.length !== 1 ) {
return;
}
d = d[0];
if ( d.revisions === undefined || d.revisions.length !== 1 ) {
return;
}
d = d.revisions[0].content;
if ( d === undefined ) {
return;
}
var i18n = {
en: 'The section title you entered already $1is on the page$2. Would you like to change it?',
it: 'Il titolo di sezione che hai inserito $1esiste già nella pagina$2. Vorresti cambiarlo?'
};
mw.messages.set( 'AlreadySection', i18n[mw.config.get( 'wgUserLanguage' )] || i18n.en );
var encodeSection = function ( title ) {
return mw.util.wikiUrlencode( title.replace( /\[\[(.+\|)?(.+)\]\]/g, '$2' ) ).replace( /\%/g, '.' ).replace( /\//g, '.2F' );
};
var sections = $.map(
d.match( /(^|\n)\=+\s*[^\n]*\S+[^\n]*\s*\=+(\n|$)/g ),
function ( e ) {
return encodeSection( e.trim().replace( /^\=+|\=+$/g, '' ).trim() );
}
);
if ( sections.length === 0 ) {
return;
}
$( '#wpSummary' ).on( 'input', function () {
if ( sections.indexOf( encodeSection( this.value ) ) !== -1 ) {
var href = mw.util.getUrl( mw.config.get( 'wgPageName' ) ) + '#' + encodeSection( this.value );
$( this ).attr( {
title: mw.message( 'AlreadySection', '<a href="' + href + '">', '</a>' ).text()
} ).tipsy( {
title: 'title',
html: true,
gravity: 'w',
fade: true,
trigger: 'manual'
} ).tipsy( 'show' );
} else {
try {
$( this ).tipsy( 'hide' );
} catch ( e ) {
}
}
} );
} );
} );
} );