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.
当前为
// ==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.
//
// @namespace http://tampermonkey.net/
// @version 2023.10.05
//
// @author Henrik Hank
// @license MIT (https://opensource.org/license/mit/)
//
// @match *://chat.openai.com/*
// @run-at document-start
// @grant none
// ==/UserScript==
void function userscript() {
"use strict";
const origAddEventListenerFn = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function(type, listener, optionsOrUseCapture) {
const lowercaseType = type.toLowerCase();
let mayPass = true;
// (It doesn't seem to be necessary to prevent these events: "focusin", "pageshow", "resume".)
if ([ "focus", "visibilitychange" ].includes(lowercaseType)) {
let callStack = new Error().stack + "\n";
callStack = callStack.substring(callStack.indexOf("\n", callStack.indexOf("-extension://")) + 1); // Remove first entry referring to these very lines of code.
// (When our substitute listeners are in the call stack of official code adding listeners, then these `addEventListener()` calls will incorrectly not be blocked. `GM_info()` might be able to help.)
const calledByBrowserExtension = callStack.includes("-extension://"); // Includes userscripts because of their manager.
if (! calledByBrowserExtension && lowercaseType === "visibilitychange") {
origAddEventListenerFn.call(
this,
type,
function(event) {
if (document.visibilityState !== "visible") { // Filter out this event subtype.
listener.call(this, event);
}
},
optionsOrUseCapture
);
}
mayPass = calledByBrowserExtension; // Filter out above mentioned event types in official code.
}
if (mayPass) {
origAddEventListenerFn.apply(this, arguments);
}
};
}.call();