TesterTV_Translit (Beta)

A quick way to converting text from one writing system to another. In this case english to russian.

目前為 2023-11-06 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         TesterTV_Translit (Beta)
// @namespace    https://greasyfork.org/ru/users/1194621-testertv
// @version      2023.11.06
// @license      GNU GPLv3 or later
// @description  A quick way to converting text from one writing system to another. In this case english to russian.
// @author       TesterTV
// @match        *://*/*
// @grant        GM_setClipboard
// ==/UserScript==

// Check if the current page is embedded
var isEmbedded = window.self !== window.top;

// If not embedded than...
if (!isEmbedded) {

//********************************************************************************************************************
//***                                              Main Button 🇹                                                  ***
//********************************************************************************************************************

  // Create a new button element
  const TranslitButton = document.createElement('button');

  // Set the CSS styles for positioning the button
  TranslitButton.style.position = 'fixed';
  TranslitButton.style.top = '60px';
  TranslitButton.style.right = '10px';
  TranslitButton.style.zIndex = '9999';
  TranslitButton.innerText = '🇹';
  TranslitButton.style.position = 'fixed';
  TranslitButton.style.background = 'none';
  TranslitButton.style.border = 'none';

  // Append the button to the document body
  document.body.appendChild(TranslitButton);

//********************************************************************************************************************
//***                                              Main Textarea 📝                                               ***
//********************************************************************************************************************

// Create a new textarea element
const TranslitTextarea = document.createElement('textarea');

// Set the CSS styles for positioning the textarea
TranslitTextarea.style.position = 'fixed';
TranslitTextarea.style.top = '66px';
TranslitTextarea.style.right = '36px';
TranslitTextarea.style.width = '400px';
TranslitTextarea.style.height = '400px';
TranslitTextarea.style.zIndex = '9999';
TranslitTextarea.placeholder = 'Enter text here...';
TranslitTextarea.style.background = '#303236';
TranslitTextarea.style.border = '1px solid grey';
TranslitTextarea.style.color = 'white';
TranslitTextarea.style.fontSize = '16px';
TranslitTextarea.style.fontFamily = 'Arial, Helvetica, sans-serif';
TranslitTextarea.style.display = 'none';
TranslitTextarea.style.outline = 'none';

// Append the textarea to the document body
document.body.appendChild(TranslitTextarea);

//********************************************************************************************************************
//***                                    Field for number of letters 🔠                                           ***
//********************************************************************************************************************

// Create an extra field to display the number of letters
const LettersNumberField = document.createElement('div');
LettersNumberField.style.position = 'fixed';
LettersNumberField.style.top = '476px';
LettersNumberField.style.right = '36px';
LettersNumberField.style.color = 'grey';
LettersNumberField.style.textShadow = '2px 2px 4px rgba(0, 0, 0, 0.5)';
LettersNumberField.style.zIndex = '9999';
LettersNumberField.innerText = 'length: 0'
LettersNumberField.style.display = 'none';

// Append the extra field to the document body
document.body.appendChild(LettersNumberField);

// Update number of letters in field
TranslitTextarea.addEventListener('input', function() {
  // Update the extra field with the height value
  LettersNumberField.innerText = "length: " + TranslitTextarea.value.length;
  if (LettersNumberField.innerText === 'length: 0') {LettersNumberField.innerText = "";}
});

//********************************************************************************************************************
//***                                   Main Textarea - Listener event 📝👂                                       ***
//********************************************************************************************************************

// Add click event listener to the button
  TranslitButton.addEventListener('mouseover', function() {
    // Toggle the visibility of the textarea
    LettersNumberField.style.display = 'block';
    TranslitTextarea.style.display = 'block';
    TranslitTextarea.focus();
  });

// Add event listener to hide textarea and copy text to clipboard when clicking on the webpage
    document.addEventListener('click', () => {
        // Check if the textarea is visible
        if (TranslitTextarea.style.display !== 'none') {
            // Copying the text, if it exists, to the clipboard
            if (TranslitTextarea.value !== '') {
                try {GM_setClipboard(TranslitTextarea.value);} catch (error) {}
                try {navigator.clipboard.writeText(TranslitTextarea.value);} catch (error) {}
            }
            // Clear and close textarea
            TranslitTextarea.value = '';
            LettersNumberField.innerText = "length: " + TranslitTextarea.value.length
            TranslitTextarea.style.display = 'none';
            LettersNumberField.style.display = 'none';
        }
    });

// Add event listeners to prevent action when clicking on tabs, buttons,...
  TranslitTextarea.addEventListener('click', (e) => {
    e.stopPropagation(); // Stop event propagation
  });

//********************************************************************************************************************
//***                                        Arrays for transliteration 📚                                        ***
//********************************************************************************************************************

const array1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
const array2 = ['а', 'б', 'ц', 'д', 'е', 'ф', 'г', 'х', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'я', 'р', 'с', 'т', 'у', 'в', 'щ', 'х', 'ы', 'з', 'А', 'Б', 'Ц', 'Д', 'Е', 'Ф', 'Г', 'Х', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Я', 'Р', 'С', 'Т', 'У', 'В', 'Щ', 'Х', 'Ы', 'З'];

// Create a lookup table object for faster symbol lookup
const lookupTable = {};
for (let i = 0; i < array1.length; i++) {
  lookupTable[array1[i]] = array2[i];
}

//********************************************************************************************************************
//***                                              Functionality ⚙️                                               ***
//********************************************************************************************************************

// Listen for input event on the textarea
TranslitTextarea.addEventListener('input', function() {
  // Get the current value of the textarea
  const value = TranslitTextarea.value;

  // Replace all occurrences of symbols using the lookup table
  TranslitTextarea.value = value.replaceAll(/[a-zA-Z]/g, (match) => lookupTable[match]);

  TranslitTextarea.value = TranslitTextarea.value.replace("йо", "ё").replace("ЙО", "Ё").replace("Йо", "Ё").replace("йО", "Ё");
  TranslitTextarea.value = TranslitTextarea.value.replace("ыо", "ё").replace("ЫО", "Ё").replace("Ыо", "Ё").replace("ЫО", "Ё");
  TranslitTextarea.value = TranslitTextarea.value.replace("ö", "ё").replace("Ö", "Ё");
  TranslitTextarea.value = TranslitTextarea.value.replace("зх", "ж").replace("ЗХ", "Ж").replace("Зх", "Ж").replace("зХ", "Ж");
  TranslitTextarea.value = TranslitTextarea.value.replace("цх", "ч").replace("ЦХ", "Ч").replace("Цх", "Ч").replace("цХ", "Ч");
  TranslitTextarea.value = TranslitTextarea.value.replace("сх", "ш").replace("СХ", "Ш").replace("Сх", "Ш").replace("сХ", "Ш");
  TranslitTextarea.value = TranslitTextarea.value.replace("шх", "щ").replace("ШХ", "Щ").replace("Шх", "Щ").replace("шХ", "Щ");
  TranslitTextarea.value = TranslitTextarea.value.replace("#", "ъ").replace("##", "Ъ");
  TranslitTextarea.value = TranslitTextarea.value.replace("твз", "ъ").replace("ТВЗ", "Ъ").replace("ТВз", "Ъ").replace("ТвЗ", "Ъ").replace("Твз", "Ъ").replace("тВЗ", "Ъ").replace("тВз", "Ъ").replace("твЗ", "Ъ");
  TranslitTextarea.value = TranslitTextarea.value.replace("'", "ь").replace("''", "Ь");
  TranslitTextarea.value = TranslitTextarea.value.replace("мйз", "ь").replace("МЙЗ", "Ь").replace("МЙз", "Ь").replace("МйЗ", "Ь").replace("Мйз", "Ь").replace("мЙЗ", "Ь").replace("мЙз", "Ь").replace("мйЗ", "Ь");
  TranslitTextarea.value = TranslitTextarea.value.replace("йе", "э").replace("ЙЕ", "Э").replace("Йе", "Э").replace("йЕ", "Э");
  TranslitTextarea.value = TranslitTextarea.value.replace("ä", "э").replace("Ä", "Э");
  TranslitTextarea.value = TranslitTextarea.value.replace("йу", "ю").replace("ЙУ", "Ю").replace("Йу", "Ю").replace("йУ", "Ю");
  TranslitTextarea.value = TranslitTextarea.value.replace("ыу", "ю").replace("ЫУ", "Ю").replace("Ыу", "Ю").replace("ыУ", "Ю");
  TranslitTextarea.value = TranslitTextarea.value.replace("ü", "ю").replace("Ü", "Ю");
  TranslitTextarea.value = TranslitTextarea.value.replace("йа", "я").replace("ЙА", "Я").replace("Йа", "Я").replace("йА", "Я");
  TranslitTextarea.value = TranslitTextarea.value.replace("ыа", "я").replace("ЫА", "Я").replace("Ыа", "Я").replace("ыА", "Я");

  TranslitTextarea.value = TranslitTextarea.value.replace("č", "ч").replace("Č", "Ч");
  TranslitTextarea.value = TranslitTextarea.value.replace("ž", "ж").replace("Ž", "Ж");
  TranslitTextarea.value = TranslitTextarea.value.replace("š", "ш").replace("Š", "Ш");

  TranslitTextarea.value = TranslitTextarea.value.replace("шод", "сход");
});
}