ChatGPT 页面摘要按钮

🍓 让 ChatGPT 一键总结您正在阅读的网页

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

  1. // ==UserScript==
  2. // @name chatgpt-page-summary-button
  3. // @description 🍓 let ChatGPT summary the web page you are reading in one click
  4. // @author mefengl
  5. // @version 0.1.6
  6. // @namespace https://github.com/mefengl
  7. // @require https://cdn.jsdelivr.net/npm/@mozilla/readability@0.4.3/Readability.min.js
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=openai.com
  9. // @license MIT
  10. // @match *://*/*
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // @grant GM_addValueChangeListener
  14.  
  15. // @name:en ChatGPT Page Summary Button
  16. // @description:en 🍓 let ChatGPT summarize the web page you are reading in one click
  17. // @name:zh-CN ChatGPT 页面摘要按钮
  18. // @description:zh-CN 🍓 让 ChatGPT 一键总结您正在阅读的网页
  19. // @name:es Botón de resumen de página de ChatGPT
  20. // @description:es 🍓 permite que ChatGPT resuma la página web que estás leyendo con un solo clic
  21. // @name:hi ChatGPT पृष्ठ सारांश बटन
  22. // @description:hi 🍓 ChatGPT को वेबपेज जो आप पढ़ रहे हैं को एक क्लिक में संक्षेप में देने दें
  23. // @name:ar زر ملخص الصفحة لـ ChatGPT
  24. // @description:ar 🍓 دع ChatGPT يلخص صفحة الويب التي تقرأها بنقرة واحدة
  25. // @name:pt Botão de resumo de página do ChatGPT
  26. // @description:pt 🍓 permita que o ChatGPT resuma a página da web que você está lendo com um clique
  27. // @name:ru Кнопка резюме страницы ChatGPT
  28. // @description:ru 🍓 позволяет ChatGPT кратко описывать веб-страницу, которую вы читаете, одним щелчком мыши
  29. // @name:ja ChatGPTページ要約ボタン
  30. // @description:ja 🍓 ChatGPTで読んでいるWebページをワンクリックで要約
  31. // @name:de ChatGPT-Seitenzusammenfassungs-Button
  32. // @description:de 🍓 Lassen Sie ChatGPT die Webseite, die Sie gerade lesen, mit einem Klick zusammenfassen
  33. // @name:fr Bouton de résumé de page ChatGPT
  34. // @description:fr 🍓 laissez ChatGPT résumer la page Web que vous lisez en un seul clic
  35. // ==/UserScript==
  36. "use strict";
  37. (() => {
  38. var __create = Object.create;
  39. var __defProp = Object.defineProperty;
  40. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  41. var __getOwnPropNames = Object.getOwnPropertyNames;
  42. var __getProtoOf = Object.getPrototypeOf;
  43. var __hasOwnProp = Object.prototype.hasOwnProperty;
  44. var __commonJS = (cb, mod) => function __require() {
  45. return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  46. };
  47. var __copyProps = (to, from, except, desc) => {
  48. if (from && typeof from === "object" || typeof from === "function") {
  49. for (let key of __getOwnPropNames(from))
  50. if (!__hasOwnProp.call(to, key) && key !== except)
  51. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  52. }
  53. return to;
  54. };
  55. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  56. // If the importer is in node compatibility mode or this is not an ESM
  57. // file that has been converted to a CommonJS file using a Babel-
  58. // compatible transform (i.e. "__esModule" has not been set), then set
  59. // "default" to the CommonJS "module.exports" for node compatibility.
  60. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  61. mod
  62. ));
  63. var __async = (__this, __arguments, generator) => {
  64. return new Promise((resolve, reject) => {
  65. var fulfilled = (value) => {
  66. try {
  67. step(generator.next(value));
  68. } catch (e) {
  69. reject(e);
  70. }
  71. };
  72. var rejected = (value) => {
  73. try {
  74. step(generator.throw(value));
  75. } catch (e) {
  76. reject(e);
  77. }
  78. };
  79. var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
  80. step((generator = generator.apply(__this, __arguments)).next());
  81. });
  82. };
  83.  
  84. // ../../packages/chatkit/dist/chatgpt/index.js
  85. var require_chatgpt = __commonJS({
  86. "../../packages/chatkit/dist/chatgpt/index.js"(exports, module) {
  87. "use strict";
  88. var __defProp2 = Object.defineProperty;
  89. var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
  90. var __getOwnPropNames2 = Object.getOwnPropertyNames;
  91. var __hasOwnProp2 = Object.prototype.hasOwnProperty;
  92. var __export = (target, all) => {
  93. for (var name in all)
  94. __defProp2(target, name, { get: all[name], enumerable: true });
  95. };
  96. var __copyProps2 = (to, from, except, desc) => {
  97. if (from && typeof from === "object" || typeof from === "function") {
  98. for (let key of __getOwnPropNames2(from))
  99. if (!__hasOwnProp2.call(to, key) && key !== except)
  100. __defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
  101. }
  102. return to;
  103. };
  104. var __toCommonJS = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
  105. var chatgpt_exports = {};
  106. __export(chatgpt_exports, {
  107. getContinueGeneratingButton: () => getContinueGeneratingButton,
  108. getConversation: () => getConversation,
  109. getHistoryBlockTitle: () => getHistoryBlockTitle,
  110. getHistoryBlocks: () => getHistoryBlocks,
  111. getHistoryBlocksWithTitle: () => getHistoryBlocksWithTitle,
  112. getLastResponse: () => getLastResponse,
  113. getLastResponseElement: () => getLastResponseElement,
  114. getModelSelectButton: () => getModelSelectButton,
  115. getNav: () => getNav,
  116. getNewModelSelectButtons: () => getNewModelSelectButtons,
  117. getRegenerateButton: () => getRegenerateButton,
  118. getStopGeneratingButton: () => getStopGeneratingButton,
  119. getSubmitButton: () => getSubmitButton,
  120. getTextarea: () => getTextarea,
  121. getTextareaValue: () => getTextareaValue,
  122. hasNewModelSelectButtons: () => hasNewModelSelectButtons,
  123. isConversationStarted: () => isConversationStarted,
  124. isGenerating: () => isGenerating,
  125. isHorizontalConversation: () => isHorizontalConversation,
  126. onSend: () => onSend,
  127. regenerate: () => regenerate,
  128. send: () => send,
  129. setHorizontalConversation: () => setHorizontalConversation,
  130. setPromptListener: () => setPromptListener2,
  131. setPureConversation: () => setPureConversation,
  132. setTextarea: () => setTextarea,
  133. waitForIdle: () => waitForIdle
  134. });
  135. module.exports = __toCommonJS(chatgpt_exports);
  136. function getNav() {
  137. return document.querySelector("nav");
  138. }
  139. function getHistoryBlocks() {
  140. const nav = getNav();
  141. if (!nav)
  142. return [];
  143. const result = Array.from(nav.querySelectorAll("ol")).map((ol) => ol.parentElement);
  144. return result;
  145. }
  146. function getHistoryBlockTitle(historyBlock) {
  147. var _a;
  148. return ((_a = historyBlock.querySelector("h3")) == null ? void 0 : _a.textContent) || "";
  149. }
  150. function getHistoryBlocksWithTitle() {
  151. const historyBlocks = getHistoryBlocks();
  152. const result = historyBlocks.map((historyBlock) => ({
  153. block: historyBlock,
  154. title: getHistoryBlockTitle(historyBlock)
  155. }));
  156. return result;
  157. }
  158. function getTextarea() {
  159. const form = document.querySelector("form");
  160. if (!form)
  161. return;
  162. const textareas = form.querySelectorAll("textarea");
  163. const result = textareas[0];
  164. return result;
  165. }
  166. function getSubmitButton() {
  167. const textarea = getTextarea();
  168. if (!textarea)
  169. return;
  170. return textarea.nextElementSibling;
  171. }
  172. function getRegenerateButton() {
  173. const form = document.querySelector("form");
  174. if (!form)
  175. return;
  176. const buttons = form.querySelectorAll("button");
  177. const result = Array.from(buttons).find((button) => {
  178. var _a;
  179. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("regenerate");
  180. });
  181. return result;
  182. }
  183. function getContinueGeneratingButton() {
  184. const form = document.querySelector("form");
  185. if (!form)
  186. return;
  187. const buttons = form.querySelectorAll("button");
  188. const result = Array.from(buttons).find((button) => {
  189. var _a;
  190. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("continue generating");
  191. });
  192. return result;
  193. }
  194. function getStopGeneratingButton() {
  195. const form = document.querySelector("form");
  196. if (!form)
  197. return;
  198. const buttons = form.querySelectorAll("button");
  199. const result = Array.from(buttons).find((button) => {
  200. var _a;
  201. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("stop generating");
  202. });
  203. return result;
  204. }
  205. function getLastResponseElement() {
  206. const responseElements = document.querySelectorAll(".group.w-full");
  207. return responseElements[responseElements.length - 1];
  208. }
  209. function getLastResponse() {
  210. const lastResponseElement = getLastResponseElement();
  211. if (!lastResponseElement)
  212. return;
  213. const lastResponse = lastResponseElement.textContent;
  214. return lastResponse;
  215. }
  216. function getTextareaValue() {
  217. var _a;
  218. return ((_a = getTextarea()) == null ? void 0 : _a.value) || "";
  219. }
  220. function setTextarea(message) {
  221. const textarea = getTextarea();
  222. if (!textarea)
  223. return;
  224. textarea.value = message;
  225. textarea.dispatchEvent(new Event("input", { bubbles: true }));
  226. }
  227. function send(message) {
  228. return __async(this, null, function* () {
  229. setTextarea(message);
  230. const textarea = getTextarea();
  231. if (!textarea)
  232. return;
  233. while (textarea.value === message) {
  234. textarea.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
  235. yield new Promise((resolve) => setTimeout(resolve, 100));
  236. }
  237. });
  238. }
  239. function regenerate() {
  240. const regenerateButton = getRegenerateButton();
  241. if (!regenerateButton)
  242. return;
  243. regenerateButton.click();
  244. }
  245. function onSend(callback) {
  246. const textarea = getTextarea();
  247. if (!textarea)
  248. return;
  249. textarea.addEventListener("keydown", function(event) {
  250. if (event.key === "Enter" && !event.shiftKey) {
  251. callback();
  252. }
  253. });
  254. const sendButton = getSubmitButton();
  255. if (!sendButton)
  256. return;
  257. sendButton.addEventListener("mousedown", callback);
  258. }
  259. function isGenerating() {
  260. var _a, _b;
  261. return ((_b = (_a = getSubmitButton()) == null ? void 0 : _a.firstElementChild) == null ? void 0 : _b.childElementCount) === 3;
  262. }
  263. function waitForIdle() {
  264. return new Promise((resolve) => {
  265. const interval = setInterval(() => {
  266. if (!isGenerating()) {
  267. clearInterval(interval);
  268. resolve();
  269. }
  270. }, 1e3);
  271. });
  272. }
  273. function setPromptListener2(key = "prompt_texts") {
  274. let last_trigger_time = +/* @__PURE__ */ new Date();
  275. if (location.href.includes("chat.openai")) {
  276. GM_addValueChangeListener(key, (name, old_value, new_value) => __async(this, null, function* () {
  277. if (+/* @__PURE__ */ new Date() - last_trigger_time < 500) {
  278. return;
  279. }
  280. last_trigger_time = +/* @__PURE__ */ new Date();
  281. setTimeout(() => __async(this, null, function* () {
  282. const prompt_texts = new_value;
  283. if (prompt_texts.length > 0) {
  284. let firstTime = true;
  285. while (prompt_texts.length > 0) {
  286. if (!firstTime) {
  287. yield new Promise((resolve) => setTimeout(resolve, 2e3));
  288. }
  289. if (!firstTime && isGenerating()) {
  290. continue;
  291. }
  292. firstTime = false;
  293. const prompt_text = prompt_texts.shift() || "";
  294. yield send(prompt_text);
  295. }
  296. }
  297. }), 0);
  298. GM_setValue(key, []);
  299. }));
  300. }
  301. }
  302. function getConversation() {
  303. var _a, _b;
  304. return (_b = (_a = document.querySelector('div[class^="react-scroll-to-bottom"]')) == null ? void 0 : _a.firstChild) == null ? void 0 : _b.firstChild;
  305. }
  306. function getModelSelectButton() {
  307. const conversation = getConversation();
  308. if (!conversation)
  309. return;
  310. return Array.from(conversation.querySelectorAll("button")).find((button) => {
  311. var _a;
  312. return (_a = button.textContent) == null ? void 0 : _a.trim().toLowerCase().includes("model");
  313. });
  314. }
  315. function getNewModelSelectButtons() {
  316. return Array.from(document.querySelectorAll("[class^='group/button']"));
  317. }
  318. function hasNewModelSelectButtons() {
  319. return getNewModelSelectButtons().length > 0;
  320. }
  321. function isConversationStarted() {
  322. return !getModelSelectButton();
  323. }
  324. function setPureConversation() {
  325. const conversation = getConversation();
  326. if (!conversation)
  327. return;
  328. const firstChild = conversation.firstChild;
  329. if (!firstChild)
  330. return;
  331. const newDiv = document.createElement("div");
  332. conversation.insertBefore(newDiv, firstChild.nextSibling);
  333. }
  334. function isHorizontalConversation() {
  335. const conversation = getConversation();
  336. if (!conversation)
  337. return true;
  338. if (!isConversationStarted())
  339. return true;
  340. return conversation.classList.contains("grid");
  341. }
  342. function setHorizontalConversation() {
  343. if (isHorizontalConversation())
  344. return;
  345. setPureConversation();
  346. const conversation = getConversation();
  347. if (!conversation)
  348. return;
  349. conversation.classList.remove("flex", "flex-col", "items-center");
  350. conversation.classList.add("grid", "grid-cols-2", "place-items-center");
  351. }
  352. }
  353. });
  354.  
  355. // ../../packages/chatkit/chatgpt.js
  356. var require_chatgpt2 = __commonJS({
  357. "../../packages/chatkit/chatgpt.js"(exports, module) {
  358. module.exports = require_chatgpt();
  359. }
  360. });
  361.  
  362. // src/index.ts
  363. var import_chatgpt = __toESM(require_chatgpt2(), 1);
  364.  
  365. // src/createButton/index.ts
  366. function createButton(callback) {
  367. if (window.location.href.includes("chat.openai")) {
  368. return;
  369. }
  370. const hideRight = document.title.match(/[\u4e00-\u9fa5]/) ? "-130px" : "-120px";
  371. const button = document.createElement("button");
  372. button.innerHTML = "\u7F51\u9875\u603B\u7ED3";
  373. button.style.position = "fixed";
  374. button.style.width = "140px";
  375. button.style.top = "180px";
  376. button.style.right = hideRight;
  377. button.style.zIndex = "999999";
  378. button.style.backgroundColor = "#F8BBC6";
  379. button.style.color = "#fff";
  380. button.style.opacity = "0.8";
  381. button.style.border = "none";
  382. button.style.borderRadius = "4px";
  383. button.style.padding = "10px 16px";
  384. button.style.fontSize = "18px";
  385. button.style.cursor = "pointer";
  386. button.style.transition = "right 0.3s";
  387. document.body.appendChild(button);
  388. button.addEventListener("mouseenter", () => {
  389. button.style.right = "-10px";
  390. });
  391. button.addEventListener("mouseleave", () => {
  392. button.style.right = hideRight;
  393. });
  394. document.addEventListener("fullscreenchange", () => {
  395. if (document.fullscreenElement) {
  396. button.style.display = "none";
  397. } else {
  398. button.style.display = "block";
  399. }
  400. });
  401. button.addEventListener("click", callback);
  402. }
  403. var createButton_default = createButton;
  404.  
  405. // src/SimpleArticleSegmentation/index.ts
  406. var MIN_PARAGRAPH_LENGTH = 1600;
  407. var MAX_PARAGRAPH_LENGTH = 1800;
  408. var SimpleArticleSegmentation = class {
  409. constructor(text) {
  410. this.text = text;
  411. }
  412. segment() {
  413. const paragraphs = [];
  414. const sentences = this.text.split(new RegExp("(?<=[.!?])\\s+"));
  415. let paragraph = "";
  416. for (const sentence of sentences) {
  417. if (paragraph.length + sentence.length + 1 <= MAX_PARAGRAPH_LENGTH) {
  418. paragraph += (paragraph.length > 0 ? " " : "") + sentence;
  419. } else {
  420. if (paragraph.length >= MIN_PARAGRAPH_LENGTH) {
  421. paragraphs.push(paragraph);
  422. paragraph = sentence;
  423. } else {
  424. paragraph += " " + sentence;
  425. }
  426. }
  427. }
  428. if (paragraph.length >= MIN_PARAGRAPH_LENGTH) {
  429. paragraphs.push(paragraph);
  430. }
  431. return paragraphs;
  432. }
  433. };
  434. var SimpleArticleSegmentation_default = SimpleArticleSegmentation;
  435.  
  436. // src/getParagraphs/index.ts
  437. function getParagraphs() {
  438. try {
  439. let docClone = document.cloneNode(true);
  440. let article = new Readability(docClone).parse();
  441. if (article && article.textContent) {
  442. const segmenter = new SimpleArticleSegmentation_default(article.textContent);
  443. const paragraphs = segmenter.segment();
  444. for (let i = 0; i < paragraphs.length; i++) {
  445. paragraphs[i] = paragraphs[i].trim();
  446. }
  447. return paragraphs;
  448. } else {
  449. console.warn("Readability.js could not extract any text content from this page.");
  450. return [];
  451. }
  452. } catch (error) {
  453. console.error("An error occurred while using Readability.js:", error);
  454. return [];
  455. }
  456. }
  457. var getParagraphs_default = getParagraphs;
  458.  
  459. // src/index.ts
  460. function initialize() {
  461. return __async(this, null, function* () {
  462. yield new Promise((resolve) => window.addEventListener("load", resolve));
  463. yield new Promise((resolve) => setTimeout(resolve, 1e3));
  464. });
  465. }
  466. function main() {
  467. return __async(this, null, function* () {
  468. yield initialize();
  469. const key = "prompt_texts";
  470. (0, import_chatgpt.setPromptListener)(key);
  471. const summaryWeb = () => __async(this, null, function* () {
  472. const paragraphs = getParagraphs_default();
  473. console.log(paragraphs);
  474. const prompt_texts = paragraphs.map((paragraph) => {
  475. return `"""
  476. ${paragraph}
  477. """
  478. Summarize this paragraph into a bulleted list of the most important information, prefix with emoji. Use Markdown syntax to optimize the display format:`;
  479. });
  480. console.log(prompt_texts);
  481. GM_setValue(key, prompt_texts);
  482. });
  483. createButton_default(summaryWeb);
  484. });
  485. }
  486. (function() {
  487. main();
  488. })();
  489. })();