Greasy Fork 还支持 简体中文。

chatgpt-infinite

Infinite auto ask for chatgpt

目前為 2023-04-19 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name chatgpt-infinite
  3. // @namespace https://github.com/mefengl
  4. // @version 0.3.6
  5. // @description Infinite auto ask for chatgpt
  6. // @icon https://www.google.com/s2/favicons?sz=64&domain=openai.com
  7. // @author mefengl
  8. // @match https://chat.openai.com/*
  9. // @grant none
  10. // @license MIT
  11.  
  12. // @name:en ChatGPT Infinite
  13. // @description:en Infinite auto ask for chatgpt
  14. // @name:zh-CN ChatGPT无限
  15. // @description:zh-CN 无限自动询问ChatGPT
  16. // @name:es ChatGPT Infinito
  17. // @description:es Preguntas automáticas infinitas para chatgpt
  18. // @name:hi चैटजीपीटी अनंत
  19. // @description:hi चैटजीपीटी के लिए अनंत स्वचालित पूछताछ
  20. // @name:ar ChatGPT لانهائي
  21. // @description:ar طلب تلقائي لا نهائي لـ chatgpt
  22. // @name:pt ChatGPT Infinito
  23. // @description:pt Perguntas automáticas infinitas para chatgpt
  24. // @name:ru ChatGPT Бесконечный
  25. // @description:ru Бесконечный автоматический запрос для chatgpt
  26. // @name:ja ChatGPTインフィニティ
  27. // @description:ja ChatGPTの無限自動質問
  28. // @name:de ChatGPT Unendlich
  29. // @description:de Unendliches automatisches Fragen für ChatGPT
  30. // @name:fr ChatGPT Infini
  31. // @description:fr Questions automatiques infinies pour chatgpt
  32. // ==/UserScript==
  33. (() => {
  34. var __async = (__this, __arguments, generator) => {
  35. return new Promise((resolve, reject) => {
  36. var fulfilled = (value) => {
  37. try {
  38. step(generator.next(value));
  39. } catch (e) {
  40. reject(e);
  41. }
  42. };
  43. var rejected = (value) => {
  44. try {
  45. step(generator.throw(value));
  46. } catch (e) {
  47. reject(e);
  48. }
  49. };
  50. var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
  51. step((generator = generator.apply(__this, __arguments)).next());
  52. });
  53. };
  54.  
  55. // ../../packages/chatkit/dist/index.mjs
  56. function getTextarea() {
  57. const form = document.querySelector("form");
  58. if (!form)
  59. return;
  60. const textareas = form.querySelectorAll("textarea");
  61. const result = textareas[0];
  62. return result;
  63. }
  64. function getSubmitButton() {
  65. const textarea = getTextarea();
  66. if (!textarea)
  67. return;
  68. return textarea.nextElementSibling;
  69. }
  70. function getRegenerateButton() {
  71. const form = document.querySelector("form");
  72. if (!form)
  73. return;
  74. const buttons = form.querySelectorAll("button");
  75. const result = Array.from(buttons).find((button) => {
  76. var _a;
  77. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("regenerate");
  78. });
  79. return result;
  80. }
  81. function getStopGeneratingButton() {
  82. const form = document.querySelector("form");
  83. if (!form)
  84. return;
  85. const buttons = form.querySelectorAll("button");
  86. const result = Array.from(buttons).find((button) => {
  87. var _a;
  88. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("stop generating");
  89. });
  90. return result;
  91. }
  92. function getLastResponseElement() {
  93. const responseElements = document.querySelectorAll(".group.w-full");
  94. return responseElements[responseElements.length - 1];
  95. }
  96. function getLastResponse() {
  97. const lastResponseElement = getLastResponseElement();
  98. if (!lastResponseElement)
  99. return;
  100. const lastResponse = lastResponseElement.textContent;
  101. return lastResponse;
  102. }
  103. function getTextareaValue() {
  104. var _a;
  105. return ((_a = getTextarea()) == null ? void 0 : _a.value) || "";
  106. }
  107. function setTextarea(message) {
  108. const textarea = getTextarea();
  109. if (!textarea)
  110. return;
  111. textarea.value = message;
  112. textarea.dispatchEvent(new Event("input"));
  113. }
  114. function send(message) {
  115. setTextarea(message);
  116. const textarea = getTextarea();
  117. if (!textarea)
  118. return;
  119. textarea.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
  120. }
  121. function regenerate() {
  122. const regenerateButton = getRegenerateButton();
  123. if (!regenerateButton)
  124. return;
  125. regenerateButton.click();
  126. }
  127. function onSend(callback) {
  128. const textarea = getTextarea();
  129. if (!textarea)
  130. return;
  131. textarea.addEventListener("keydown", function(event) {
  132. if (event.key === "Enter" && !event.shiftKey) {
  133. callback();
  134. }
  135. });
  136. const sendButton = getSubmitButton();
  137. if (!sendButton)
  138. return;
  139. sendButton.addEventListener("mousedown", callback);
  140. }
  141. function isGenerating() {
  142. var _a, _b;
  143. return ((_b = (_a = getSubmitButton()) == null ? void 0 : _a.firstElementChild) == null ? void 0 : _b.childElementCount) === 3;
  144. }
  145. function waitForIdle() {
  146. return new Promise((resolve) => {
  147. const interval = setInterval(() => {
  148. if (!isGenerating()) {
  149. clearInterval(interval);
  150. resolve();
  151. }
  152. }, 1e3);
  153. });
  154. }
  155. function setPromptListener(key = "prompt_texts") {
  156. let last_trigger_time = +/* @__PURE__ */ new Date();
  157. if (location.href.includes("chat.openai")) {
  158. GM_addValueChangeListener(key, (name, old_value, new_value) => __async(this, null, function* () {
  159. if (+/* @__PURE__ */ new Date() - last_trigger_time < 500) {
  160. return;
  161. }
  162. last_trigger_time = +/* @__PURE__ */ new Date();
  163. setTimeout(() => __async(this, null, function* () {
  164. const prompt_texts = new_value;
  165. if (prompt_texts.length > 0) {
  166. let firstTime = true;
  167. while (prompt_texts.length > 0) {
  168. if (!firstTime) {
  169. yield new Promise((resolve) => setTimeout(resolve, 2e3));
  170. }
  171. if (!firstTime && chatgpt.isGenerating()) {
  172. continue;
  173. }
  174. firstTime = false;
  175. const prompt_text = prompt_texts.shift() || "";
  176. chatgpt.send(prompt_text);
  177. }
  178. }
  179. }), 0);
  180. GM_setValue(key, []);
  181. }));
  182. }
  183. }
  184. function getConversation() {
  185. var _a, _b;
  186. return (_b = (_a = document.querySelector('div[class^="react-scroll-to-bottom"]')) == null ? void 0 : _a.firstChild) == null ? void 0 : _b.firstChild;
  187. }
  188. function getModelSelectButton() {
  189. const conversation = getConversation();
  190. if (!conversation)
  191. return;
  192. return Array.from(conversation.querySelectorAll("button")).find((button) => {
  193. var _a;
  194. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("model");
  195. });
  196. }
  197. function isConversationStarted() {
  198. return !getModelSelectButton();
  199. }
  200. function setPureConversation() {
  201. const conversation = getConversation();
  202. if (!conversation)
  203. return;
  204. const firstChild = conversation.firstChild;
  205. if (!firstChild)
  206. return;
  207. const newDiv = document.createElement("div");
  208. conversation.insertBefore(newDiv, firstChild.nextSibling);
  209. }
  210. function isHorizontalConversation() {
  211. const conversation = getConversation();
  212. if (!conversation)
  213. return true;
  214. if (!isConversationStarted())
  215. return true;
  216. return conversation.classList.contains("grid");
  217. }
  218. function setHorizontalConversation() {
  219. if (isHorizontalConversation())
  220. return;
  221. setPureConversation();
  222. const conversation = getConversation();
  223. if (!conversation)
  224. return;
  225. conversation.classList.remove("flex", "flex-col", "items-center");
  226. conversation.classList.add("grid", "grid-cols-2", "place-items-center");
  227. }
  228. var chatgpt = {
  229. getTextarea,
  230. getSubmitButton,
  231. getRegenerateButton,
  232. getStopGeneratingButton,
  233. getLastResponseElement,
  234. getLastResponse,
  235. getTextareaValue,
  236. setTextarea,
  237. send,
  238. regenerate,
  239. onSend,
  240. isGenerating,
  241. waitForIdle,
  242. setPromptListener,
  243. getConversation,
  244. getModelSelectButton,
  245. isConversationStarted,
  246. setPureConversation,
  247. isHorizontalConversation,
  248. setHorizontalConversation
  249. };
  250. var chatgpt_default = chatgpt;
  251.  
  252. // src/askForLanguage/index.ts
  253. function askForLanguage() {
  254. return __async(this, null, function* () {
  255. return prompt("What language do you want to use?");
  256. });
  257. }
  258.  
  259. // src/infiniteLoop/index.ts
  260. function startInfiniteLoop() {
  261. return __async(this, null, function* () {
  262. const language = yield askForLanguage();
  263. if (!language)
  264. return;
  265. chatgpt_default.send(`you can only answer question in ${language} language`);
  266. yield chatgpt_default.waitForIdle();
  267. while (true) {
  268. const lastResponse = chatgpt_default.getLastResponse();
  269. const question = extractQuestion(lastResponse);
  270. yield chatgpt_default.send(question + "\nanswer above question, and show me one more further question I can ask in the end prefixed with Q:");
  271. yield chatgpt_default.waitForIdle();
  272. yield sleep(1e4);
  273. }
  274. });
  275. }
  276. function extractQuestion(text) {
  277. return text.split("Q:").pop().trim();
  278. }
  279. function sleep(ms) {
  280. return new Promise((resolve) => setTimeout(resolve, ms));
  281. }
  282. var infiniteLoop_default = startInfiniteLoop;
  283.  
  284. // src/index.ts
  285. function initialize() {
  286. return __async(this, null, function* () {
  287. yield new Promise((resolve) => window.addEventListener("load", resolve));
  288. yield new Promise((resolve) => setTimeout(resolve, 1e3));
  289. });
  290. }
  291. function main() {
  292. return __async(this, null, function* () {
  293. yield initialize();
  294. infiniteLoop_default();
  295. });
  296. }
  297. (function() {
  298. main();
  299. })();
  300. })();