Korisnik:Aca/Preslov.js
Napomena: Nakon objave možda ćete trebate očistiti međuspremnik (keš) svog preglednika kako biste vidjeli promjene.
- Firefox / Safari: držite Shift i pritisnite Reload, ili pritisnite bilo Ctrl-F5 ili Ctrl-R (Command-R na Macu)
- Google Chrome: pritisnite Ctrl-Shift-R (Command-Shift-R na Macu)
- Internet Explorer / Edge: držite Ctrl i kliknite Refresh, ili pritisnite Ctrl-F5
- Opera: pritisnite Ctrl-F5.
// <nowiki>
( function () {
var wmFullText = 'Preslovljivač će pretvoriti celu sadržinu stranice u latinicu. Želite li zaista da nastavite?',
wmTalkPage = 'Preslovljivač neće obraditi celu sadržinu stranice za razgovor.\n\nBiće obrađena samo poruka koju istaknete.';
window.wfPlugins = window.wfPlugins || [];
window.wfPluginsT = window.wfPluginsT || [];
// Function takes an input or text as an argument. If it is absent, it uses $( '#wpTextbox1' )
// as an input.
window.Wikify = function ( inputOrText ) {
'use strict';
var text, isInput, $input, caretPosition, textScroll,
txt = '',
hidden = [],
winScroll = document.documentElement.scrollTop,
$CodeMirrorVscrollbar = $( '.CodeMirror-vscrollbar' );
// Check what's in the first parameter
if ( typeof inputOrText === 'string' ) {
text = inputOrText;
} else {
isInput = ( inputOrText &&
( ( inputOrText.nodeType && inputOrText.value !== undefined ) || // node
( inputOrText.prop && inputOrText.prop( 'nodeType' ) ) // jQuery object
)
);
$input = $( isInput ? inputOrText : '#wpTextbox1' );
}
// FUNCTIONS
function r( r1, r2 ) {
txt = txt.replaceAll( r1, r2 );
}
function processLink( link, left, right ) {
left = left.replace( /[ _\u00A0]+/g, ' ' ).trim();
if ( left.match( /^(?:Kategorija|Datoteka) ?:/ ) ) {
return '[[' + left + '|' + right + ']]';
}
right = right.replace( / {2,}/g, ' ' ).trim();
var inLink = right.substr( 0, left.length );
var afterLink = right.substr( left.length );
var uniLeft = left.substr( 0, 1 ).toUpperCase() + left.substr( 1 );
var uniRight = ( right.substr( 0, 1 ).toUpperCase() + right.substr( 1 ) ).replace( /[_\u00A0]/g, ' ' );
if ( uniRight.indexOf( uniLeft ) === 0 && afterLink.match( /^[a-zа-яё]*$/ ) ) {
return '[[' + inLink + ']]' + afterLink;
} else {
return '[[' + left + '|' + right + ']]';
}
}
function processText() {
var i,
u = '\u00A0'; // non-breaking space
if ( mw.config.get( 'wgNamespaceNumber' ) % 2 || mw.config.get( 'wgNamespaceNumber' ) === 4 ) { // is talk page
var sigs = txt.match( /\d\d:\d\d, \d\d? \S{3,8} 20\d\d \(UTC\)/g );
if ( sigs && sigs.length > 1 ) {
alert( wmTalkPage );
return;
}
}
r( 'а', 'a' );
r( 'б', 'b' );
r( 'в', 'v' );
r( 'г', 'g' );
r( 'д', 'd' );
r( 'ђ', 'đ' );
r( 'е', 'e' );
r( 'ж', 'ž' );
r( 'з', 'z' );
r( 'и', 'i' );
r( 'ј', 'j' );
r( 'к', 'k' );
r( 'л', 'l' );
r( 'љ', 'lj' );
r( 'м', 'm' );
r( 'н', 'n' );
r( 'њ', 'nj' );
r( 'о', 'o' );
r( 'п', 'p' );
r( 'р', 'r' );
r( 'с', 's' );
r( 'т', 't' );
r( 'ћ', 'ć' );
r( 'у', 'u' );
r( 'ф', 'f' );
r( 'х', 'h' );
r( 'ц', 'c' );
r( 'ч', 'č' );
r( 'џ', 'dž' );
r( 'ш', 'š' );
r( 'А', 'A' );
r( 'Б', 'B' );
r( 'В', 'V' );
r( 'Г', 'G' );
r( 'Д', 'D' );
r( 'Ђ', 'Đ' );
r( 'Е', 'E' );
r( 'Ж', 'Ž' );
r( 'З', 'Z' );
r( 'И', 'I' );
r( 'Ј', 'J' );
r( 'К', 'K' );
r( 'Л', 'L' );
r( 'Љ', 'Lj' );
r( 'М', 'M' );
r( 'Н', 'N' );
r( 'Њ', 'Nj' );
r( 'О', 'O' );
r( 'П', 'P' );
r( 'Р', 'R' );
r( 'С', 'S' );
r( 'Т', 'T' );
r( 'Ћ', 'Ć' );
r( 'У', 'U' );
r( 'Ф', 'F' );
r( 'Х', 'H' );
r( 'Ц', 'C' );
r( 'Ч', 'Č' );
r( 'Џ', 'Dž' );
r( 'Ш', 'Š' );
}
function processAllText() {
txt = $input ? $input.textSelection( 'getContents' ) : text;
processText();
if ( $input ) {
r( /^[\n\r]+/g, '' );
// 2017 wikitext editor adds an empty line to the end with every text replacement
// Remove the following block when [[phab:T198010]] is fixed.
if ( window.ve && ve.init && ve.init.target && ve.init.target.active ) {
r( /[\n\r]+$/g, '' );
}
$input.textSelection( 'setContents', txt );
if ( caretPosition ) {
$input.textSelection( 'setSelection', {
start: caretPosition[0] > txt.length ? txt.length : caretPosition[0]
} );
}
} else {
text = txt;
}
if ( window.auto_comment &&
window.insertSummary &&
!document.editform.wpSection.value
) {
window.insertSummary( 'preslovljivač' );
}
}
// MAIN CODE
if ( $input ) {
$input.focus();
caretPosition = $input.textSelection( 'getCaretPosition', { startAndEnd: true } );
if ( caretPosition ) {
textScroll = ( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input )
.scrollTop();
if ( caretPosition[0] === caretPosition[1] ) {
processAllText();
} else {
txt = $input.textSelection( 'getSelection' );
processText();
// replaceSelection doesn't work with MediaWiki 1.30 in case this gadget is loaded
// from other wiki
$input.textSelection( 'encapsulateSelection', {
replace: true,
peri: txt
} );
// In CodeMirror, the selection isn't preserved, so we do it explicitly
$input.textSelection( 'setSelection', {
start: caretPosition[0],
end: caretPosition[0] + txt.length
} );
}
( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input )
.scrollTop( textScroll );
// If something went wrong
} else if ( confirm( wmFullText ) ) {
processAllText();
}
} else {
processAllText();
return text;
}
// scroll back, for 2017 wikitext editor, IE, Opera
document.documentElement.scrollTop = winScroll;
};
function registerWikificatorTool() {
registerTool( {
name: 'wikificator',
position: 100,
title: 'Preslovljivač',
label: 'Preslovljivač – automatski preslovljivač stranica',
callback: Wikify,
classic: {
icon: '//upload.wikimedia.org/wikipedia/commons/0/06/Wikify-toolbutton.png',
},
visual: {
icon: '//upload.wikimedia.org/wikipedia/commons/thumb/4/41/Wikificator_VE_icon.svg/20px-Wikificator_VE_icon.svg.png',
modes: [ 'source' ],
addRightAway: true,
},
} );
}
if ( mw.config.get( 'wgServerName' ) === 'ru.wikipedia.org' ) {
registerWikificatorTool();
} else {
$.when(
mw.loader.using( [ 'mediawiki.util', 'user.options' ] ),
$.getScript( 'https://ru.wikipedia.org/w/index.php?title=MediaWiki:Gadget-registerTool.js&action=raw&ctype=text/javascript' )
).done( registerWikificatorTool );
}
}() );
// </nowiki>