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-21 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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();