🏠 Home 

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. (function() {
  14. 'use strict';
  15. /**
  16. * @param {string} selectors
  17. * @returns {HTMLElement[]}
  18. */
  19. function my$(selectors) {
  20. return [...document.querySelectorAll(selectors)];
  21. }
  22. function clean() {
  23. const to_be_removed = '[class*="juice:p-3"], button.cursor-pointer:nth-child(7)';
  24. my$(to_be_removed).forEach(el => el.remove());
  25. my$("div.mx-auto").filter(
  26. el => el.textContent.search("到目前为止,此对话有帮助吗?") >= 0
  27. ).forEach(el => el.remove());
  28. }
  29. function black_font_color() {
  30. const style = "<style>*:not(pre, pre *) { color: black !important; }</style>";
  31. document.head.insertAdjacentHTML("beforeend", style);
  32. }
  33. /**
  34. * @param {string} dialog
  35. */
  36. function set_dialog_the_only_one(dialog) {
  37. document.body.outerHTML = "<body></body>";
  38. document.body.innerHTML = dialog;
  39. }
  40. /**
  41. * 当按下Ctrl+P时,重置页面,打印PDF
  42. * @param {KeyboardEvent} event
  43. */
  44. function on_ctrl_p(event) {
  45. if (!(event.ctrlKey && event.code === "KeyP")) {
  46. return;
  47. }
  48. event.preventDefault();
  49. event.stopPropagation();
  50. // 获取聊天DIV
  51. const div = my$(".pb-9")[0];
  52. if (!div) {
  53. alert("找不到聊天记录!");
  54. return;
  55. }
  56. const diaglog = div.outerHTML;
  57. // 重置页面
  58. clean();
  59. set_dialog_the_only_one(diaglog);
  60. black_font_color();
  61. // 打印PDF
  62. setTimeout(print, 500);
  63. }
  64. function main() {
  65. addEventListener("keydown", on_ctrl_p, true);
  66. }
  67. document.addEventListener("DOMContentLoaded", () => setTimeout(main, 2000));
  68. })();