ChatGPT 页面翻译按钮

🍓 让 ChatGPT 一键翻译您正在阅读的网页

当前为 2023-07-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name chatgpt-page-translate-button
  3. // @description 🍓 let ChatGPT translate the web page you are reading in one click
  4. // @author mefengl
  5. // @version 0.4.8
  6. // @namespace https://github.com/mefengl
  7. // @require https://cdn.jsdelivr.net/npm/@mozilla/readability@0.4.3/Readability.min.js
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=openai.com
  9. // @license MIT
  10. // @match *://*/*
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_unregisterMenuCommand
  15. // @grant GM_addValueChangeListener
  16.  
  17. // @name:en ChatGPT Page Translate Button
  18. // @description:en 🍓 let ChatGPT translate the web page you are reading in one click
  19. // @name:zh-CN ChatGPT 页面翻译按钮
  20. // @description:zh-CN 🍓 让 ChatGPT 一键翻译您正在阅读的网页
  21. // @name:es Botón de traducción de página de ChatGPT
  22. // @description:es 🍓 permite que ChatGPT traduzca la página web que estás leyendo con un solo clic
  23. // @name:hi ChatGPT पृष्ठ अनुवाद बटन
  24. // @description:hi 🍓 ChatGPT को वेबपेज जो आप पढ़ रहे हैं को एक क्लिक में अनुवाद करने दें
  25. // @name:ar زر ترجمة الصفحة لـ ChatGPT
  26. // @description:ar 🍓 دع ChatGPT يترجم صفحة الويب التي تقرأها بنقرة واحدة
  27. // @name:pt Botão de tradução de página do ChatGPT
  28. // @description:pt 🍓 permita que o ChatGPT traduza a página da web que você está lendo com um clique
  29. // @name:ru Кнопка перевода страницы ChatGPT
  30. // @description:ru 🍓 позволяет ChatGPT переводить веб-страницу, которую вы читаете, одним щелчком мыши
  31. // @name:ja ChatGPTページ翻訳ボタン
  32. // @description:ja 🍓 ChatGPTで読んでいるWebページをワンクリックで翻訳
  33. // @name:de ChatGPT-Seitenübersetzungs-Button
  34. // @description:de 🍓 Lassen Sie ChatGPT die Webseite, die Sie gerade lesen, mit einem Klick übersetzen
  35. // @name:fr Bouton de traduction de page ChatGPT
  36. // @description:fr 🍓 laissez ChatGPT traduire la page Web que vous lisez en un seul clic
  37. // ==/UserScript==
  38. "use strict";
  39. (() => {
  40. var __create = Object.create;
  41. var __defProp = Object.defineProperty;
  42. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  43. var __getOwnPropNames = Object.getOwnPropertyNames;
  44. var __getProtoOf = Object.getPrototypeOf;
  45. var __hasOwnProp = Object.prototype.hasOwnProperty;
  46. var __commonJS = (cb, mod) => function __require() {
  47. return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  48. };
  49. var __copyProps = (to, from, except, desc) => {
  50. if (from && typeof from === "object" || typeof from === "function") {
  51. for (let key of __getOwnPropNames(from))
  52. if (!__hasOwnProp.call(to, key) && key !== except)
  53. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  54. }
  55. return to;
  56. };
  57. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  58. // If the importer is in node compatibility mode or this is not an ESM
  59. // file that has been converted to a CommonJS file using a Babel-
  60. // compatible transform (i.e. "__esModule" has not been set), then set
  61. // "default" to the CommonJS "module.exports" for node compatibility.
  62. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  63. mod
  64. ));
  65. var __async = (__this, __arguments, generator) => {
  66. return new Promise((resolve, reject) => {
  67. var fulfilled = (value) => {
  68. try {
  69. step(generator.next(value));
  70. } catch (e) {
  71. reject(e);
  72. }
  73. };
  74. var rejected = (value) => {
  75. try {
  76. step(generator.throw(value));
  77. } catch (e) {
  78. reject(e);
  79. }
  80. };
  81. var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
  82. step((generator = generator.apply(__this, __arguments)).next());
  83. });
  84. };
  85.  
  86. // ../../packages/chatkit/dist/chatgpt/index.js
  87. var require_chatgpt = __commonJS({
  88. "../../packages/chatkit/dist/chatgpt/index.js"(exports, module) {
  89. "use strict";
  90. var __defProp2 = Object.defineProperty;
  91. var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
  92. var __getOwnPropNames2 = Object.getOwnPropertyNames;
  93. var __hasOwnProp2 = Object.prototype.hasOwnProperty;
  94. var __export = (target, all) => {
  95. for (var name in all)
  96. __defProp2(target, name, { get: all[name], enumerable: true });
  97. };
  98. var __copyProps2 = (to, from, except, desc) => {
  99. if (from && typeof from === "object" || typeof from === "function") {
  100. for (let key of __getOwnPropNames2(from))
  101. if (!__hasOwnProp2.call(to, key) && key !== except)
  102. __defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
  103. }
  104. return to;
  105. };
  106. var __toCommonJS = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
  107. var chatgpt_exports = {};
  108. __export(chatgpt_exports, {
  109. getContinueGeneratingButton: () => getContinueGeneratingButton,
  110. getConversation: () => getConversation,
  111. getHistoryBlockTitle: () => getHistoryBlockTitle,
  112. getHistoryBlocks: () => getHistoryBlocks,
  113. getHistoryBlocksWithTitle: () => getHistoryBlocksWithTitle,
  114. getLastResponse: () => getLastResponse,
  115. getLastResponseElement: () => getLastResponseElement,
  116. getModelSelectButton: () => getModelSelectButton,
  117. getNav: () => getNav,
  118. getNewModelSelectButtons: () => getNewModelSelectButtons,
  119. getRegenerateButton: () => getRegenerateButton,
  120. getStopGeneratingButton: () => getStopGeneratingButton,
  121. getSubmitButton: () => getSubmitButton,
  122. getTextarea: () => getTextarea,
  123. getTextareaValue: () => getTextareaValue,
  124. hasNewModelSelectButtons: () => hasNewModelSelectButtons,
  125. isConversationStarted: () => isConversationStarted,
  126. isGenerating: () => isGenerating,
  127. isHorizontalConversation: () => isHorizontalConversation,
  128. onSend: () => onSend,
  129. regenerate: () => regenerate,
  130. send: () => send,
  131. setHorizontalConversation: () => setHorizontalConversation,
  132. setPromptListener: () => setPromptListener2,
  133. setPureConversation: () => setPureConversation,
  134. setTextarea: () => setTextarea,
  135. waitForIdle: () => waitForIdle
  136. });
  137. module.exports = __toCommonJS(chatgpt_exports);
  138. function getNav() {
  139. return document.querySelector("nav");
  140. }
  141. function getHistoryBlocks() {
  142. const nav = getNav();
  143. if (!nav)
  144. return [];
  145. const result = Array.from(nav.querySelectorAll("ol")).map((ol) => ol.parentElement);
  146. return result;
  147. }
  148. function getHistoryBlockTitle(historyBlock) {
  149. var _a;
  150. return ((_a = historyBlock.querySelector("h3")) == null ? void 0 : _a.textContent) || "";
  151. }
  152. function getHistoryBlocksWithTitle() {
  153. const historyBlocks = getHistoryBlocks();
  154. const result = historyBlocks.map((historyBlock) => ({
  155. block: historyBlock,
  156. title: getHistoryBlockTitle(historyBlock)
  157. }));
  158. return result;
  159. }
  160. function getTextarea() {
  161. const form = document.querySelector("form");
  162. if (!form)
  163. return;
  164. const textareas = form.querySelectorAll("textarea");
  165. const result = textareas[0];
  166. return result;
  167. }
  168. function getSubmitButton() {
  169. const textarea = getTextarea();
  170. if (!textarea)
  171. return;
  172. return textarea.nextElementSibling;
  173. }
  174. function getRegenerateButton() {
  175. const form = document.querySelector("form");
  176. if (!form)
  177. return;
  178. const buttons = form.querySelectorAll("button");
  179. const result = Array.from(buttons).find((button) => {
  180. var _a;
  181. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("regenerate");
  182. });
  183. return result;
  184. }
  185. function getContinueGeneratingButton() {
  186. const form = document.querySelector("form");
  187. if (!form)
  188. return;
  189. const buttons = form.querySelectorAll("button");
  190. const result = Array.from(buttons).find((button) => {
  191. var _a;
  192. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("continue generating");
  193. });
  194. return result;
  195. }
  196. function getStopGeneratingButton() {
  197. const form = document.querySelector("form");
  198. if (!form)
  199. return;
  200. const buttons = form.querySelectorAll("button");
  201. const result = Array.from(buttons).find((button) => {
  202. var _a;
  203. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("stop generating");
  204. });
  205. return result;
  206. }
  207. function getLastResponseElement() {
  208. const responseElements = document.querySelectorAll(".group.w-full");
  209. return responseElements[responseElements.length - 1];
  210. }
  211. function getLastResponse() {
  212. const lastResponseElement = getLastResponseElement();
  213. if (!lastResponseElement)
  214. return;
  215. const lastResponse = lastResponseElement.textContent;
  216. return lastResponse;
  217. }
  218. function getTextareaValue() {
  219. var _a;
  220. return ((_a = getTextarea()) == null ? void 0 : _a.value) || "";
  221. }
  222. function setTextarea(message) {
  223. const textarea = getTextarea();
  224. if (!textarea)
  225. return;
  226. textarea.value = message;
  227. textarea.dispatchEvent(new Event("input", { bubbles: true }));
  228. }
  229. function send(message) {
  230. return __async(this, null, function* () {
  231. setTextarea(message);
  232. const textarea = getTextarea();
  233. if (!textarea)
  234. return;
  235. while (textarea.value === message) {
  236. textarea.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
  237. yield new Promise((resolve) => setTimeout(resolve, 100));
  238. }
  239. });
  240. }
  241. function regenerate() {
  242. const regenerateButton = getRegenerateButton();
  243. if (!regenerateButton)
  244. return;
  245. regenerateButton.click();
  246. }
  247. function onSend(callback) {
  248. const textarea = getTextarea();
  249. if (!textarea)
  250. return;
  251. textarea.addEventListener("keydown", function(event) {
  252. if (event.key === "Enter" && !event.shiftKey) {
  253. callback();
  254. }
  255. });
  256. const sendButton = getSubmitButton();
  257. if (!sendButton)
  258. return;
  259. sendButton.addEventListener("mousedown", callback);
  260. }
  261. function isGenerating() {
  262. var _a, _b;
  263. return ((_b = (_a = getSubmitButton()) == null ? void 0 : _a.firstElementChild) == null ? void 0 : _b.childElementCount) === 3;
  264. }
  265. function waitForIdle() {
  266. return new Promise((resolve) => {
  267. const interval = setInterval(() => {
  268. if (!isGenerating()) {
  269. clearInterval(interval);
  270. resolve();
  271. }
  272. }, 1e3);
  273. });
  274. }
  275. function setPromptListener2(key = "prompt_texts") {
  276. let last_trigger_time = +/* @__PURE__ */ new Date();
  277. if (location.href.includes("chat.openai")) {
  278. GM_addValueChangeListener(key, (name, old_value, new_value) => __async(this, null, function* () {
  279. if (+/* @__PURE__ */ new Date() - last_trigger_time < 500) {
  280. return;
  281. }
  282. last_trigger_time = +/* @__PURE__ */ new Date();
  283. setTimeout(() => __async(this, null, function* () {
  284. const prompt_texts = new_value;
  285. if (prompt_texts.length > 0) {
  286. let firstTime = true;
  287. while (prompt_texts.length > 0) {
  288. if (!firstTime) {
  289. yield new Promise((resolve) => setTimeout(resolve, 2e3));
  290. }
  291. if (!firstTime && isGenerating()) {
  292. continue;
  293. }
  294. firstTime = false;
  295. const prompt_text = prompt_texts.shift() || "";
  296. yield send(prompt_text);
  297. }
  298. }
  299. }), 0);
  300. GM_setValue(key, []);
  301. }));
  302. }
  303. }
  304. function getConversation() {
  305. var _a, _b;
  306. return (_b = (_a = document.querySelector('div[class^="react-scroll-to-bottom"]')) == null ? void 0 : _a.firstChild) == null ? void 0 : _b.firstChild;
  307. }
  308. function getModelSelectButton() {
  309. const conversation = getConversation();
  310. if (!conversation)
  311. return;
  312. return Array.from(conversation.querySelectorAll("button")).find((button) => {
  313. var _a;
  314. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("model");
  315. });
  316. }
  317. function getNewModelSelectButtons() {
  318. return Array.from(document.querySelectorAll("[class^='group/button']"));
  319. }
  320. function hasNewModelSelectButtons() {
  321. return getNewModelSelectButtons().length > 0;
  322. }
  323. function isConversationStarted() {
  324. return !getModelSelectButton();
  325. }
  326. function setPureConversation() {
  327. const conversation = getConversation();
  328. if (!conversation)
  329. return;
  330. const firstChild = conversation.firstChild;
  331. if (!firstChild)
  332. return;
  333. const newDiv = document.createElement("div");
  334. conversation.insertBefore(newDiv, firstChild.nextSibling);
  335. }
  336. function isHorizontalConversation() {
  337. const conversation = getConversation();
  338. if (!conversation)
  339. return true;
  340. if (!isConversationStarted())
  341. return true;
  342. return conversation.classList.contains("grid");
  343. }
  344. function setHorizontalConversation() {
  345. if (isHorizontalConversation())
  346. return;
  347. setPureConversation();
  348. const conversation = getConversation();
  349. if (!conversation)
  350. return;
  351. conversation.classList.remove("flex", "flex-col", "items-center");
  352. conversation.classList.add("grid", "grid-cols-2", "place-items-center");
  353. }
  354. }
  355. });
  356.  
  357. // ../../packages/chatkit/chatgpt.js
  358. var require_chatgpt2 = __commonJS({
  359. "../../packages/chatkit/chatgpt.js"(exports, module) {
  360. module.exports = require_chatgpt();
  361. }
  362. });
  363.  
  364. // src/index.ts
  365. var import_chatgpt = __toESM(require_chatgpt2(), 1);
  366.  
  367. // src/createButton/index.ts
  368. function createButton(callback) {
  369. if (window.location.href.includes("chat.openai")) {
  370. return;
  371. }
  372. const hideRight = document.title.match(/[\u4e00-\u9fa5]/) ? "-130px" : "-120px";
  373. const button = document.createElement("button");
  374. button.innerHTML = "\u7F51\u9875\u7FFB\u8BD1";
  375. button.style.position = "fixed";
  376. button.style.width = "140px";
  377. button.style.top = "120px";
  378. button.style.right = hideRight;
  379. button.style.zIndex = "999999";
  380. button.style.backgroundColor = "#4285f4";
  381. button.style.color = "#fff";
  382. button.style.opacity = "0.8";
  383. button.style.border = "none";
  384. button.style.borderRadius = "4px";
  385. button.style.padding = "10px 16px";
  386. button.style.fontSize = "18px";
  387. button.style.cursor = "pointer";
  388. button.style.transition = "right 0.3s";
  389. document.body.appendChild(button);
  390. button.addEventListener("mouseenter", () => {
  391. button.style.right = "-10px";
  392. });
  393. button.addEventListener("mouseleave", () => {
  394. button.style.right = hideRight;
  395. });
  396. document.addEventListener("fullscreenchange", () => {
  397. if (document.fullscreenElement) {
  398. button.style.display = "none";
  399. } else {
  400. button.style.display = "block";
  401. }
  402. });
  403. button.addEventListener("click", callback);
  404. }
  405. var createButton_default = createButton;
  406.  
  407. // src/SimpleArticleSegmentation/index.ts
  408. var MIN_PARAGRAPH_LENGTH = 1600;
  409. var MAX_PARAGRAPH_LENGTH = 1800;
  410. var SimpleArticleSegmentation = class {
  411. constructor(text) {
  412. this.text = text;
  413. }
  414. segment() {
  415. const paragraphs = [];
  416. const sentences = this.text.split(new RegExp("(?<=[.!?])\\s+"));
  417. let paragraph = "";
  418. for (const sentence of sentences) {
  419. if (paragraph.length + sentence.length + 1 <= MAX_PARAGRAPH_LENGTH) {
  420. paragraph += (paragraph.length > 0 ? " " : "") + sentence;
  421. } else {
  422. if (paragraph.length >= MIN_PARAGRAPH_LENGTH) {
  423. paragraphs.push(paragraph);
  424. paragraph = sentence;
  425. } else {
  426. paragraph += " " + sentence;
  427. }
  428. }
  429. }
  430. if (paragraph.length >= MIN_PARAGRAPH_LENGTH) {
  431. paragraphs.push(paragraph);
  432. }
  433. return paragraphs;
  434. }
  435. };
  436. var SimpleArticleSegmentation_default = SimpleArticleSegmentation;
  437.  
  438. // src/getParagraphs/index.ts
  439. function getParagraphs() {
  440. try {
  441. let docClone = document.cloneNode(true);
  442. let article = new Readability(docClone).parse();
  443. if (article && article.textContent) {
  444. const segmenter = new SimpleArticleSegmentation_default(article.textContent);
  445. const paragraphs = segmenter.segment();
  446. for (let i = 0; i < paragraphs.length; i++) {
  447. paragraphs[i] = paragraphs[i].trim();
  448. }
  449. return paragraphs;
  450. } else {
  451. console.warn("Readability.js could not extract any text content from this page.");
  452. return [];
  453. }
  454. } catch (error) {
  455. console.error("An error occurred while using Readability.js:", error);
  456. return [];
  457. }
  458. }
  459. var getParagraphs_default = getParagraphs;
  460.  
  461. // src/MenuManger/index.ts
  462. var MenuManager = class {
  463. constructor(default_menu_all) {
  464. this.default_menu_all = default_menu_all;
  465. this.menu_all = GM_getValue("menu_all", this.default_menu_all);
  466. for (const name in this.default_menu_all) {
  467. if (!(name in this.menu_all)) {
  468. this.menu_all[name] = this.default_menu_all[name];
  469. }
  470. }
  471. this.menu_id = GM_getValue("menu_id", {});
  472. this.update_menu();
  473. }
  474. registerMenuCommand(name, value) {
  475. if (name === "chat_language") {
  476. return GM_registerMenuCommand(`${name}\uFF1A${value}`, () => {
  477. const language = prompt("Please input the language you want to use", value.toString());
  478. if (language) {
  479. this.menu_all[name] = language;
  480. GM_setValue("menu_all", this.menu_all);
  481. this.update_menu();
  482. location.reload();
  483. }
  484. });
  485. }
  486. const menuText = ` ${name}\uFF1A${value ? "\u2705" : "\u274C"}`;
  487. const commandCallback = () => {
  488. this.menu_all[name] = !this.menu_all[name];
  489. GM_setValue("menu_all", this.menu_all);
  490. this.update_menu();
  491. location.reload();
  492. };
  493. return GM_registerMenuCommand(menuText, commandCallback);
  494. }
  495. update_menu() {
  496. for (const name in this.menu_all) {
  497. const value = this.menu_all[name];
  498. if (this.menu_id[name]) {
  499. GM_unregisterMenuCommand(this.menu_id[name]);
  500. }
  501. this.menu_id[name] = this.registerMenuCommand(name, value);
  502. }
  503. GM_setValue("menu_id", this.menu_id);
  504. }
  505. getMenuValue(name) {
  506. return this.menu_all[name];
  507. }
  508. };
  509. var MenuManger_default = MenuManager;
  510.  
  511. // ../../packages/monkit/dist/index.mjs
  512. function getLocalLanguage() {
  513. const userLanguage = navigator.language;
  514. const languageNames = new Intl.DisplayNames([userLanguage], { type: "language" });
  515. const readableLanguage = languageNames.of(userLanguage);
  516. return readableLanguage;
  517. }
  518.  
  519. // src/index.ts
  520. function initialize() {
  521. return __async(this, null, function* () {
  522. yield new Promise((resolve) => window.addEventListener("load", resolve));
  523. yield new Promise((resolve) => setTimeout(resolve, 1e3));
  524. });
  525. }
  526. function main() {
  527. return __async(this, null, function* () {
  528. yield initialize();
  529. const defaultMenu = {
  530. "chat_language": getLocalLanguage() || "Chinese"
  531. };
  532. const menuManager = new MenuManger_default(defaultMenu);
  533. const chatLanguage = menuManager.getMenuValue("chat_language");
  534. const key = "prompt_texts";
  535. (0, import_chatgpt.setPromptListener)(key);
  536. const translateWeb = () => __async(this, null, function* () {
  537. const paragraphs = getParagraphs_default();
  538. const prompt_texts = paragraphs.map((paragraph) => {
  539. return `"""
  540. ${paragraph}
  541. """
  542. translate above paragraphs in """ to ${chatLanguage} with compact and intuitive format (use Markdown syntax to optimize the display format):`;
  543. });
  544. GM_setValue(key, prompt_texts);
  545. });
  546. createButton_default(translateWeb);
  547. });
  548. }
  549. (function() {
  550. main();
  551. })();
  552. })();