chat-octopus

let octopus send message for you

当前为 2023-03-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name chat-octopus
  3. // @namespace https://github.com/mefengl
  4. // @version 0.0.8
  5. // @description let octopus send message for you
  6. // @icon https://www.google.com/s2/favicons?sz=64&domain=openai.com
  7. // @author mefengl
  8. // @match https://chat.openai.com/*
  9. // @match https://bard.google.com/*
  10. // @require https://cdn.staticfile.org/jquery/3.6.1/jquery.min.js
  11. // @grant GM_openInTab
  12. // @grant GM_registerMenuCommand
  13. // @grant GM_unregisterMenuCommand
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_addValueChangeListener
  17. // @license MIT
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. 'use strict';
  22.  
  23. const default_menu_all = {
  24. };
  25. const menu_all = GM_getValue("menu_all", default_menu_all);
  26. // 菜单更新的逻辑
  27. const menus = [
  28. { checker: () => location.href.includes("chat.openai"), name: "openai", value: true },
  29. { checker: () => location.href.includes("bard.google"), name: "bard", value: true },
  30. ];
  31.  
  32. menus.forEach(menu => {
  33. $(() => menu.checker() && GM_setValue(menu.name, true));
  34. if (GM_getValue(menu.name) == true) {
  35. default_menu_all[menu.name] = menu.value;
  36. }
  37. });
  38.  
  39. // 检查是否有新增菜单
  40. for (let name in default_menu_all) {
  41. if (!(name in menu_all)) {
  42. menu_all[name] = default_menu_all[name];
  43. }
  44. }
  45. const menu_id = GM_getValue("menu_id", {});
  46.  
  47. function registerMenuCommand(name, value) {
  48. const menuText = ` ${name}:${value ? '✅' : '❌'}`;
  49. const commandCallback = () => {
  50. menu_all[name] = !menu_all[name];
  51. GM_setValue('menu_all', menu_all);
  52. update_menu();
  53. location.reload();
  54. };
  55. return GM_registerMenuCommand(menuText, commandCallback);
  56. }
  57. function update_menu() {
  58. for (let name in menu_all) {
  59. const value = menu_all[name];
  60. if (menu_id[name]) {
  61. GM_unregisterMenuCommand(menu_id[name]);
  62. }
  63. menu_id[name] = registerMenuCommand(name, value);
  64. }
  65. GM_setValue('menu_id', menu_id);
  66. }
  67. update_menu();
  68.  
  69. /* ************************************************************************* */
  70. const get_submit_button = () => {
  71. const form = document.querySelector('form');
  72. const buttons = form.querySelectorAll('button');
  73. const result = buttons[buttons.length - 1]; // by textContent maybe better
  74. return result;
  75. };
  76. const get_textarea = () => {
  77. const form = document.querySelector('form');
  78. const textareas = form.querySelectorAll('textarea');
  79. const result = textareas[0];
  80. return result;
  81. };
  82. const get_regenerate_button = () => {
  83. const form = document.querySelector('form');
  84. const buttons = form.querySelectorAll('button');
  85. for (let i = 0; i < buttons.length; i++) {
  86. const buttonText = buttons[i].textContent.trim().toLowerCase();
  87. if (buttonText.includes('regenerate')) {
  88. return buttons[i];
  89. }
  90. }
  91. };
  92. const chatgpt_send = (text) => {
  93. const textarea = get_textarea();
  94. textarea.value = text;
  95. textarea.dispatchEvent(new Event('input'));
  96. const submitButton = get_submit_button();
  97. submitButton.click();
  98. };
  99. // ChatGPT send prompt to other ai
  100. let chatgpt_last_prompt = '';
  101. $(() => {
  102. if (menu_all.openai && location.href.includes("chat.openai")) {
  103. const submit_button = get_submit_button();
  104. submit_button.addEventListener('click', () => {
  105. const textarea = get_textarea();
  106. const prompt = textarea.value;
  107. chatgpt_last_prompt = prompt;
  108. GM_setValue('bard_prompt_texts', [prompt]);
  109. })
  110. }
  111. });
  112. // ChatGPT response to prompt comes from other ai
  113. let last_trigger_time = +new Date();
  114. $(() => {
  115. if (location.href.includes("chat.openai")) {
  116. console.log("chatgpt add value change listener");
  117. GM_addValueChangeListener("chatgpt_prompt_texts", (name, old_value, new_value) => {
  118. console.log("prompt_texts changed in chatgpt");
  119. console.log(new_value);
  120. if (+new Date() - last_trigger_time < 500) {
  121. return;
  122. }
  123. last_trigger_time = new Date();
  124. setTimeout(async () => {
  125. const prompt_texts = new_value;
  126. if (prompt_texts.length > 0) {
  127. // get prompt_texts from local
  128. let firstTime = true;
  129. while (prompt_texts.length > 0) {
  130. if (!firstTime) { await new Promise(resolve => setTimeout(resolve, 2000)); }
  131. if (!firstTime && get_regenerate_button() == undefined) { continue; }
  132. firstTime = false;
  133. const prompt_text = prompt_texts.shift();
  134. if (prompt_text === chatgpt_last_prompt) { continue; }
  135. console.log("chatgpt send prompt_text", prompt_text);
  136. chatgpt_send(prompt_text);
  137. }
  138. }
  139. }, 0);
  140. GM_setValue("chatgpt_prompt_texts", []);
  141. });
  142. }
  143. });
  144.  
  145. /* ************************************************************************* */
  146. function getSubmitButton() {
  147. return document.querySelector('button[aria-label="Send message"]');
  148. };
  149. function getInputArea() {
  150. return document.querySelector(".input-area");
  151. };
  152. function getTextarea() {
  153. const inputArea = getInputArea();
  154. return inputArea.querySelector('textarea');
  155. };
  156. function getRegenerateButton() {
  157. return document.querySelector('button[aria-label="Retry"]');
  158. };
  159. function bard_send(text) {
  160. const textarea = getTextarea();
  161. textarea.value = text;
  162. textarea.dispatchEvent(new Event('input'));
  163. const submitButton = getSubmitButton();
  164. submitButton.click();
  165. };
  166. // Bard send prompt to other ai
  167. let bard_last_prompt = "";
  168. $(async () => {
  169. if (menu_all.bard && location.href.includes("bard.google")) {
  170. while (!getSubmitButton()) { await new Promise(resolve => setTimeout(resolve, 500)); }
  171. const submit_button = getSubmitButton();
  172. submit_button.addEventListener('mousedown', () => {
  173. console.log("bard send");
  174. const textarea = getTextarea();
  175. const prompt = textarea.value;
  176. console.log(prompt);
  177. bard_last_prompt = prompt;
  178. GM_setValue('chatgpt_prompt_texts', [prompt]);
  179. })
  180. }
  181. });
  182. // Bard response to prompt_texts
  183. let lastTriggerTime = +new Date();
  184. if (location.href.includes("bard.google")) {
  185. GM_addValueChangeListener("bard_prompt_texts", (name, old_value, new_value) => {
  186. if (+new Date() - lastTriggerTime < 500) {
  187. return;
  188. }
  189. lastTriggerTime = new Date();
  190. setTimeout(async () => {
  191. const promptTexts = new_value;
  192. if (promptTexts.length > 0) {
  193. // get promptTexts from local
  194. let firstTime = true;
  195. while (promptTexts.length > 0) {
  196. if (!firstTime) { await new Promise(resolve => setTimeout(resolve, 2000)); }
  197. if (!firstTime && getRegenerateButton() == undefined) { continue; }
  198. firstTime = false;
  199. const promptText = promptTexts.shift();
  200. if (promptText === bard_last_prompt) { continue; }
  201. bard_send(promptText);
  202. }
  203. }
  204. }, 0);
  205. GM_setValue("bard_prompt_texts", []);
  206. });
  207. }
  208. })();
  209.