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 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Prevent ChatGPT Conversation Reset
  3. // @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.
  4. //
  5. // @namespace http://tampermonkey.net/
  6. // @version 2023.10.06
  7. //
  8. // @author Henrik Hank
  9. // @license MIT (https://opensource.org/license/mit/)
  10. //
  11. // @match *://chat.openai.com/*
  12. // @run-at document-start
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. /* jshint esversion: 11 */
  17.  
  18. void function userscript() {
  19. "use strict";
  20.  
  21. const origAddEventListenerFn = EventTarget.prototype.addEventListener;
  22.  
  23. EventTarget.prototype.addEventListener = function(type, listener, optionsOrUseCapture) {
  24. let mayPass = true;
  25.  
  26. if (
  27. (type === "focus" && this === window) ||
  28. type === "visibilitychange" // Always on `document`.
  29. ) {
  30. // Identify function caller via call stack.
  31. const callStack = new Error().stack + "\n";
  32. const secondStackEntry = callStack.match(/-extension:[/][/].*\n(.+)/)?.[1]; // First entry is this function, attributed to userscript manager.
  33. const calledByBrowserExtension = secondStackEntry?.includes("-extension://") ?? false; // Also accounts for userscripts (managed by extension).
  34.  
  35. // Filter out event types in official code.
  36. mayPass = calledByBrowserExtension;
  37.  
  38. // Filter `visibilitychange` event partially.
  39. if (! calledByBrowserExtension && type === "visibilitychange") {
  40. origAddEventListenerFn.call(
  41. this,
  42. type,
  43. function(event) {
  44. // Filter out only this event subtype.
  45. if (document.visibilityState !== "visible") {
  46. listener.call(this, event);
  47. }
  48. },
  49. optionsOrUseCapture
  50. );
  51. }
  52. }
  53.  
  54. if (mayPass) {
  55. origAddEventListenerFn.apply(this, arguments);
  56. }
  57. };
  58. }.call();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址