TesterTV_Translit (Beta)

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

当前为 2023-11-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TesterTV_Translit (Beta)
  3. // @namespace https://greasyfork.org/ru/users/1194621-testertv
  4. // @version 2023.11.06
  5. // @license GNU GPLv3 or later
  6. // @description A quick way to converting text from one writing system to another. In this case english to russian.
  7. // @author TesterTV
  8. // @match *://*/*
  9. // @grant GM_setClipboard
  10. // ==/UserScript==
  11.  
  12. // Check if the current page is embedded
  13. var isEmbedded = window.self !== window.top;
  14.  
  15. // If not embedded than...
  16. if (!isEmbedded) {
  17.  
  18. //********************************************************************************************************************
  19. //*** Main Button 🇹 ***
  20. //********************************************************************************************************************
  21.  
  22. // Create a new button element
  23. const TranslitButton = document.createElement('button');
  24.  
  25. // Set the CSS styles for positioning the button
  26. TranslitButton.style.position = 'fixed';
  27. TranslitButton.style.top = '60px';
  28. TranslitButton.style.right = '10px';
  29. TranslitButton.style.zIndex = '9999';
  30. TranslitButton.innerText = '🇹';
  31. TranslitButton.style.position = 'fixed';
  32. TranslitButton.style.background = 'none';
  33. TranslitButton.style.border = 'none';
  34.  
  35. // Append the button to the document body
  36. document.body.appendChild(TranslitButton);
  37.  
  38. //********************************************************************************************************************
  39. //*** Main Textarea 📝 ***
  40. //********************************************************************************************************************
  41.  
  42. // Create a new textarea element
  43. const TranslitTextarea = document.createElement('textarea');
  44.  
  45. // Set the CSS styles for positioning the textarea
  46. TranslitTextarea.style.position = 'fixed';
  47. TranslitTextarea.style.top = '66px';
  48. TranslitTextarea.style.right = '36px';
  49. TranslitTextarea.style.width = '400px';
  50. TranslitTextarea.style.height = '400px';
  51. TranslitTextarea.style.zIndex = '9999';
  52. TranslitTextarea.placeholder = 'Enter text here...';
  53. TranslitTextarea.style.background = '#303236';
  54. TranslitTextarea.style.border = '1px solid grey';
  55. TranslitTextarea.style.color = 'white';
  56. TranslitTextarea.style.fontSize = '16px';
  57. TranslitTextarea.style.fontFamily = 'Arial, Helvetica, sans-serif';
  58. TranslitTextarea.style.display = 'none';
  59. TranslitTextarea.style.outline = 'none';
  60.  
  61. // Append the textarea to the document body
  62. document.body.appendChild(TranslitTextarea);
  63.  
  64. //********************************************************************************************************************
  65. //*** Field for number of letters 🔠 ***
  66. //********************************************************************************************************************
  67.  
  68. // Create an extra field to display the number of letters
  69. const LettersNumberField = document.createElement('div');
  70. LettersNumberField.style.position = 'fixed';
  71. LettersNumberField.style.top = '476px';
  72. LettersNumberField.style.right = '36px';
  73. LettersNumberField.style.color = 'grey';
  74. LettersNumberField.style.textShadow = '2px 2px 4px rgba(0, 0, 0, 0.5)';
  75. LettersNumberField.style.zIndex = '9999';
  76. LettersNumberField.innerText = 'length: 0'
  77. LettersNumberField.style.display = 'none';
  78.  
  79. // Append the extra field to the document body
  80. document.body.appendChild(LettersNumberField);
  81.  
  82. // Update number of letters in field
  83. TranslitTextarea.addEventListener('input', function() {
  84. // Update the extra field with the height value
  85. LettersNumberField.innerText = "length: " + TranslitTextarea.value.length;
  86. if (LettersNumberField.innerText === 'length: 0') {LettersNumberField.innerText = "";}
  87. });
  88.  
  89. //********************************************************************************************************************
  90. //*** Main Textarea - Listener event 📝👂 ***
  91. //********************************************************************************************************************
  92.  
  93. // Add click event listener to the button
  94. TranslitButton.addEventListener('mouseover', function() {
  95. // Toggle the visibility of the textarea
  96. LettersNumberField.style.display = 'block';
  97. TranslitTextarea.style.display = 'block';
  98. TranslitTextarea.focus();
  99. });
  100.  
  101. // Add event listener to hide textarea and copy text to clipboard when clicking on the webpage
  102. document.addEventListener('click', () => {
  103. // Check if the textarea is visible
  104. if (TranslitTextarea.style.display !== 'none') {
  105. // Copying the text, if it exists, to the clipboard
  106. if (TranslitTextarea.value !== '') {
  107. try {GM_setClipboard(TranslitTextarea.value);} catch (error) {}
  108. try {navigator.clipboard.writeText(TranslitTextarea.value);} catch (error) {}
  109. }
  110. // Clear and close textarea
  111. TranslitTextarea.value = '';
  112. LettersNumberField.innerText = "length: " + TranslitTextarea.value.length
  113. TranslitTextarea.style.display = 'none';
  114. LettersNumberField.style.display = 'none';
  115. }
  116. });
  117.  
  118. // Add event listeners to prevent action when clicking on tabs, buttons,...
  119. TranslitTextarea.addEventListener('click', (e) => {
  120. e.stopPropagation(); // Stop event propagation
  121. });
  122.  
  123. //********************************************************************************************************************
  124. //*** Arrays for transliteration 📚 ***
  125. //********************************************************************************************************************
  126.  
  127. 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'];
  128. const array2 = ['а', 'б', 'ц', 'д', 'е', 'ф', 'г', 'х', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'я', 'р', 'с', 'т', 'у', 'в', 'щ', 'х', 'ы', 'з', 'А', 'Б', 'Ц', 'Д', 'Е', 'Ф', 'Г', 'Х', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Я', 'Р', 'С', 'Т', 'У', 'В', 'Щ', 'Х', 'Ы', 'З'];
  129.  
  130. // Create a lookup table object for faster symbol lookup
  131. const lookupTable = {};
  132. for (let i = 0; i < array1.length; i++) {
  133. lookupTable[array1[i]] = array2[i];
  134. }
  135.  
  136. //********************************************************************************************************************
  137. //*** Functionality ⚙️ ***
  138. //********************************************************************************************************************
  139.  
  140. // Listen for input event on the textarea
  141. TranslitTextarea.addEventListener('input', function() {
  142. // Get the current value of the textarea
  143. const value = TranslitTextarea.value;
  144.  
  145. // Replace all occurrences of symbols using the lookup table
  146. TranslitTextarea.value = value.replaceAll(/[a-zA-Z]/g, (match) => lookupTable[match]);
  147.  
  148. TranslitTextarea.value = TranslitTextarea.value.replace("йо", "ё").replace("ЙО", "Ё").replace("Йо", "Ё").replace("йО", "Ё");
  149. TranslitTextarea.value = TranslitTextarea.value.replace("ыо", "ё").replace("ЫО", "Ё").replace("Ыо", "Ё").replace("ЫО", "Ё");
  150. TranslitTextarea.value = TranslitTextarea.value.replace("ö", "ё").replace("Ö", "Ё");
  151. TranslitTextarea.value = TranslitTextarea.value.replace("зх", "ж").replace("ЗХ", "Ж").replace("Зх", "Ж").replace("зХ", "Ж");
  152. TranslitTextarea.value = TranslitTextarea.value.replace("цх", "ч").replace("ЦХ", "Ч").replace("Цх", "Ч").replace("цХ", "Ч");
  153. TranslitTextarea.value = TranslitTextarea.value.replace("сх", "ш").replace("СХ", "Ш").replace("Сх", "Ш").replace("сХ", "Ш");
  154. TranslitTextarea.value = TranslitTextarea.value.replace("шх", "щ").replace("ШХ", "Щ").replace("Шх", "Щ").replace("шХ", "Щ");
  155. TranslitTextarea.value = TranslitTextarea.value.replace("#", "ъ").replace("##", "Ъ");
  156. TranslitTextarea.value = TranslitTextarea.value.replace("твз", "ъ").replace("ТВЗ", "Ъ").replace("ТВз", "Ъ").replace("ТвЗ", "Ъ").replace("Твз", "Ъ").replace("тВЗ", "Ъ").replace("тВз", "Ъ").replace("твЗ", "Ъ");
  157. TranslitTextarea.value = TranslitTextarea.value.replace("'", "ь").replace("''", "Ь");
  158. TranslitTextarea.value = TranslitTextarea.value.replace("мйз", "ь").replace("МЙЗ", "Ь").replace("МЙз", "Ь").replace("МйЗ", "Ь").replace("Мйз", "Ь").replace("мЙЗ", "Ь").replace("мЙз", "Ь").replace("мйЗ", "Ь");
  159. TranslitTextarea.value = TranslitTextarea.value.replace("йе", "э").replace("ЙЕ", "Э").replace("Йе", "Э").replace("йЕ", "Э");
  160. TranslitTextarea.value = TranslitTextarea.value.replace("ä", "э").replace("Ä", "Э");
  161. TranslitTextarea.value = TranslitTextarea.value.replace("йу", "ю").replace("ЙУ", "Ю").replace("Йу", "Ю").replace("йУ", "Ю");
  162. TranslitTextarea.value = TranslitTextarea.value.replace("ыу", "ю").replace("ЫУ", "Ю").replace("Ыу", "Ю").replace("ыУ", "Ю");
  163. TranslitTextarea.value = TranslitTextarea.value.replace("ü", "ю").replace("Ü", "Ю");
  164. TranslitTextarea.value = TranslitTextarea.value.replace("йа", "я").replace("ЙА", "Я").replace("Йа", "Я").replace("йА", "Я");
  165. TranslitTextarea.value = TranslitTextarea.value.replace("ыа", "я").replace("ЫА", "Я").replace("Ыа", "Я").replace("ыА", "Я");
  166.  
  167. TranslitTextarea.value = TranslitTextarea.value.replace("č", "ч").replace("Č", "Ч");
  168. TranslitTextarea.value = TranslitTextarea.value.replace("ž", "ж").replace("Ž", "Ж");
  169. TranslitTextarea.value = TranslitTextarea.value.replace("š", "ш").replace("Š", "Ш");
  170.  
  171. TranslitTextarea.value = TranslitTextarea.value.replace("шод", "сход");
  172. });
  173. }