chat-octopus

let octopus send message for you

当前为 2023-04-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name chat-octopus
  3. // @namespace https://github.com/mefengl
  4. // @version 0.2.11
  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. // ../../packages/chatkit/dist/index.mjs
  43. function getTextarea() {
  44. const form = document.querySelector("form");
  45. if (!form)
  46. return;
  47. const textareas = form.querySelectorAll("textarea");
  48. const result = textareas[0];
  49. return result;
  50. }
  51. function getSubmitButton() {
  52. const form = document.querySelector("form");
  53. if (!form)
  54. return;
  55. const buttons = form.querySelectorAll("button");
  56. const result = buttons[buttons.length - 1];
  57. return result;
  58. }
  59. function getRegenerateButton() {
  60. const form = document.querySelector("form");
  61. if (!form)
  62. return;
  63. const buttons = form.querySelectorAll("button");
  64. const result = Array.from(buttons).find((button) => {
  65. var _a;
  66. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("regenerate");
  67. });
  68. return result;
  69. }
  70. function getStopGeneratingButton() {
  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("stop generating");
  78. });
  79. return result;
  80. }
  81. function getLastResponseElement() {
  82. const responseElements = document.querySelectorAll(".group.w-full");
  83. return responseElements[responseElements.length - 1];
  84. }
  85. function getLastResponse() {
  86. const lastResponseElement = getLastResponseElement();
  87. if (!lastResponseElement)
  88. return;
  89. const lastResponse = lastResponseElement.textContent;
  90. return lastResponse;
  91. }
  92. function getTextareaValue() {
  93. var _a;
  94. return ((_a = getTextarea()) == null ? void 0 : _a.value) || "";
  95. }
  96. function setTextarea(message) {
  97. const textarea = getTextarea();
  98. if (!textarea)
  99. return;
  100. textarea.value = message;
  101. textarea.dispatchEvent(new Event("input"));
  102. }
  103. function send(message) {
  104. setTextarea(message);
  105. const textarea = getTextarea();
  106. if (!textarea)
  107. return;
  108. textarea.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
  109. }
  110. function onSend(callback) {
  111. const textarea = getTextarea();
  112. if (!textarea)
  113. return;
  114. textarea.addEventListener("keydown", function(event) {
  115. if (event.key === "Enter" && !event.shiftKey) {
  116. callback();
  117. }
  118. });
  119. const sendButton = getSubmitButton();
  120. if (!sendButton)
  121. return;
  122. sendButton.addEventListener("mousedown", callback);
  123. }
  124. function waitForIdle() {
  125. return new Promise((resolve) => {
  126. const interval = setInterval(() => {
  127. if (!getStopGeneratingButton()) {
  128. clearInterval(interval);
  129. resolve();
  130. }
  131. }, 1e3);
  132. });
  133. }
  134. var chatgpt = {
  135. getTextarea,
  136. getSubmitButton,
  137. getRegenerateButton,
  138. getStopGeneratingButton,
  139. getLastResponseElement,
  140. getLastResponse,
  141. getTextareaValue,
  142. setTextarea,
  143. send,
  144. onSend,
  145. waitForIdle
  146. };
  147. var chatgpt_default = chatgpt;
  148. function getSubmitButton2() {
  149. return document.querySelector('button[aria-label="Send message"]');
  150. }
  151. function getInputArea() {
  152. return document.querySelector(".input-area");
  153. }
  154. function getTextarea2() {
  155. const inputArea = getInputArea();
  156. return inputArea ? inputArea.querySelector("textarea") : null;
  157. }
  158. function getRegenerateButton2() {
  159. return document.querySelector('button[aria-label="Retry"]');
  160. }
  161. function getLastPrompt() {
  162. const promptElements = document.querySelectorAll(".query-text");
  163. const lastPrompt = promptElements[promptElements.length - 1];
  164. return lastPrompt;
  165. }
  166. function getLatestPromptText() {
  167. const lastPrompt = getLastPrompt();
  168. if (!lastPrompt)
  169. return "";
  170. const lastPromptText = lastPrompt.textContent;
  171. return lastPromptText || "";
  172. }
  173. function send2(text) {
  174. const textarea = getTextarea2();
  175. if (!textarea)
  176. return;
  177. textarea.value = text;
  178. textarea.dispatchEvent(new Event("input"));
  179. const submitButton = getSubmitButton2();
  180. if (!submitButton)
  181. return;
  182. submitButton.click();
  183. }
  184. function onSend2(callback) {
  185. const textarea = getTextarea2();
  186. if (!textarea)
  187. return;
  188. textarea.addEventListener("keydown", function(event) {
  189. if (event.key === "Enter" && !event.shiftKey) {
  190. callback();
  191. }
  192. });
  193. const sendButton = getSubmitButton2();
  194. if (!sendButton)
  195. return;
  196. sendButton.addEventListener("mousedown", callback);
  197. }
  198. var bard = {
  199. getSubmitButton: getSubmitButton2,
  200. getInputArea,
  201. getTextarea: getTextarea2,
  202. getRegenerateButton: getRegenerateButton2,
  203. getLastPrompt,
  204. getLatestPromptText,
  205. send: send2,
  206. onSend: onSend2
  207. };
  208. var bard_default = bard;
  209. function getActionBar() {
  210. var _a, _b, _c;
  211. 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) || null;
  212. }
  213. function getSubmitButton3() {
  214. const actionBar = getActionBar();
  215. if (!actionBar) {
  216. return null;
  217. }
  218. return actionBar.querySelector('button[aria-label="Submit"]');
  219. }
  220. function getTextarea3() {
  221. const actionBar = getActionBar();
  222. if (!actionBar) {
  223. return null;
  224. }
  225. return actionBar.querySelector("textarea");
  226. }
  227. function getStopGeneratingButton2() {
  228. var _a, _b;
  229. const actionBar = getActionBar();
  230. if (!actionBar) {
  231. return null;
  232. }
  233. const stopGeneratingButton = (_b = (_a = actionBar.querySelector("cib-typing-indicator")) == null ? void 0 : _a.shadowRoot) == null ? void 0 : _b.querySelector('button[aria-label="Stop Responding"]');
  234. if (!stopGeneratingButton) {
  235. return null;
  236. }
  237. if (stopGeneratingButton.disabled) {
  238. return null;
  239. }
  240. return stopGeneratingButton;
  241. }
  242. function getNewChatButton() {
  243. const actionBar = getActionBar();
  244. if (!actionBar) {
  245. return null;
  246. }
  247. return actionBar.querySelector('button[aria-label="New topic"]');
  248. }
  249. function getConversation() {
  250. var _a, _b, _c;
  251. 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) || null;
  252. }
  253. function getChatTurns() {
  254. const conversation = getConversation();
  255. if (!conversation) {
  256. return null;
  257. }
  258. return Array.from(conversation.querySelectorAll("cib-chat-turn")).map((t) => t.shadowRoot);
  259. }
  260. function getLastChatTurn() {
  261. const chatTurns = getChatTurns();
  262. if (!chatTurns) {
  263. return null;
  264. }
  265. return chatTurns[chatTurns.length - 1];
  266. }
  267. function getLastResponse2() {
  268. var _a;
  269. const lastChatTurn = getLastChatTurn();
  270. if (!lastChatTurn) {
  271. return null;
  272. }
  273. return ((_a = lastChatTurn.querySelectorAll("cib-message-group")[1]) == null ? void 0 : _a.shadowRoot) || null;
  274. }
  275. function getLastResponseText() {
  276. var _a;
  277. const lastResponse = getLastResponse2();
  278. if (!lastResponse) {
  279. return null;
  280. }
  281. const message = Array.from(lastResponse.querySelectorAll("cib-message")).map((m) => m.shadowRoot).find((m) => m == null ? void 0 : m.querySelector("cib-shared"));
  282. return ((_a = message == null ? void 0 : message.textContent) == null ? void 0 : _a.trim()) || null;
  283. }
  284. function send3(text) {
  285. const textarea = getTextarea3();
  286. if (!textarea) {
  287. return;
  288. }
  289. textarea.value = text;
  290. textarea.dispatchEvent(new Event("input"));
  291. const submitButton = getSubmitButton3();
  292. if (!submitButton) {
  293. return;
  294. }
  295. submitButton.click();
  296. }
  297. function onSend3(callback) {
  298. const textarea = getTextarea3();
  299. if (!textarea)
  300. return;
  301. textarea.addEventListener("keydown", function(event) {
  302. if (event.key === "Enter" && !event.shiftKey) {
  303. callback();
  304. }
  305. });
  306. const sendButton = getSubmitButton3();
  307. if (!sendButton)
  308. return;
  309. sendButton.addEventListener("mousedown", callback);
  310. }
  311. var bingchat = {
  312. getActionBar,
  313. getSubmitButton: getSubmitButton3,
  314. getTextarea: getTextarea3,
  315. getStopGeneratingButton: getStopGeneratingButton2,
  316. getNewChatButton,
  317. getConversation,
  318. getChatTurns,
  319. getLastChatTurn,
  320. getLastResponse: getLastResponse2,
  321. getLastResponseText,
  322. send: send3,
  323. onSend: onSend3
  324. };
  325. var bingchat_default = bingchat;
  326.  
  327. // src/index.js
  328. (function() {
  329. "use strict";
  330. const default_menu_all = {};
  331. const menu_all = GM_getValue("menu_all", default_menu_all);
  332. const menus = [
  333. { checker: () => location.href.includes("chat.openai"), name: "openai", value: true },
  334. { checker: () => location.href.includes("bard.google"), name: "bard", value: true },
  335. { checker: () => location.href.includes("Bing+AI"), name: "bing", value: true }
  336. ];
  337. menus.forEach((menu) => {
  338. $(() => menu.checker() && GM_setValue(menu.name, true));
  339. if (GM_getValue(menu.name) == true) {
  340. default_menu_all[menu.name] = menu.value;
  341. }
  342. });
  343. for (let name in default_menu_all) {
  344. if (!(name in menu_all)) {
  345. menu_all[name] = default_menu_all[name];
  346. }
  347. }
  348. const menu_id = GM_getValue("menu_id", {});
  349. function registerMenuCommand(name, value) {
  350. const menuText = ` ${name}\uFF1A${value ? "\u2705" : "\u274C"}`;
  351. const commandCallback = () => {
  352. menu_all[name] = !menu_all[name];
  353. GM_setValue("menu_all", menu_all);
  354. update_menu();
  355. location.reload();
  356. };
  357. return GM_registerMenuCommand(menuText, commandCallback);
  358. }
  359. function update_menu() {
  360. for (let name in menu_all) {
  361. const value = menu_all[name];
  362. if (menu_id[name]) {
  363. GM_unregisterMenuCommand(menu_id[name]);
  364. }
  365. menu_id[name] = registerMenuCommand(name, value);
  366. }
  367. GM_setValue("menu_id", menu_id);
  368. }
  369. update_menu();
  370. let chatgpt_last_prompt = "";
  371. $(() => {
  372. if (menu_all.openai && location.href.includes("chat.openai")) {
  373. chatgpt_default.onSend(() => {
  374. const textarea = chatgpt_default.getTextarea();
  375. const prompt = textarea.value;
  376. chatgpt_last_prompt = prompt;
  377. GM_setValue("bard_prompt_texts", [prompt]);
  378. GM_setValue("bing_prompt_texts", [prompt]);
  379. });
  380. }
  381. });
  382. let last_trigger_time = +/* @__PURE__ */ new Date();
  383. $(() => {
  384. if (location.href.includes("chat.openai")) {
  385. GM_addValueChangeListener("chatgpt_prompt_texts", (name, old_value, new_value) => {
  386. if (+/* @__PURE__ */ new Date() - last_trigger_time < 500) {
  387. return;
  388. }
  389. last_trigger_time = +/* @__PURE__ */ new Date();
  390. setTimeout(() => __async(this, null, function* () {
  391. const prompt_texts = new_value;
  392. if (prompt_texts.length > 0) {
  393. let firstTime = true;
  394. while (prompt_texts.length > 0) {
  395. if (!firstTime) {
  396. yield new Promise((resolve) => setTimeout(resolve, 2e3));
  397. }
  398. if (!firstTime && chatgpt_default.getRegenerateButton() == void 0) {
  399. continue;
  400. }
  401. firstTime = false;
  402. const prompt_text = prompt_texts.shift();
  403. if (prompt_text === chatgpt_last_prompt) {
  404. continue;
  405. }
  406. chatgpt_default.send(prompt_text);
  407. }
  408. }
  409. }), 0);
  410. GM_setValue("chatgpt_prompt_texts", []);
  411. });
  412. }
  413. });
  414. let bard_last_prompt = "";
  415. $(() => __async(this, null, function* () {
  416. if (menu_all.bard && location.href.includes("bard.google")) {
  417. while (!bard_default.getSubmitButton()) {
  418. yield new Promise((resolve) => setTimeout(resolve, 500));
  419. }
  420. bard_default.onSend(() => {
  421. const textarea = bard_default.getTextarea();
  422. let prompt = textarea.value;
  423. if (!prompt) {
  424. prompt = bard_default.getLatestPromptText();
  425. }
  426. bard_last_prompt = prompt;
  427. GM_setValue("chatgpt_prompt_texts", [prompt]);
  428. GM_setValue("bing_prompt_texts", [prompt]);
  429. });
  430. }
  431. }));
  432. let lastTriggerTime = +/* @__PURE__ */ new Date();
  433. if (location.href.includes("bard.google")) {
  434. GM_addValueChangeListener("bard_prompt_texts", (name, old_value, new_value) => {
  435. if (+/* @__PURE__ */ new Date() - lastTriggerTime < 500) {
  436. return;
  437. }
  438. lastTriggerTime = +/* @__PURE__ */ new Date();
  439. setTimeout(() => __async(this, null, function* () {
  440. const promptTexts = new_value;
  441. if (promptTexts.length > 0) {
  442. let firstTime = true;
  443. while (promptTexts.length > 0) {
  444. if (!firstTime) {
  445. yield new Promise((resolve) => setTimeout(resolve, 2e3));
  446. }
  447. if (!firstTime && bard_default.getRegenerateButton() == void 0) {
  448. continue;
  449. }
  450. firstTime = false;
  451. const promptText = promptTexts.shift();
  452. if (promptText === bard_last_prompt) {
  453. continue;
  454. }
  455. bard_default.send(promptText);
  456. }
  457. }
  458. }), 0);
  459. GM_setValue("bard_prompt_texts", []);
  460. });
  461. }
  462. let bing_last_prompt = "";
  463. $(() => __async(this, null, function* () {
  464. if (menu_all.bing && location.href.includes("Bing+AI")) {
  465. while (!bingchat_default.getSubmitButton()) {
  466. yield new Promise((resolve) => setTimeout(resolve, 500));
  467. }
  468. bingchat_default.onSend(() => {
  469. const textarea = bingchat_default.getTextarea();
  470. const prompt = textarea.value;
  471. bing_last_prompt = prompt;
  472. GM_setValue("chatgpt_prompt_texts", [prompt]);
  473. GM_setValue("bard_prompt_texts", [prompt]);
  474. });
  475. }
  476. }));
  477. let last_trigger_time_bing = +/* @__PURE__ */ new Date();
  478. if (location.href.includes("Bing+AI")) {
  479. GM_addValueChangeListener("bing_prompt_texts", (name, old_value, new_value) => {
  480. if (+/* @__PURE__ */ new Date() - last_trigger_time_bing < 500) {
  481. return;
  482. }
  483. last_trigger_time_bing = /* @__PURE__ */ new Date();
  484. setTimeout(() => __async(this, null, function* () {
  485. const prompt_texts = new_value;
  486. if (prompt_texts.length > 0) {
  487. let firstTime = true;
  488. while (prompt_texts.length > 0) {
  489. if (!firstTime) {
  490. yield new Promise((resolve) => setTimeout(resolve, 2e3));
  491. }
  492. if (!firstTime && bingchat_default.getStopGeneratingButton() != void 0) {
  493. continue;
  494. }
  495. firstTime = false;
  496. const prompt_text = prompt_texts.shift();
  497. if (prompt_text === bing_last_prompt) {
  498. continue;
  499. }
  500. bingchat_default.send(prompt_text);
  501. }
  502. }
  503. }), 0);
  504. GM_setValue("bing_prompt_texts", []);
  505. });
  506. }
  507. })();
  508. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址