ChatGPT 自动继续 🔄

⚡ 自动点击ChatGPT中的“继续生成”按钮,处理错误!

目前为 2023-11-10 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name ChatGPT Auto-Continue 🔄
  3. // @description ⚡ Automatically click the 'Continue Generating' button in ChatGPT, handling errors!
  4. // @author mefengl
  5. // @version 1.1.2
  6. // @namespace https://github.com/mefengl
  7. // @icon https://www.google.com/s2/favicons?sz=64&domain=openai.com
  8. // @license MIT
  9. // @match https://chat.openai.com/*
  10. // @grant none
  11.  
  12. // @name:en ChatGPT Auto-Continue 🔄
  13. // @description:en ⚡ Automatically click the 'Continue Generating' button in ChatGPT, handling errors!
  14. // @name:zh-CN ChatGPT 自动继续 🔄
  15. // @description:zh-CN ⚡ 自动点击ChatGPT中的“继续生成”按钮,处理错误!
  16. // @name:es ChatGPT Auto-Continuar 🔄
  17. // @description:es ⚡ ¡Haz clic automáticamente en el botón 'Continuar generando' en ChatGPT, manejando errores!
  18. // @name:hi ChatGPT स्वचालित जारी रखें 🔄
  19. // @description:hi ⚡ ChatGPT में 'जारी रखने' बटन पर स्वचालित रूप से क्लिक करें, त्रुटियों को संभालते हुए!
  20. // @name:ar ChatGPT التكميل الآلي 🔄
  21. // @description:ar ⚡ انقر تلقائيًا على زر 'متابعة التوليد' في ChatGPT ، معالجة الأخطاء!
  22. // @name:pt ChatGPT Auto-Continuar 🔄
  23. // @description:pt ⚡ Clique automaticamente no botão 'Continuar Gerando' no ChatGPT, tratando erros!
  24. // @name:ru ChatGPT Авто-Продолжение 🔄
  25. // @description:ru ⚡ Автоматически нажимайте на кнопку "Продолжить генерацию" в ChatGPT, обрабатывая ошибки!
  26. // @name:ja ChatGPT 自動続行 🔄
  27. // @description:ja ⚡ ChatGPTの「続けて生成」ボタンを自動的にクリックし、エラーを処理します!
  28. // @name:de ChatGPT Auto-Fortsetzen 🔄
  29. // @description:de ⚡ Klicken Sie automatisch auf die Schaltfläche "Generierung fortsetzen" in ChatGPT, Fehler behandeln!
  30. // @name:fr ChatGPT Auto-Continuer 🔄
  31. // @description:fr ⚡ Cliquez automatiquement sur le bouton 'Continuer à générer' dans ChatGPT, gérer les erreurs!
  32. // ==/UserScript==
  33. "use strict";
  34. (() => {
  35. var __create = Object.create;
  36. var __defProp = Object.defineProperty;
  37. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  38. var __getOwnPropNames = Object.getOwnPropertyNames;
  39. var __getProtoOf = Object.getPrototypeOf;
  40. var __hasOwnProp = Object.prototype.hasOwnProperty;
  41. var __commonJS = (cb, mod) => function __require() {
  42. return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  43. };
  44. var __copyProps = (to, from, except, desc) => {
  45. if (from && typeof from === "object" || typeof from === "function") {
  46. for (let key of __getOwnPropNames(from))
  47. if (!__hasOwnProp.call(to, key) && key !== except)
  48. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  49. }
  50. return to;
  51. };
  52. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  53. // If the importer is in node compatibility mode or this is not an ESM
  54. // file that has been converted to a CommonJS file using a Babel-
  55. // compatible transform (i.e. "__esModule" has not been set), then set
  56. // "default" to the CommonJS "module.exports" for node compatibility.
  57. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  58. mod
  59. ));
  60. var __async = (__this, __arguments, generator) => {
  61. return new Promise((resolve, reject) => {
  62. var fulfilled = (value) => {
  63. try {
  64. step(generator.next(value));
  65. } catch (e) {
  66. reject(e);
  67. }
  68. };
  69. var rejected = (value) => {
  70. try {
  71. step(generator.throw(value));
  72. } catch (e) {
  73. reject(e);
  74. }
  75. };
  76. var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
  77. step((generator = generator.apply(__this, __arguments)).next());
  78. });
  79. };
  80.  
  81. // ../../packages/chatkit/dist/chatgpt/index.js
  82. var require_chatgpt = __commonJS({
  83. "../../packages/chatkit/dist/chatgpt/index.js"(exports, module) {
  84. "use strict";
  85. var __defProp2 = Object.defineProperty;
  86. var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
  87. var __getOwnPropNames2 = Object.getOwnPropertyNames;
  88. var __hasOwnProp2 = Object.prototype.hasOwnProperty;
  89. var __export = (target, all) => {
  90. for (var name in all)
  91. __defProp2(target, name, { get: all[name], enumerable: true });
  92. };
  93. var __copyProps2 = (to, from, except, desc) => {
  94. if (from && typeof from === "object" || typeof from === "function") {
  95. for (let key of __getOwnPropNames2(from))
  96. if (!__hasOwnProp2.call(to, key) && key !== except)
  97. __defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
  98. }
  99. return to;
  100. };
  101. var __toCommonJS = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
  102. var chatgpt_exports = {};
  103. __export(chatgpt_exports, {
  104. clickFollowUpButton: () => clickFollowUpButton,
  105. getButton: () => getButton,
  106. getContinueGeneratingButton: () => getContinueGeneratingButton2,
  107. getConversation: () => getConversation,
  108. getCopyLinkButton: () => getCopyLinkButton,
  109. getFollowUpButtons: () => getFollowUpButtons,
  110. getHistoryBlockTitle: () => getHistoryBlockTitle,
  111. getHistoryBlocks: () => getHistoryBlocks,
  112. getHistoryBlocksWithTitle: () => getHistoryBlocksWithTitle,
  113. getInitialButtons: () => getInitialButtons,
  114. getLastResponse: () => getLastResponse,
  115. getLastResponseElement: () => getLastResponseElement,
  116. getModelSelectButton: () => getModelSelectButton,
  117. getNav: () => getNav,
  118. getNewModelSelectButtons: () => getNewModelSelectButtons,
  119. getRegenerateButton: () => getRegenerateButton2,
  120. getResponseElementHTMLs: () => getResponseElementHTMLs,
  121. getShareChatButton: () => getShareChatButton,
  122. getStopGeneratingButton: () => getStopGeneratingButton,
  123. getSubmitButton: () => getSubmitButton,
  124. getTextarea: () => getTextarea2,
  125. getTextareaValue: () => getTextareaValue,
  126. hasNewModelSelectButtons: () => hasNewModelSelectButtons,
  127. isConversationStarted: () => isConversationStarted,
  128. isGenerating: () => isGenerating2,
  129. isHorizontalConversation: () => isHorizontalConversation,
  130. onSend: () => onSend,
  131. regenerate: () => regenerate,
  132. send: () => send,
  133. sendArray: () => sendArray,
  134. setHorizontalConversation: () => setHorizontalConversation,
  135. setPromptListener: () => setPromptListener,
  136. setPureConversation: () => setPureConversation,
  137. setTextarea: () => setTextarea,
  138. waitForIdle: () => waitForIdle
  139. });
  140. module.exports = __toCommonJS(chatgpt_exports);
  141. function getNav() {
  142. return document.querySelector("nav");
  143. }
  144. function getHistoryBlocks() {
  145. const nav = getNav();
  146. if (!nav)
  147. return [];
  148. const result = Array.from(nav.querySelectorAll("ol")).map((ol) => ol.parentElement);
  149. return result;
  150. }
  151. function getHistoryBlockTitle(historyBlock) {
  152. var _a;
  153. return ((_a = historyBlock.querySelector("h3")) == null ? void 0 : _a.textContent) || "";
  154. }
  155. function getHistoryBlocksWithTitle() {
  156. const historyBlocks = getHistoryBlocks();
  157. const result = historyBlocks.map((historyBlock) => ({
  158. block: historyBlock,
  159. title: getHistoryBlockTitle(historyBlock)
  160. }));
  161. return result;
  162. }
  163. function getTextarea2() {
  164. const form = document.querySelector("form");
  165. if (!form)
  166. return;
  167. const textareas = form.querySelectorAll("textarea");
  168. const result = textareas[0];
  169. return result;
  170. }
  171. function getSubmitButton() {
  172. const textarea = getTextarea2();
  173. if (!textarea)
  174. return;
  175. return textarea.nextElementSibling;
  176. }
  177. function getInitialButtons() {
  178. return Array.from(document.querySelectorAll('button[as="button"]')).filter((button) => button.querySelectorAll(".truncate").length === 2);
  179. }
  180. function getFollowUpButtons() {
  181. return Array.from(document.querySelectorAll('button[as="button"]')).filter((button) => {
  182. var _a;
  183. return (_a = button.textContent) == null ? void 0 : _a.trim().match(/[.!?]$/);
  184. });
  185. }
  186. function clickFollowUpButton(index) {
  187. const followUpButtons = getFollowUpButtons();
  188. if (followUpButtons.length === 0)
  189. return;
  190. if (index === void 0 || index < 0 || index >= followUpButtons.length) {
  191. index = Math.floor(Math.random() * followUpButtons.length);
  192. }
  193. followUpButtons[index].click();
  194. }
  195. function getButton(text) {
  196. return Array.from(document.querySelectorAll('button[as="button"]')).find((button) => {
  197. var _a;
  198. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes(text);
  199. });
  200. }
  201. function getRegenerateButton2() {
  202. return getButton("regenerate");
  203. }
  204. function getContinueGeneratingButton2() {
  205. return getButton("continue");
  206. }
  207. function getStopGeneratingButton() {
  208. return getButton("stop");
  209. }
  210. function getResponseElementHTMLs() {
  211. return Array.from(document.querySelectorAll(".markdown")).map((m) => m.innerHTML);
  212. }
  213. function getLastResponseElement() {
  214. const responseElements = document.querySelectorAll(".group.w-full");
  215. return responseElements[responseElements.length - 1];
  216. }
  217. function getLastResponse() {
  218. const lastResponseElement = getLastResponseElement();
  219. if (!lastResponseElement)
  220. return;
  221. const lastResponse = lastResponseElement.textContent;
  222. return lastResponse;
  223. }
  224. function getTextareaValue() {
  225. var _a;
  226. return ((_a = getTextarea2()) == null ? void 0 : _a.value) || "";
  227. }
  228. function setTextarea(message) {
  229. const textarea = getTextarea2();
  230. if (!textarea)
  231. return;
  232. textarea.value = message;
  233. textarea.dispatchEvent(new Event("input", { bubbles: true }));
  234. }
  235. function send(message) {
  236. return __async(this, null, function* () {
  237. setTextarea(message);
  238. const textarea = getTextarea2();
  239. if (!textarea)
  240. return;
  241. while (textarea.value === message) {
  242. textarea.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
  243. yield new Promise((resolve) => setTimeout(resolve, 100));
  244. }
  245. for (let i = 0; i < 10; i++) {
  246. if (isGenerating2()) {
  247. break;
  248. }
  249. yield new Promise((resolve) => setTimeout(resolve, 1e3));
  250. }
  251. });
  252. }
  253. function regenerate() {
  254. const regenerateButton = getRegenerateButton2();
  255. if (!regenerateButton)
  256. return;
  257. regenerateButton.click();
  258. }
  259. function onSend(callback) {
  260. const textarea = getTextarea2();
  261. if (!textarea)
  262. return;
  263. textarea.addEventListener("keydown", function(event) {
  264. if (event.key === "Enter" && !event.shiftKey) {
  265. callback();
  266. }
  267. });
  268. const sendButton = getSubmitButton();
  269. if (!sendButton)
  270. return;
  271. sendButton.addEventListener("mousedown", callback);
  272. }
  273. function isGenerating2() {
  274. var _a, _b;
  275. return ((_b = (_a = getSubmitButton()) == null ? void 0 : _a.firstElementChild) == null ? void 0 : _b.childElementCount) === 3;
  276. }
  277. function waitForIdle() {
  278. return new Promise((resolve) => {
  279. const interval = setInterval(() => {
  280. if (!isGenerating2()) {
  281. clearInterval(interval);
  282. resolve();
  283. }
  284. }, 1e3);
  285. });
  286. }
  287. function sendArray(messages) {
  288. return __async(this, null, function* () {
  289. var _a, _b;
  290. let firstTime = true;
  291. const isLong = messages.length > 60;
  292. while (messages.length > 0) {
  293. const waitTime = isLong && !document.hasFocus() ? 20 * 1e3 : 2e3;
  294. if (!firstTime) {
  295. yield new Promise((resolve) => setTimeout(resolve, waitTime));
  296. }
  297. if (isGenerating2()) {
  298. continue;
  299. } else if (getContinueGeneratingButton2()) {
  300. (_a = getContinueGeneratingButton2()) == null ? void 0 : _a.click();
  301. continue;
  302. } else if (getRegenerateButton2() && !getTextarea2()) {
  303. yield new Promise((resolve) => setTimeout(resolve, 10 * 1e3));
  304. (_b = getRegenerateButton2()) == null ? void 0 : _b.click();
  305. continue;
  306. }
  307. firstTime = false;
  308. if (messages.length === 0) {
  309. break;
  310. }
  311. yield send(messages.shift() || "");
  312. }
  313. });
  314. }
  315. function setPromptListener(key = "prompt_texts") {
  316. let last_trigger_time = +/* @__PURE__ */ new Date();
  317. if (location.href.includes("chat.openai")) {
  318. GM_addValueChangeListener(key, (name, old_value, new_value) => __async(this, null, function* () {
  319. if (+/* @__PURE__ */ new Date() - last_trigger_time < 500) {
  320. return;
  321. }
  322. last_trigger_time = +/* @__PURE__ */ new Date();
  323. setTimeout(() => __async(this, null, function* () {
  324. sendArray(new_value);
  325. GM_setValue(key, []);
  326. }), 0);
  327. }));
  328. }
  329. }
  330. function getConversation() {
  331. var _a, _b;
  332. return (_b = (_a = document.querySelector('div[class^="react-scroll-to-bottom"]')) == null ? void 0 : _a.firstChild) == null ? void 0 : _b.firstChild;
  333. }
  334. function getModelSelectButton() {
  335. const conversation = getConversation();
  336. if (!conversation)
  337. return;
  338. return Array.from(conversation.querySelectorAll("button")).find((button) => {
  339. var _a;
  340. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("model");
  341. });
  342. }
  343. function getNewModelSelectButtons() {
  344. return Array.from(document.querySelectorAll("[class^='group/button']"));
  345. }
  346. function hasNewModelSelectButtons() {
  347. return getNewModelSelectButtons().length > 0;
  348. }
  349. function isConversationStarted() {
  350. return !getModelSelectButton();
  351. }
  352. function setPureConversation() {
  353. const conversation = getConversation();
  354. if (!conversation)
  355. return;
  356. const firstChild = conversation.firstChild;
  357. if (!firstChild)
  358. return;
  359. const newDiv = document.createElement("div");
  360. conversation.insertBefore(newDiv, firstChild.nextSibling);
  361. }
  362. function isHorizontalConversation() {
  363. const conversation = getConversation();
  364. if (!conversation)
  365. return true;
  366. if (!isConversationStarted())
  367. return true;
  368. return conversation.classList.contains("grid");
  369. }
  370. function setHorizontalConversation() {
  371. if (isHorizontalConversation())
  372. return;
  373. setPureConversation();
  374. const conversation = getConversation();
  375. if (!conversation)
  376. return;
  377. conversation.classList.remove("flex", "flex-col", "items-center");
  378. conversation.classList.add("grid", "grid-cols-2", "place-items-center");
  379. }
  380. function getShareChatButton() {
  381. return document.querySelector('button[aria-label="Share chat"]');
  382. }
  383. function getCopyLinkButton() {
  384. return Array.from(document.querySelectorAll('button[as="button"]')).filter((button) => {
  385. var _a;
  386. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("copy link");
  387. })[0];
  388. }
  389. }
  390. });
  391.  
  392. // ../../packages/chatkit/chatgpt.js
  393. var require_chatgpt2 = __commonJS({
  394. "../../packages/chatkit/chatgpt.js"(exports, module) {
  395. module.exports = require_chatgpt();
  396. }
  397. });
  398.  
  399. // src/index.ts
  400. var import_chatgpt = __toESM(require_chatgpt2(), 1);
  401. var retryCount = 0;
  402. var maxRetries = 3;
  403. var lastRetryTime = null;
  404. function initialize() {
  405. return __async(this, null, function* () {
  406. yield new Promise((resolve) => window.addEventListener("load", resolve));
  407. yield new Promise((resolve) => setTimeout(resolve, 1e3));
  408. });
  409. }
  410. function main() {
  411. return __async(this, null, function* () {
  412. yield initialize();
  413. let firstTime = true;
  414. setInterval(() => __async(this, null, function* () {
  415. var _a, _b;
  416. const currentTime = (/* @__PURE__ */ new Date()).getTime();
  417. if (lastRetryTime && currentTime - lastRetryTime >= 5 * 60 * 1e3) {
  418. retryCount = 0;
  419. }
  420. while (true) {
  421. const waitTime = !document.hasFocus() ? 20 * 1e3 : 2e3;
  422. if (!firstTime) {
  423. yield new Promise((resolve) => setTimeout(resolve, waitTime));
  424. }
  425. if (!firstTime && (0, import_chatgpt.isGenerating)()) {
  426. continue;
  427. } else if ((0, import_chatgpt.getContinueGeneratingButton)()) {
  428. (_a = (0, import_chatgpt.getContinueGeneratingButton)()) == null ? void 0 : _a.click();
  429. continue;
  430. } else if ((0, import_chatgpt.getRegenerateButton)() && !(0, import_chatgpt.getTextarea)()) {
  431. if (retryCount < maxRetries) {
  432. yield new Promise((resolve) => setTimeout(resolve, 2 * 1e3));
  433. (_b = (0, import_chatgpt.getRegenerateButton)()) == null ? void 0 : _b.click();
  434. retryCount++;
  435. lastRetryTime = (/* @__PURE__ */ new Date()).getTime();
  436. continue;
  437. } else {
  438. console.error("Failed to regenerate after 3 attempts. Stopping retries.");
  439. break;
  440. }
  441. }
  442. firstTime = false;
  443. break;
  444. }
  445. }), 1e3);
  446. });
  447. }
  448. (function() {
  449. main();
  450. })();
  451. })();