GPT-HISTORY-PRINTER

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         GPT-HISTORY-PRINTER
// @namespace    http://tampermonkey.net/
// @version      0.0.2
// @description  导出GPT聊天记录为PDF文件,按下Ctrl+P即可导出。
// @author       You
// @match        https://chatgpt.com/c/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chatgpt.com
// @grant        none
// @run-at       document-start
// @license      GPL-3.0-only
// ==/UserScript==

(function() {
    'use strict';

    /**
     * @param {string} selectors 
     * @returns {HTMLElement[]}
     */
    function my$(selectors) {
        return [...document.querySelectorAll(selectors)];
    }

    function clean() {
        const to_be_removed = '[class*="juice:p-3"], button.cursor-pointer:nth-child(7)';
        my$(to_be_removed).forEach(el => el.remove());

        my$("div.mx-auto").filter(
            el => el.textContent.search("到目前为止,此对话有帮助吗?") >= 0
        ).forEach(el => el.remove());
    }

    function black_font_color() {
        const style = "<style>*:not(pre, pre *) { color: black !important; }</style>";
        document.head.insertAdjacentHTML("beforeend", style); 
    }

    /**
     * @param {string} dialog 
     */
    function set_dialog_the_only_one(dialog) {
        document.body.outerHTML = "<body></body>";
        document.body.innerHTML = dialog;
    }

    /**
     * 当按下Ctrl+P时,重置页面,打印PDF
     * @param {KeyboardEvent} event 
     */
    function on_ctrl_p(event) {
        if (!(event.ctrlKey && event.code === "KeyP")) {
            return;
        }

        event.preventDefault();
        event.stopPropagation();

        // 获取聊天DIV
        const div = my$(".pb-9")[0];
        if (!div) {
            alert("找不到聊天记录!");
            return;
        }
        const diaglog = div.outerHTML;

        // 重置页面
        clean();
        set_dialog_the_only_one(diaglog);
        black_font_color();

        // 打印PDF
        setTimeout(print, 500);
    }

    function main() {
        addEventListener("keydown", on_ctrl_p, true);
    }
    
    document.addEventListener("DOMContentLoaded", () => setTimeout(main, 2000));
})();