Greasy Fork

Greasy Fork is available in English.

GPT-HISTORY-PRINTER

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

当前为 2024-05-20 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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));
})();