ChatGPT Input Protector

Prevent input words loss on ChatGPT page loading

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         ChatGPT Input Protector
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Prevent input words loss on ChatGPT page loading
// @match        https://chat.openai.com/*
// @match        https://chatgpt.com/* 
// @license MIT 
// ==/UserScript==

(function () {
    'use strict';

    const STORAGE_KEY = "chatgpt_input_cache";

    function getInputBox() {
        return document.querySelector('[contenteditable="true"]');
    }

    // 保存输入
    function saveInput(text) {
        localStorage.setItem(STORAGE_KEY, text);
    }

    // 读取输入
    function loadInput() {
        return localStorage.getItem(STORAGE_KEY) || "";
    }

    // 恢复输入
    function restoreInput(el) {
        const saved = loadInput();
        if (saved && el.innerText.trim() === "") {
            el.innerText = saved;

            // 触发 input 事件(关键!让 React 知道内容变了)
            el.dispatchEvent(new Event('input', { bubbles: true }));
        }
    }

    function init() {
        let input = getInputBox();
        if (!input) return;

        // 初次恢复
        restoreInput(input);

        // 监听输入变化
        input.addEventListener("input", () => {
            saveInput(input.innerText);
        });

        // 监听 DOM 变化(防止 React 重建)
        const observer = new MutationObserver(() => {
            let newInput = getInputBox();
            if (newInput && newInput !== input) {
                input = newInput;
                restoreInput(input);

                input.addEventListener("input", () => {
                    saveInput(input.innerText);
                });
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // 等页面加载
    window.addEventListener("load", () => {
        setTimeout(init, 500);
    });

})();