ChatGPT Utils

Modifies the behavior of the chat interface on the OpenAI website

目前为 2022-12-27 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name ChatGPT Utils
  3. // @description Modifies the behavior of the chat interface on the OpenAI website
  4. // @namespace ChatGPTUtils
  5. // @version 1.7.8
  6. // @author CriDos
  7. // @match https://chat.openai.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=chat.openai.com
  9. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js
  10. // @grant GM_xmlhttpRequest
  11. // @run-at document-end
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. 'use strict';
  16.  
  17. console.log(`ChatGPT Utils initializing...`);
  18.  
  19. let debug = false;
  20.  
  21. setInterval(() => {
  22. try {
  23. addAutoTranslate();
  24. //addTranslateButtons();
  25. } catch (error) {
  26. console.error(error);
  27. }
  28.  
  29. try {
  30. findAndHookTextareaElement();
  31. } catch (error) {
  32. console.error(error);
  33. }
  34. }, 100);
  35.  
  36. function addAutoTranslate() {
  37. var messages = document.querySelectorAll(".markdown.prose");
  38.  
  39. for (var i = 0; i < messages.length; i++) {
  40. const msgMarkdownNode = messages[i];
  41. const parentMsgMarkdown = msgMarkdownNode.parentElement;
  42.  
  43. if (parentMsgMarkdown.isAutoTranslate) {
  44. continue;
  45. }
  46. parentMsgMarkdown.isAutoTranslate = true;
  47.  
  48. setInterval(async () => {
  49. await translateNode(msgMarkdownNode);
  50. }, 500);
  51. }
  52. }
  53.  
  54. function addTranslateButtons() {
  55. var messages = document.querySelectorAll(".markdown.prose");
  56.  
  57. for (var i = 0; i < messages.length; i++) {
  58. const msgMarkdownNode = messages[i];
  59. const msgIcon = msgMarkdownNode.parentElement.parentElement.parentElement.previousElementSibling;
  60.  
  61. if (!msgIcon.querySelector(".translate-button")) {
  62. var btn = document.createElement("button");
  63. btn.textContent = "Tr";
  64. btn.classList.add("translate-button");
  65. btn.style.cssText = "width: 30px; height: 30px;";
  66. msgIcon.insertBefore(btn, msgIcon.firstChild);
  67.  
  68. btn.addEventListener("click", async () => {
  69. await translateNode(msgMarkdownNode);
  70. });
  71. }
  72. }
  73. }
  74.  
  75. function findAndHookTextareaElement() {
  76. const targetElement = document.querySelector("textarea");
  77. if (targetElement === null) {
  78. return;
  79. }
  80.  
  81. if (targetElement.isAddHookKeydownEvent === true) {
  82. return;
  83. }
  84.  
  85. targetElement.isAddHookKeydownEvent = true;
  86.  
  87. console.log(`Textarea element found. Adding keydown event listener.`);
  88. targetElement.addEventListener("keydown", async event => await handleSubmit(event, targetElement), true);
  89. }
  90.  
  91. async function handleSubmit(event, targetElement) {
  92. console.log(`Keydown event detected: type - ${event.type}, key - ${event.key}`);
  93.  
  94. if (event.shiftKey && event.key === "Enter") {
  95. return;
  96. }
  97.  
  98. if (window.isActiveOnSubmit === true) {
  99. return;
  100. }
  101.  
  102. if (event.key === "Enter") {
  103. window.isActiveOnSubmit = true;
  104. event.stopImmediatePropagation();
  105.  
  106. const request = targetElement.value;
  107. targetElement.value = "";
  108.  
  109. const translatedText = await translate(request, "ru", "en", "text");
  110.  
  111. targetElement.focus();
  112. targetElement.value = translatedText;
  113. const enterEvent = new KeyboardEvent("keydown", {
  114. bubbles: true,
  115. cancelable: true,
  116. key: "Enter",
  117. code: "Enter"
  118. });
  119. targetElement.dispatchEvent(enterEvent);
  120.  
  121. window.isActiveOnSubmit = false;
  122. }
  123. }
  124.  
  125. async function translateNode(node) {
  126. const translateClassName = "translate-markdown";
  127. const parentNode = node.parentElement;
  128. const nodeContent = $(node).html();
  129.  
  130. if (node.storeContent == nodeContent) {
  131. return;
  132. }
  133. node.storeContent = nodeContent;
  134.  
  135. var translateNode = parentNode.querySelector(`.${translateClassName}`);
  136. if (translateNode == null) {
  137. translateNode = node.cloneNode(true);
  138. translateNode.classList.add(translateClassName);
  139. parentNode.insertBefore(translateNode, parentNode.firstChild);
  140. }
  141.  
  142. var translatedContent = await translateHTML(nodeContent, "en", navigator.language)
  143. translatedContent = translatedContent.replace(/<\/div>$/, '');
  144.  
  145. $(translateNode).html(translatedContent + `<p>.......... конец_перевода ..........</p></div>`);
  146. }
  147.  
  148. async function translateHTML(html, sLang, tLang) {
  149. const excludeTagRegex = /<(pre|code)[^>]*>([\s\S]*?)<\/(pre|code)>/g;
  150. const excludeTags = [];
  151. const excludePlaceholder = 'e0x1c';
  152.  
  153. let htmlContent = html;
  154.  
  155. let excludeTagsMatch;
  156. while (excludeTagsMatch = excludeTagRegex.exec(html)) {
  157. excludeTags.push(excludeTagsMatch[0]);
  158. htmlContent = htmlContent.replace(excludeTagsMatch[0], `<${excludePlaceholder}${excludeTags.length - 1}>`);
  159. }
  160.  
  161. if (debug) {
  162. console.log(`preTranslateHTML: ${html}`);
  163. }
  164.  
  165. htmlContent = await translate(htmlContent, sLang, tLang);
  166.  
  167. for (let i = 0; i < excludeTags.length; i++) {
  168. htmlContent = htmlContent.replace(`<${excludePlaceholder}${i}>`, excludeTags[i]);
  169. }
  170.  
  171. if (debug) {
  172. console.log(`postTranslateHTML: ${htmlContent}`);
  173. }
  174.  
  175. return htmlContent;
  176. }
  177.  
  178. async function translate(text, sLang, tLang, format, key) {
  179. try {
  180. if (debug) {
  181. console.log(`preTranslate: ${text}`);
  182. }
  183.  
  184. if (format == null) {
  185. format = "html";
  186. }
  187.  
  188. if (key == null) {
  189. key = "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw";
  190. }
  191.  
  192. const result = await new Promise((resolve, reject) => {
  193. GM_xmlhttpRequest({
  194. method: "POST",
  195. url: `https://translate.googleapis.com/translate_a/t?client=gtx&format=${format}&sl=${sLang}&tl=${tLang}&key=${key}`,
  196. data: `q=${encodeURIComponent(text)}`,
  197. headers: {
  198. "Content-Type": "application/x-www-form-urlencoded"
  199. },
  200. onload: response => {
  201. resolve(JSON.parse(response.responseText)[0]);
  202. },
  203. onerror: response => {
  204. reject(response.statusText);
  205. }
  206. });
  207. });
  208.  
  209. if (debug) {
  210. console.log(`postTranslate: ${result}`);
  211. }
  212.  
  213. return result;
  214. } catch (error) {
  215. console.error(error);
  216. }
  217. }