Google Translate English split words

Violentmonkey 腳本

  1. // ==UserScript==
  2. // @name Google Translate English split words
  3. // @name:en Google Translate English split words
  4. // @name:zh-CN Google Translate English split words
  5. // @name:zh-TW Google Translate English split words
  6. // @name:ja Google Translate English split words
  7. // @name:ko Google Translate English split words
  8. // @name:de Google Translate English split words
  9. // @name:fr Google Translate English split words
  10. // @name:es Google Translate English split words
  11. // @name:pt Google Translate English split words
  12. // @name:ru Google Translate English split words
  13. // @name:it Google Translate English split words
  14. // @name:tr Google Translate English split words
  15. // @name:ar Google Translate English split words
  16. // @name:th Google Translate English split words
  17. // @name:vi Google Translate English split words
  18. // @name:id Google Translate English split words
  19. // @namespace Violentmonkey Scripts
  20. // @match *://translate.google.com/*
  21. // @version XiaoYing_2023.05.25.22
  22. // @grant GM_info
  23. // @grant GM_getValue
  24. // @grant GM_setValue
  25. // @grant GM_addStyle
  26. // @grant GM_deleteValue
  27. // @grant GM_xmlhttpRequest
  28. // @grant GM_setClipboard
  29. // @grant GM_registerMenuCommand
  30. // @grant GM_unregisterMenuCommand
  31. // @grant GM_getResourceText
  32. // @grant GM_getResourceURL
  33. // @grant GM_openInTab
  34. // @grant unsafeWindow
  35. // @run-at document-start
  36. // @author github.com @XiaoYingYo
  37. // @require https://gf.qytechs.cn/scripts/464929-module-jquery-xiaoying/code/module_jquery_XiaoYing.js
  38. // @require https://gf.qytechs.cn/scripts/464780-global-module/code/global_module.js
  39. // @description Violentmonkey Scripts
  40. // @description:en Violentmonkey Scripts
  41. // @description:zh-CN Violentmonkey 脚本
  42. // @description:zh-TW Violentmonkey 腳本
  43. // @description:ja Violentmonkey スクリプト
  44. // @description:ko Violentmonkey 스크립트
  45. // @description:de Violentmonkey Skripte
  46. // @description:fr Violentmonkey Scripts
  47. // @description:es Violentmonkey Scripts
  48. // @description:pt Violentmonkey Scripts
  49. // @description:ru Violentmonkey Сценарии
  50. // @description:it Violentmonkey Scripts
  51. // @description:tr Violentmonkey Scripts
  52. // @description:ar Violentmonkey Scripts
  53. // @description:th Violentmonkey Scripts
  54. // @description:vi Violentmonkey Scripts
  55. // @description:id Violentmonkey Scripts
  56. // ==/UserScript==
  57.  
  58.  
  59. var GlobalVariable = new Map();
  60.  
  61. var ProcessRules = new Map();
  62.  
  63. ProcessRules.set('convertToTitleCase_01', (Text) => {
  64. return convertToTitleCase(Text, '_');
  65. });
  66.  
  67. ProcessRules.set('convertToTitleCase_02', (Text) => {
  68. return convertToTitleCase(Text, '-');
  69. });
  70.  
  71. ProcessRules.set('UppercaseSplitWords', (Text) => {
  72. return Text.replace(/(?<!\s)([A-Z])/g, ' $1').trim();
  73. });
  74.  
  75. function ProcessText(textarea) {
  76. if (GlobalVariable.get('InputIng') === 1) {
  77. return null;
  78. }
  79. let text = textarea.val();
  80. if (!containsEnglishLetter(text)) {
  81. return null;
  82. }
  83. if (text == '') {
  84. return null;
  85. }
  86. GlobalVariable.set('InputIng', 1);
  87. let oldLength = text.length;
  88. let newText = text;
  89. for (const item of ProcessRules.values()) {
  90. newText = item(newText);
  91. if (newText !== text) {
  92. break;
  93. }
  94. }
  95. if (newText === text) {
  96. GlobalVariable.set('InputIng', 0);
  97. return null;
  98. }
  99. let newLength = newText.length;
  100. global_module.AnalogInput.AnalogInput(textarea[0], newText);
  101. GlobalVariable.set('InputIng', 0);
  102. let oldChanges = GlobalVariable.get('IgnoreChanges');
  103. GlobalVariable.set('IgnoreChanges', oldChanges + 1);
  104. return newLength - oldLength;
  105. }
  106.  
  107. function convertToTitleCase(Text, separator) {
  108. let regx = new RegExp(separator, 'g');
  109. const words = Text.replace(regx, ' ').split(' ');
  110. if (words.length == 1) {
  111. return Text;
  112. }
  113. const titleCaseWords = words.map((word) => {
  114. const lowerCaseWord = word.toLowerCase();
  115. return lowerCaseWord.charAt(0).toUpperCase() + lowerCaseWord.slice(1);
  116. });
  117. return titleCaseWords.join(' ');
  118. }
  119.  
  120. function containsEnglishLetter(str) {
  121. for (let i = 0; i < str.length; i++) {
  122. if (i > 1000) {
  123. break;
  124. }
  125. const charCode = str.charCodeAt(i);
  126. if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122)) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132.  
  133. async function main() {
  134. let textarea = await global_module.waitForElement('textarea[class][jsname]');
  135. let polite = await global_module.waitForElement("div[aria-live='polite']");
  136. textarea = textarea.eq(0);
  137. GlobalVariable.set('InputIng', 0);
  138. GlobalVariable.set('IgnoreChanges', 0);
  139. let MutationObserver = unsafeWindow.MutationObserver || unsafeWindow.WebKitMutationObserver || unsafeWindow.MozMutationObserver;
  140. let observer = new MutationObserver(
  141. global_module.debounce(() => {
  142. if (GlobalVariable.get('IgnoreChanges') !== 0) {
  143. let oldChanges = GlobalVariable.get('IgnoreChanges');
  144. GlobalVariable.set('IgnoreChanges', oldChanges - 1);
  145. return;
  146. }
  147. let selectionStart = textarea.prop('selectionStart');
  148. let selectionEnd = textarea.prop('selectionEnd');
  149. let index = ProcessText(textarea);
  150. if (index && index != 0) {
  151. selectionStart += index;
  152. selectionEnd += index;
  153. }
  154. textarea.prop('selectionStart', selectionStart);
  155. textarea.prop('selectionEnd', selectionEnd);
  156. textarea.focus();
  157. }),
  158. 1000
  159. );
  160. observer.observe(polite[0], { childList: true, subtree: false });
  161. }
  162.  
  163. main();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址