Greasy Fork 还支持 简体中文。

chat-octopus

let octopus send message for you

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

  1. // ==UserScript==
  2. // @name chat-octopus
  3. // @namespace https://github.com/mefengl
  4. // @version 0.2.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. // @match https://www.bing.com/search*q=Bing+AI*
  11. // @require https://cdn.staticfile.org/jquery/3.6.1/jquery.min.js
  12. // @grant GM_openInTab
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_unregisterMenuCommand
  15. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_addValueChangeListener
  18. // @license MIT
  19. // ==/UserScript==
  20. (() => {
  21. var __async = (__this, __arguments, generator) => {
  22. return new Promise((resolve, reject) => {
  23. var fulfilled = (value) => {
  24. try {
  25. step(generator.next(value));
  26. } catch (e) {
  27. reject(e);
  28. }
  29. };
  30. var rejected = (value) => {
  31. try {
  32. step(generator.throw(value));
  33. } catch (e) {
  34. reject(e);
  35. }
  36. };
  37. var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
  38. step((generator = generator.apply(__this, __arguments)).next());
  39. });
  40. };
  41.  
  42. // src/index.js
  43. (function() {
  44. "use strict";
  45. const default_menu_all = {};
  46. const menu_all = GM_getValue("menu_all", default_menu_all);
  47. const menus = [
  48. { checker: () => location.href.includes("chat.openai"), name: "openai", value: true },
  49. { checker: () => location.href.includes("bard.google"), name: "bard", value: true },
  50. { checker: () => location.href.includes("Bing+AI"), name: "bing", value: true }
  51. ];
  52. menus.forEach((menu) => {
  53. $(() => menu.checker() && GM_setValue(menu.name, true));
  54. if (GM_getValue(menu.name) == true) {
  55. default_menu_all[menu.name] = menu.value;
  56. }
  57. });
  58. for (let name in default_menu_all) {
  59. if (!(name in menu_all)) {
  60. menu_all[name] = default_menu_all[name];
  61. }
  62. }
  63. const menu_id = GM_getValue("menu_id", {});
  64. function registerMenuCommand(name, value) {
  65. const menuText = ` ${name}\uFF1A${value ? "\u2705" : "\u274C"}`;
  66. const commandCallback = () => {
  67. menu_all[name] = !menu_all[name];
  68. GM_setValue("menu_all", menu_all);
  69. update_menu();
  70. location.reload();
  71. };
  72. return GM_registerMenuCommand(menuText, commandCallback);
  73. }
  74. function update_menu() {
  75. for (let name in menu_all) {
  76. const value = menu_all[name];
  77. if (menu_id[name]) {
  78. GM_unregisterMenuCommand(menu_id[name]);
  79. }
  80. menu_id[name] = registerMenuCommand(name, value);
  81. }
  82. GM_setValue("menu_id", menu_id);
  83. }
  84. update_menu();
  85. const chatgpt = {
  86. getSubmitButton: function() {
  87. const form = document.querySelector("form");
  88. if (!form)
  89. return;
  90. const buttons = form.querySelectorAll("button");
  91. const result = buttons[buttons.length - 1];
  92. return result;
  93. },
  94. getTextarea: function() {
  95. const form = document.querySelector("form");
  96. if (!form)
  97. return;
  98. const textareas = form.querySelectorAll("textarea");
  99. const result = textareas[0];
  100. return result;
  101. },
  102. getRegenerateButton: function() {
  103. var _a, _b;
  104. const form = document.querySelector("form");
  105. if (!form)
  106. return;
  107. const buttons = form.querySelectorAll("button");
  108. for (let i = 0; i < buttons.length; i++) {
  109. const buttonText = (_b = (_a = buttons[i]) == null ? void 0 : _a.textContent) == null ? void 0 : _b.trim().toLowerCase();
  110. if (buttonText == null ? void 0 : buttonText.includes("regenerate")) {
  111. return buttons[i];
  112. }
  113. }
  114. },
  115. getStopGeneratingButton: function() {
  116. var _a, _b;
  117. const form = document.querySelector("form");
  118. if (!form)
  119. return;
  120. const buttons = form.querySelectorAll("button");
  121. if (buttons.length === 0)
  122. return;
  123. for (let i = 0; i < buttons.length; i++) {
  124. const buttonText = (_b = (_a = buttons[i]) == null ? void 0 : _a.textContent) == null ? void 0 : _b.trim().toLowerCase();
  125. if (buttonText == null ? void 0 : buttonText.includes("stop")) {
  126. return buttons[i];
  127. }
  128. }
  129. },
  130. send: function(text) {
  131. const textarea = this.getTextarea();
  132. if (!textarea)
  133. return;
  134. textarea.value = text;
  135. textarea.dispatchEvent(new Event("input", { bubbles: true }));
  136. textarea.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
  137. },
  138. onSend: function(callback) {
  139. const textarea = this.getTextarea();
  140. if (!textarea)
  141. return;
  142. textarea.addEventListener("keydown", function(event) {
  143. if (event.key === "Enter" && !event.shiftKey) {
  144. callback();
  145. }
  146. });
  147. const sendButton = this.getSubmitButton();
  148. if (!sendButton)
  149. return;
  150. sendButton.addEventListener("mousedown", callback);
  151. }
  152. };
  153. let chatgpt_last_prompt = "";
  154. $(() => {
  155. if (menu_all.openai && location.href.includes("chat.openai")) {
  156. chatgpt.onSend(() => {
  157. const textarea = chatgpt.getTextarea();
  158. const prompt = textarea.value;
  159. chatgpt_last_prompt = prompt;
  160. GM_setValue("bard_prompt_texts", [prompt]);
  161. GM_setValue("bing_prompt_texts", [prompt]);
  162. });
  163. }
  164. });
  165. let last_trigger_time = +/* @__PURE__ */ new Date();
  166. $(() => {
  167. if (location.href.includes("chat.openai")) {
  168. console.log("chatgpt add value change listener");
  169. GM_addValueChangeListener("chatgpt_prompt_texts", (name, old_value, new_value) => {
  170. console.log("prompt_texts changed in chatgpt");
  171. console.log(new_value);
  172. if (+/* @__PURE__ */ new Date() - last_trigger_time < 500) {
  173. return;
  174. }
  175. last_trigger_time = +/* @__PURE__ */ new Date();
  176. setTimeout(() => __async(this, null, function* () {
  177. const prompt_texts = new_value;
  178. if (prompt_texts.length > 0) {
  179. let firstTime = true;
  180. while (prompt_texts.length > 0) {
  181. if (!firstTime) {
  182. yield new Promise((resolve) => setTimeout(resolve, 2e3));
  183. }
  184. if (!firstTime && chatgpt.getRegenerateButton() == void 0) {
  185. continue;
  186. }
  187. firstTime = false;
  188. const prompt_text = prompt_texts.shift();
  189. if (prompt_text === chatgpt_last_prompt) {
  190. continue;
  191. }
  192. console.log("chatgpt send prompt_text", prompt_text);
  193. chatgpt.send(prompt_text);
  194. }
  195. }
  196. }), 0);
  197. GM_setValue("chatgpt_prompt_texts", []);
  198. });
  199. }
  200. });
  201. const bard = {
  202. getSubmitButton: function() {
  203. return document.querySelector('button[aria-label="Send message"]');
  204. },
  205. getInputArea: function() {
  206. return document.querySelector(".input-area");
  207. },
  208. getTextarea: function() {
  209. const inputArea = this.getInputArea();
  210. return inputArea.querySelector("textarea");
  211. },
  212. getRegenerateButton: function() {
  213. return document.querySelector('button[aria-label="Retry"]');
  214. },
  215. getLastPrompt: function() {
  216. const promptElements = document.querySelectorAll(".query-text");
  217. const lastPrompt = promptElements[promptElements.length - 1];
  218. return lastPrompt;
  219. },
  220. getLatestPromptText: function() {
  221. const lastPrompt = this.getLastPrompt();
  222. if (!lastPrompt)
  223. return "";
  224. const lastPromptText = lastPrompt.textContent;
  225. return lastPromptText;
  226. },
  227. send: function(text) {
  228. const textarea = this.getTextarea();
  229. textarea.value = text;
  230. textarea.dispatchEvent(new Event("input"));
  231. const submitButton = this.getSubmitButton();
  232. submitButton.click();
  233. },
  234. onSend: function(callback) {
  235. const textarea = this.getTextarea();
  236. if (!textarea)
  237. return;
  238. textarea.addEventListener("keydown", function(event) {
  239. if (event.key === "Enter" && !event.shiftKey) {
  240. callback();
  241. }
  242. });
  243. const sendButton = this.getSubmitButton();
  244. if (!sendButton)
  245. return;
  246. sendButton.addEventListener("mousedown", callback);
  247. }
  248. };
  249. let bard_last_prompt = "";
  250. $(() => __async(this, null, function* () {
  251. if (menu_all.bard && location.href.includes("bard.google")) {
  252. while (!bard.getSubmitButton()) {
  253. yield new Promise((resolve) => setTimeout(resolve, 500));
  254. }
  255. bard.onSend(() => {
  256. console.log("bard send");
  257. const textarea = bard.getTextarea();
  258. let prompt = textarea.value;
  259. if (!prompt) {
  260. prompt = bard.getLatestPromptText();
  261. }
  262. console.log(prompt);
  263. bard_last_prompt = prompt;
  264. GM_setValue("chatgpt_prompt_texts", [prompt]);
  265. GM_setValue("bing_prompt_texts", [prompt]);
  266. });
  267. }
  268. }));
  269. let lastTriggerTime = +/* @__PURE__ */ new Date();
  270. if (location.href.includes("bard.google")) {
  271. GM_addValueChangeListener("bard_prompt_texts", (name, old_value, new_value) => {
  272. if (+/* @__PURE__ */ new Date() - lastTriggerTime < 500) {
  273. return;
  274. }
  275. lastTriggerTime = +/* @__PURE__ */ new Date();
  276. setTimeout(() => __async(this, null, function* () {
  277. const promptTexts = new_value;
  278. if (promptTexts.length > 0) {
  279. let firstTime = true;
  280. while (promptTexts.length > 0) {
  281. if (!firstTime) {
  282. yield new Promise((resolve) => setTimeout(resolve, 2e3));
  283. }
  284. if (!firstTime && bard.getRegenerateButton() == void 0) {
  285. continue;
  286. }
  287. firstTime = false;
  288. const promptText = promptTexts.shift();
  289. if (promptText === bard_last_prompt) {
  290. continue;
  291. }
  292. bard.send(promptText);
  293. }
  294. }
  295. }), 0);
  296. GM_setValue("bard_prompt_texts", []);
  297. });
  298. }
  299. const bing = {
  300. getActionBar: function() {
  301. var _a, _b, _c;
  302. return (_c = (_b = (_a = document.querySelector("cib-serp")) == null ? void 0 : _a.shadowRoot) == null ? void 0 : _b.querySelector("cib-action-bar")) == null ? void 0 : _c.shadowRoot;
  303. },
  304. getSubmitButton: function() {
  305. const actionBar = this.getActionBar();
  306. if (!actionBar) {
  307. return null;
  308. }
  309. return actionBar.querySelector('button[aria-label="Submit"]');
  310. },
  311. getTextarea: function() {
  312. const actionBar = this.getActionBar();
  313. if (!actionBar) {
  314. return null;
  315. }
  316. return actionBar.querySelector("textarea");
  317. },
  318. getStopGeneratingButton: function() {
  319. var _a, _b;
  320. const actionBar = this.getActionBar();
  321. if (!actionBar) {
  322. return null;
  323. }
  324. const stopGeneratingButton = (_b = (_a = actionBar.querySelector("cib-typing-indicator")) == null ? void 0 : _a.shadowRoot) == null ? void 0 : _b.querySelector('button[aria-label="Stop Responding"]');
  325. if (!stopGeneratingButton) {
  326. return null;
  327. }
  328. if (stopGeneratingButton.disabled) {
  329. return null;
  330. }
  331. return stopGeneratingButton;
  332. },
  333. getNewChatButton: function() {
  334. const actionBar = this.getActionBar();
  335. if (!actionBar) {
  336. return null;
  337. }
  338. return actionBar.querySelector('button[aria-label="New topic"]');
  339. },
  340. getConversation: function() {
  341. var _a, _b, _c;
  342. return (_c = (_b = (_a = document.querySelector("cib-serp")) == null ? void 0 : _a.shadowRoot) == null ? void 0 : _b.querySelector("cib-conversation")) == null ? void 0 : _c.shadowRoot;
  343. },
  344. getChatTurns: function() {
  345. const conversation = this.getConversation();
  346. if (!conversation) {
  347. return null;
  348. }
  349. return Array.from(conversation.querySelectorAll("cib-chat-turn")).map((t) => t.shadowRoot);
  350. },
  351. getLastChatTurn: function() {
  352. const chatTurns = this.getChatTurns();
  353. if (!chatTurns) {
  354. return null;
  355. }
  356. return chatTurns[chatTurns.length - 1];
  357. },
  358. getLastResponse: function() {
  359. var _a;
  360. const lastChatTurn = this.getLastChatTurn();
  361. if (!lastChatTurn) {
  362. return null;
  363. }
  364. return (_a = lastChatTurn.querySelectorAll("cib-message-group")[1]) == null ? void 0 : _a.shadowRoot;
  365. },
  366. getLastResponseText: function() {
  367. const lastResponse = this.getLastResponse();
  368. if (!lastResponse) {
  369. return null;
  370. }
  371. return Array.from(lastResponse.querySelectorAll("cib-message")).map((m) => m.shadowRoot).find((m) => m.querySelector("cib-shared")).textContent.trim();
  372. },
  373. send: function(text) {
  374. const textarea = this.getTextarea();
  375. if (!textarea) {
  376. return null;
  377. }
  378. textarea.value = text;
  379. textarea.dispatchEvent(new Event("input"));
  380. const submitButton = this.getSubmitButton();
  381. if (!submitButton) {
  382. return null;
  383. }
  384. submitButton.click();
  385. },
  386. onSend: function(callback) {
  387. const textarea = this.getTextarea();
  388. if (!textarea)
  389. return;
  390. textarea.addEventListener("keydown", function(event) {
  391. if (event.key === "Enter" && !event.shiftKey) {
  392. callback();
  393. }
  394. });
  395. const sendButton = this.getSubmitButton();
  396. if (!sendButton)
  397. return;
  398. sendButton.addEventListener("mousedown", callback);
  399. }
  400. };
  401. let bing_last_prompt = "";
  402. $(() => __async(this, null, function* () {
  403. if (menu_all.bing && location.href.includes("Bing+AI")) {
  404. console.log("bing");
  405. while (!bing.getSubmitButton()) {
  406. yield new Promise((resolve) => setTimeout(resolve, 500));
  407. }
  408. console.log("get bing submit button");
  409. bing.onSend(() => {
  410. console.log("bing send");
  411. const textarea = bing.getTextarea();
  412. const prompt = textarea.value;
  413. console.log(prompt);
  414. bing_last_prompt = prompt;
  415. GM_setValue("chatgpt_prompt_texts", [prompt]);
  416. GM_setValue("bard_prompt_texts", [prompt]);
  417. });
  418. }
  419. }));
  420. let last_trigger_time_bing = +/* @__PURE__ */ new Date();
  421. if (location.href.includes("Bing+AI")) {
  422. GM_addValueChangeListener("bing_prompt_texts", (name, old_value, new_value) => {
  423. if (+/* @__PURE__ */ new Date() - last_trigger_time_bing < 500) {
  424. return;
  425. }
  426. last_trigger_time_bing = /* @__PURE__ */ new Date();
  427. setTimeout(() => __async(this, null, function* () {
  428. const prompt_texts = new_value;
  429. if (prompt_texts.length > 0) {
  430. let firstTime = true;
  431. while (prompt_texts.length > 0) {
  432. if (!firstTime) {
  433. yield new Promise((resolve) => setTimeout(resolve, 2e3));
  434. }
  435. if (!firstTime && bing.getStopGeneratingButton() != void 0) {
  436. continue;
  437. }
  438. firstTime = false;
  439. const prompt_text = prompt_texts.shift();
  440. if (prompt_text === bing_last_prompt) {
  441. continue;
  442. }
  443. console.log("bing send prompt_text", prompt_text);
  444. bing.send(prompt_text);
  445. }
  446. }
  447. }), 0);
  448. GM_setValue("bing_prompt_texts", []);
  449. });
  450. }
  451. })();
  452. })();