ChatGPT Input Protector

Prevent input words loss on ChatGPT page loading

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==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);
    });

})();