Cyrillic to Latin Transliterator

Transliterates Cyrillic characters to Latin characters on any webpage.

目前為 2023-05-20 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Cyrillic to Latin Transliterator
// @description  Transliterates Cyrillic characters to Latin characters on any webpage.
// @version      1.0
// @match        *://*/*
// @grant        none
// @icon         https://opu.peklo.biz/p/23/05/13/1684010167-8264b.jpg
// @namespace https://greasyfork.org/users/905173
// ==/UserScript==

(function() {
    var map = {
    'A': 'A',
    'a': 'a',
    'Б': 'B',
    'б': 'b',
    'В': 'V',
    'в': 'v',
    'Г': 'G',
    'г': 'g',
    'Д': 'D',
    'д': 'd',
    'Е': 'E',
    'е': 'e',
    'Ё': 'Ë',
    'ё': 'ë',
    'Ж': 'Ž',
    'ж': 'ž',
    'З': 'Z',
    'з': 'z',
    'И': 'I',
    'и': 'i',
    'Й': 'J',
    'й': 'j',
    'К': 'K',
    'к': 'k',
    'Л': 'L',
    'л': 'l',
    'М': 'M',
    'м': 'm',
    'Н': 'N',
    'н': 'n',
    'О': 'O',
    'о': 'o',
    'П': 'P',
    'п': 'p',
    'Р': 'R',
    'р': 'r',
    'С': 'S',
    'с': 's',
    'Т': 'T',
    'т': 't',
    'У': 'U',
    'у': 'u',
    'Ф': 'F',
    'ф': 'f',
    'Х': 'H',
    'х': 'h',
    'Ц': 'C',
    'ц': 'c',
    'Ч': 'Č',
    'ч': 'č',
    'Ш': 'Š',
    'ш': 'š',
    'Щ': 'Ŝ',
    'щ': 'ŝ',
    'Ъ': '"',
    'ъ': '"',
    'Ы': 'Y',
    'ы': 'y',
    'Ь': "'",
    'ь': "'",
    'Я':'Â',
    'я':'â',
    'Э':'È',
    'э':'è',
    'Ю':'Û',
    'ю':'û'
};

    function replaceText(node) {
        var value = node.nodeValue;
        var newValue = '';
        for (var i = 0; i < value.length; i++) {
            var char = value.charAt(i);
            newValue += char in map ? map[char] : char;
        }
        node.nodeValue = newValue;
    }

    function replaceCyrillic() {
        var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
        var node;
        while (node = walker.nextNode()) {
            replaceText(node);
        }
    }

    function createToggleButton() {
        var button = document.createElement('button');
        button.innerHTML = 'Translit RU→CZ';
        button.style.position = 'fixed';
        button.style.bottom = '20px';
        button.style.right = '20px';
        button.onclick = toggleTransliteration;
        document.body.appendChild(button);
    }

    function toggleTransliteration() {
        isTransliterationEnabled = !isTransliterationEnabled;
        if (isTransliterationEnabled) {
            replaceCyrillic();
        } else {
            location.reload();
        }
    }

    var isTransliterationEnabled = false;
    var cyrillicRegex = /[\u0400-\u04FF]/;

    if (cyrillicRegex.test(document.body.innerHTML)) {
        createToggleButton();
    }
})();