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.

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

  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.
  4. //
  5. // @namespace http://tampermonkey.net/
  6. // @version 2023.10.05
  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. void function userscript() {
  17. "use strict";
  18.  
  19. const origAddEventListenerFn = EventTarget.prototype.addEventListener;
  20.  
  21. EventTarget.prototype.addEventListener = function(type, listener, optionsOrUseCapture) {
  22. const lowercaseType = type.toLowerCase();
  23. let mayPass = true;
  24.  
  25. // (It doesn't seem to be necessary to prevent these events: "focusin", "pageshow", "resume".)
  26. if ([ "focus", "visibilitychange" ].includes(lowercaseType)) {
  27. let callStack = new Error().stack + "\n";
  28. callStack = callStack.substring(callStack.indexOf("\n", callStack.indexOf("-extension://")) + 1); // Remove first entry referring to these very lines of code.
  29.  
  30. // (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.)
  31. const calledByBrowserExtension = callStack.includes("-extension://"); // Includes userscripts because of their manager.
  32.  
  33. if (! calledByBrowserExtension && lowercaseType === "visibilitychange") {
  34. origAddEventListenerFn.call(
  35. this,
  36. type,
  37. function(event) {
  38. if (document.visibilityState !== "visible") { // Filter out this event subtype.
  39. listener.call(this, event);
  40. }
  41. },
  42. optionsOrUseCapture
  43. );
  44. }
  45.  
  46. mayPass = calledByBrowserExtension; // Filter out above mentioned event types in official code.
  47. }
  48.  
  49. if (mayPass) {
  50. origAddEventListenerFn.apply(this, arguments);
  51. }
  52. };
  53. }.call();