GPT-HISTORY-PRINTER

导出GPT聊天记录为PDF文件,按下Ctrl+P即可导出。

  1. // ==UserScript==
  2. // @name GPT-HISTORY-PRINTER
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.2
  5. // @description 导出GPT聊天记录为PDF文件,按下Ctrl+P即可导出。
  6. // @author You
  7. // @match https://chatgpt.com/c/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=chatgpt.com
  9. // @grant none
  10. // @run-at document-start
  11. // @license GPL-3.0-only
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. /**
  18. * @param {string} selectors
  19. * @returns {HTMLElement[]}
  20. */
  21. function my$(selectors) {
  22. return [...document.querySelectorAll(selectors)];
  23. }
  24.  
  25. function clean() {
  26. const to_be_removed = '[class*="juice:p-3"], button.cursor-pointer:nth-child(7)';
  27. my$(to_be_removed).forEach(el => el.remove());
  28.  
  29. my$("div.mx-auto").filter(
  30. el => el.textContent.search("到目前为止,此对话有帮助吗?") >= 0
  31. ).forEach(el => el.remove());
  32. }
  33.  
  34. function black_font_color() {
  35. const style = "<style>*:not(pre, pre *) { color: black !important; }</style>";
  36. document.head.insertAdjacentHTML("beforeend", style);
  37. }
  38.  
  39. /**
  40. * @param {string} dialog
  41. */
  42. function set_dialog_the_only_one(dialog) {
  43. document.body.outerHTML = "<body></body>";
  44. document.body.innerHTML = dialog;
  45. }
  46.  
  47. /**
  48. * 当按下Ctrl+P时,重置页面,打印PDF
  49. * @param {KeyboardEvent} event
  50. */
  51. function on_ctrl_p(event) {
  52. if (!(event.ctrlKey && event.code === "KeyP")) {
  53. return;
  54. }
  55.  
  56. event.preventDefault();
  57. event.stopPropagation();
  58.  
  59. // 获取聊天DIV
  60. const div = my$(".pb-9")[0];
  61. if (!div) {
  62. alert("找不到聊天记录!");
  63. return;
  64. }
  65. const diaglog = div.outerHTML;
  66.  
  67. // 重置页面
  68. clean();
  69. set_dialog_the_only_one(diaglog);
  70. black_font_color();
  71.  
  72. // 打印PDF
  73. setTimeout(print, 500);
  74. }
  75.  
  76. function main() {
  77. addEventListener("keydown", on_ctrl_p, true);
  78. }
  79. document.addEventListener("DOMContentLoaded", () => setTimeout(main, 2000));
  80. })();

QingJ © 2025

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