Prevent ChatGPT Conversation Reset

Prevents certain browser events from reaching official ChatGPT code. Otherwise, with chat history turned off, the conversation may be reset after 10 min of inactivity or even spontaneously while being active. The script is designed to only affect official ChatGPT code and to avoid interfering with other userscripts or browser extensions.

当前为 2023-10-06 提交的版本,查看 最新版本

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

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

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

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

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

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Prevent ChatGPT Conversation Reset
// @description  Prevents certain browser events from reaching official ChatGPT code. Otherwise, with chat history turned off, the conversation may be reset after 10 min of inactivity or even spontaneously while being active. The script is designed to only affect official ChatGPT code and to avoid interfering with other userscripts or browser extensions.
//
// @namespace    http://tampermonkey.net/
// @version      2023.10.06
//
// @author       Henrik Hank
// @license      MIT (https://opensource.org/license/mit/)
//
// @match        *://chat.openai.com/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

/* jshint esversion: 11 */

void function userscript() {
    "use strict";

    const origAddEventListenerFn = EventTarget.prototype.addEventListener;

    EventTarget.prototype.addEventListener = function(type, listener, optionsOrUseCapture) {
        let mayPass = true;

        if (
            (type === "focus" && this === window) ||
            type === "visibilitychange"  // Always on `document`.
        ) {
            // Identify function caller via call stack.
            const callStack = new Error().stack + "\n";
            const secondStackEntry = callStack.match(/-extension:[/][/].*\n(.+)/)?.[1];  // First entry is this function, attributed to userscript manager.
            const calledByBrowserExtension = secondStackEntry?.includes("-extension://") ?? false;  // Also accounts for userscripts (managed by extension).

            // Filter out event types in official code.
            mayPass = calledByBrowserExtension;

            // Filter `visibilitychange` event partially.
            if (! calledByBrowserExtension && type === "visibilitychange") {
                origAddEventListenerFn.call(
                    this,
                    type,
                    function(event) {
                        // Filter out only this event subtype.
                        if (document.visibilityState !== "visible") {
                            listener.call(this, event);
                        }
                    },
                    optionsOrUseCapture
                );
            }
        }

        if (mayPass) {
            origAddEventListenerFn.apply(this, arguments);
        }
    };
}.call();