GPT Auto task

自动在网页上与chat gpt对话

当前为 2023-06-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @namespace https://greasyfork.org/zh-CN/users/1106595-ikkem-lin
  3. // @name GPT Auto task
  4. // @author Mark
  5. // @description 自动在网页上与chat gpt对话
  6. // @homepageURL https://github.com/IKKEM-Lin/gpt-auto-task
  7. // @version 0.0.2
  8. // @match *chat.openai.com/*
  9. // @run-at document-idle
  10. // ==/UserScript==
  11. (function () {
  12. "use strict";
  13. class GPT_ASK_LOOP {
  14. queue = [];
  15. responds = [];
  16. checkInterval = 10000;
  17. account = "";
  18. constructor(account) {
  19. this.responds = JSON.parse(
  20. localStorage.getItem("reaction_responds") || "[]"
  21. );
  22. const queueCache = JSON.parse(localStorage.getItem("task_queue") || "[]");
  23. const resSnippetIds = this.responds.map((respond) => respond.snippetId);
  24. this.queue = queueCache.filter(
  25. (item) => !resSnippetIds.includes(item.id)
  26. );
  27. this.account = account || Math.ceil(Math.random() * 1e10).toString(32);
  28. this.main();
  29. }
  30. async report() {
  31. await fetch("https://gpt-hit.deno.dev/api/update", {
  32. method: "POST",
  33. body: JSON.stringify({
  34. account: this.account,
  35. reaction_count: this.responds.length,
  36. queue_count: this.queue.length,
  37. }),
  38. }).catch(err => {
  39. console.error({err})
  40. });
  41. }
  42. genPrompt(content) {
  43. return `${localStorage.getItem("mock_prompt")}
  44. ''' ${content} ''' `;
  45. }
  46. getTask() {
  47. this.report();
  48. const task = this.queue[0];
  49. if (!task) {
  50. console.log("任务队列为空");
  51. return;
  52. }
  53. return async () => {
  54. const { article_id, id, content } = task;
  55. console.log(
  56. `开始触发 ${article_id}-${id}, ${new Date().toTimeString()}`
  57. );
  58. const prompt = this.genPrompt(content);
  59. const result = await this.trigger(prompt);
  60. return { articleId: article_id, snippetId: id, reaction: result };
  61. // console.log("result:", result);
  62. };
  63. }
  64. saveRespond(respond) {
  65. const { snippetId } = respond;
  66. this.responds.push(respond);
  67. this.queue = this.queue.filter((item) => item.id !== snippetId);
  68. localStorage.setItem("task_queue", JSON.stringify(this.queue));
  69. localStorage.setItem("reaction_responds", JSON.stringify(this.responds));
  70. }
  71. sleep(duration) {
  72. return new Promise((resolve, reject) => {
  73. setTimeout(() => {
  74. resolve(true);
  75. }, duration);
  76. });
  77. }
  78. trigger(prompt, checkInterval = this.checkInterval) {
  79. return new Promise((resolve, reject) => {
  80. const textEl = document.querySelector("#prompt-textarea");
  81. const submitEl = document.querySelector("#prompt-textarea + button");
  82. textEl.value = prompt; //`你好, 帮我算下${Math.floor(Math.random() * 10000)}开平方的结果`;
  83. textEl.dispatchEvent(new Event("input", { bubbles: true }));
  84. setTimeout(() => {
  85. submitEl.click();
  86. let resCache = null;
  87. (async () => {
  88. while (true) {
  89. await this.sleep(checkInterval);
  90. const result = Array.from(
  91. document.querySelectorAll("main .group")
  92. );
  93. const temp = result[result.length - 1];
  94. if (!temp) {
  95. continue;
  96. }
  97. if (resCache === temp.innerHTML) {
  98. // console.log("匹配,resCache:", resCache);
  99. resolve(resCache);
  100. break;
  101. }
  102. resCache = temp.innerHTML;
  103. console.log(`${checkInterval / 1000}s后再次检查结果`);
  104. }
  105. })();
  106. }, 4000);
  107. });
  108. }
  109. async main(sleepTime = 5000) {
  110. while (true) {
  111. const task = this.getTask();
  112. if (!task) {
  113. await this.sleep(5 * 60 * 1000);
  114. continue;
  115. }
  116. const result = await task();
  117. this.saveRespond(result);
  118. console.log(`${sleepTime / 1000}s后将再次触发`);
  119. const newChatBtn = document.querySelector("nav>div.mb-1>a:first-child");
  120. newChatBtn.click();
  121. await this.sleep(sleepTime);
  122. }
  123. }
  124. }
  125. function start() {
  126. const name = document.querySelector('nav > div:last-child > div:last-child').innerText;
  127. if (name) {
  128. new GPT_ASK_LOOP(name);
  129. } else {
  130. setTimeout(() => {
  131. start()
  132. }, 5000);
  133. }
  134. }
  135. start()
  136. })();