User:Khanson/translit ru en.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:Khanson/translit ru en. |
//Транслитерация выделенного текста из латиницы в кириллицу.
//Часть кода позаимствована из http://ru.wikipedia.org/wiki/MediaWiki:Wikificator.js
//Часть кода позаимствована X-romix
//Автор: Khanson
var Khanson_Translit_CantWork = 'Translit cannot work in your browser'
var Khanson_Translit_FullText = 'Эта кнопка предназначена для транслитерации текста. Выберите фрагмент текста. Заглавные символы Ь и Ъ кодируются ~ и ~~ соответственно, подсказка по остальным символам будет выведена внизу окна.';
//Добавляет кнопку
function addTranslitButton(){
var toolbar = document.getElementById('toolbar')
var textbox = document.getElementById('wpTextbox1')
if (!textbox || !toolbar) return
var i = document.createElement('img')
i.src = 'http://upload.wikimedia.org/wikipedia/ru/8/85/Button-translit-ru-en.png'
i.alt = i.title = 'To latin'
i.onclick = Khanson_Translit
i.style.cursor = 'pointer'
toolbar.appendChild(i)
}
//Этот код выполняется в начале.
if (wgAction == 'edit' || wgAction == 'submit'){
addOnloadHook(addTranslitButton)
}
//Функция для оформления таблицы
function Khanson_Translit(){
//Проверяем, поддерживает ли браузер регулярные выражения (RegExp)
if (('code'.replace(/d/g, 'r') != 'core')
|| (navigator.appName=='Netscape' && navigator.appVersion.substr (0, 1) < 5))
{ alert(Khanson_CantWork); return }
var txt, hidden = [], hidIdx = 0, wpTextbox1 = document.editform.wpTextbox1
var winScroll = document.documentElement.scrollTop //remember window scroll
wpTextbox1.focus()
if (typeof wpTextbox1.selectionStart != 'undefined'
&& (navigator.productSub > 20031000 || is_safari)) { //Mozilla/Opera/Safari3
var textScroll = wpTextbox1.scrollTop
var startPos = wpTextbox1.selectionStart
var endPos = wpTextbox1.selectionEnd
txt = wpTextbox1.value.substring(startPos, endPos)
if (txt == '') {alert(Khanson_FullText); ShowHelp(); return}
else{
processText()
wpTextbox1.value = wpTextbox1.value.substring(0, startPos) + txt + wpTextbox1.value.substring(endPos)
}
wpTextbox1.selectionStart = startPos
wpTextbox1.selectionEnd = startPos + txt.length
wpTextbox1.scrollTop = textScroll
}else if (document.selection && document.selection.createRange) { //IE
//alert("IE");
var range = document.selection.createRange()
txt = range.text
if (txt == '') {alert(Khanson_FullText); ShowHelp(); return}
else{
processText()
range.text = txt
//if (!window.opera) txt = txt.replace(/\r/g,'')
if (range.moveStart) range.moveStart('character', - txt.length)
range.select()
}
}else // Для браузеров, которые не умеют возвращать выделенный фрагмент, выдаем ошибку
{ alert(Khanson_CantWork); return }
document.documentElement.scrollTop = winScroll // scroll back, for IE/Opera
//Здесь производим замену в переменной txt - это отразится на выделенном фрагменте текста
function processText(){
//txt = txt.replace(/^\s+|\s+$/g, '') //Обрезаем пробелы слева и справа
//txt = txt.replace(/\n/g, '\n|-\n| ') //Концы строк
//a b v g d e yo zh z i j k l m n o p r s t u f kh ts ch sh shch `` y ` e yu ya
txt = txt.replace(/Ъ/g, '~~');
txt = txt.replace(/Ь/g, '~');
txt = txt.replace(/щ/g, 'sh');
txt = txt.replace(/Щ/g, 'Shch');
txt = txt.replace(/ее/g, 'eye');
txt = txt.replace(/ий/g, 'y');
txt = txt.replace(/ый/g, 'y');
txt = txt.replace(/ое/g, 'oye');
txt = txt.replace(/ьe/g, 'yе');
txt = txt.replace(/ья/g, 'ya');
txt = txt.replace(/ьё/g, 'yo');
txt = txt.replace(/ью/g, 'yu');
txt = txt.replace(/ъe/g, 'yе');
txt = txt.replace(/ъя/g, 'ya');
txt = txt.replace(/ё/g, 'yo');
txt = txt.replace(/ж/g, 'zh');
txt = txt.replace(/ц/g, 'ts');
txt = txt.replace(/ч/g, 'ch');
txt = txt.replace(/ш/g, 'sh');
txt = txt.replace(/ъ/g, '``');
txt = txt.replace(/ы/g, 'y');
txt = txt.replace(/э/g, 'e');
txt = txt.replace(/ю/g, 'yu');
txt = txt.replace(/я/g, 'ya');
txt = txt.replace(/Ё/g, 'Yo');
txt = txt.replace(/Ж/g, 'Zh');
txt = txt.replace(/Ц/g, 'Ts');
txt = txt.replace(/Ч/g, 'Ch');
txt = txt.replace(/Ш/g, 'Sh');
txt = txt.replace(/Ы/g, 'Y`');
txt = txt.replace(/Э/g, 'E');
txt = txt.replace(/Ю/g, 'Yu');
txt = txt.replace(/Я/g, 'Ya');
txt = txt.replace(/а/g, 'a');
txt = txt.replace(/б/g, 'b');
txt = txt.replace(/в/g, 'v');
txt = txt.replace(/г/g, 'g');
txt = txt.replace(/д/g, 'd');
txt = txt.replace(/е/g, 'e');
txt = txt.replace(/з/g, 'z');
txt = txt.replace(/и/g, 'i');
txt = txt.replace(/й/g, 'y');
txt = txt.replace(/к/g, 'k');
txt = txt.replace(/л/g, 'l');
txt = txt.replace(/м/g, 'm');
txt = txt.replace(/н/g, 'n');
txt = txt.replace(/о/g, 'o');
txt = txt.replace(/п/g, 'p');
txt = txt.replace(/р/g, 'r');
txt = txt.replace(/с/g, 's');
txt = txt.replace(/т/g, 't');
txt = txt.replace(/у/g, 'u');
txt = txt.replace(/ф/g, 'f');
txt = txt.replace(/х/g, 'kh');
txt = txt.replace(/ц/g, 'ts');
txt = txt.replace(/ь/g, '`');
txt = txt.replace(/А/g, 'A');
txt = txt.replace(/Б/g, 'B');
txt = txt.replace(/В/g, 'V');
txt = txt.replace(/Г/g, 'G');
txt = txt.replace(/Д/g, 'D');
txt = txt.replace(/Е/g, 'Ye');
txt = txt.replace(/З/g, 'Z');
txt = txt.replace(/И/g, 'I');
txt = txt.replace(/Й/g, 'J');
txt = txt.replace(/К/g, 'K');
txt = txt.replace(/Л/g, 'L');
txt = txt.replace(/М/g, 'M');
txt = txt.replace(/Н/g, 'N');
txt = txt.replace(/О/g, 'O');
txt = txt.replace(/П/g, 'P');
txt = txt.replace(/Р/g, 'R');
txt = txt.replace(/С/g, 'S');
txt = txt.replace(/Т/g, 'T');
txt = txt.replace(/У/g, 'U');
txt = txt.replace(/Ф/g, 'F');
txt = txt.replace(/Х/g, 'Kh');
txt = txt.replace(/Ц/g, 'Ts');
txt = txt.replace(/Ф/g, 'Ph');
txt = txt.replace(/ф/g, 'ph');
setWpSummary();
}
function ShowHelp(){
var select, inputId, isEdit, maxChars = 250, useMWPreview
if (wgAction == 'edit' || wgAction == 'submit'){
inputId = 'wpSummary'
}else if (wgCanonicalNamespace == 'Special' && wgCanonicalSpecialPageName == 'Movepage'){
inputId = 'wpReason'
}else if (wgAction == 'protect'){
inputId = 'mwProtect-reason'
}else return
var hlp = document.getElementById("translit_help")
if (hlp) return
var input = document.getElementById(inputId)
if (!input) return
//create counter span
var cnt = document.createElement('span')
cnt.id = 'inputCounter'
cnt.style.marginLeft = '3px'
input.parentNode.insertBefore(cnt, input.nextSibling)
cnt.innerHTML = '<table class="standard" id="translit_help">\
<tr>\
<th>А<th>Б<th>В<th>Г<th>Д<th>Е<th>Ё<th>Ж<th>З<th>И<th>Й<th>К<th>Л<th>М<th>Н<th>О<th>П<th>Р<th>С<th>Т<th>У<th>Ф<th>Х<th>Ц<th>Ч<th>Ш<th>Щ<th>Ъ<th>Ы<th>Ь<th>Э<th>Ю<th>Я\
</tr>\
<tr>\
<td>а<td>b<td>v<td>g<td>d<td>e, ye<td>yo<td>zh<td>z<td>i<td>y<td>k<td>l<td>m<td>n<td>о<td>p<td>r<td>s<td>t<td>u<td>f<td>kh<td>ts, с<td>ch<td>sh<td>shch<td>``<td>y`<td>`<td>e`<td>yu<td>уа\
</tr></table>';
}
function setWpSummary(){
var wpSummary = document.getElementById('wpSummary')
if(wpSummary){
var temp=wpSummary.value;
temp=temp.replace(/\/\*.*?\*\// , ""); //комментарии
temp=temp.replace(/[\s]*/ , ""); //пробелы
if (temp==""){
wpSummary.value=wpSummary.value+" - [[User talk:Khanson/translit ru en.js|translit ru en.js]] - translit";
}
}
}
}