ChatGPT Temp Chat By Default

Automatically enables temporary mode on ChatGPT unless manually disabled by the user

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ChatGPT Temp Chat By Default
// @namespace    nisc
// @version      2025.06.08-B
// @description  Automatically enables temporary mode on ChatGPT unless manually disabled by the user
// @homepageURL  https://github.com/nisc/chatgpt-userscripts/
// @author       nisc
// @match        https://chatgpt.com/*
// @icon         https://chatgpt.com/favicon.ico
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  // Track if user has manually disabled temporary chat mode
  let userDisabledTempChat = false;

  /**
   * Find the temporary chat toggle button in the UI
   * @returns {HTMLButtonElement|null} The temporary chat button or null if not found
   */
  function getTempChatButton() {
    return document.querySelector('button[aria-label*="temporary chat"]');
  }

  /**
   * Enable temporary chat mode if not manually disabled by user
   * Makes multiple attempts to find and click the button
   */
  function activateTemporaryMode() {
    if (userDisabledTempChat) return;

    let attempts = 0;
    const maxAttempts = 5;
    const interval = 500;

    const tryFindButton = setInterval(() => {
      const tempChatButton = getTempChatButton();
      if (tempChatButton) {
        // Add listener to detect when user manually disables temp chat
        if (!tempChatButton._hasListener) {
          tempChatButton.addEventListener('click', function () {
            userDisabledTempChat = this.getAttribute('aria-label') === 'Turn off temporary chat';
          });
          tempChatButton._hasListener = true;
        }

        // Enable temporary chat if it's currently off
        if (tempChatButton.getAttribute('aria-label') === 'Turn on temporary chat') {
          tempChatButton.click();
        }
        clearInterval(tryFindButton);
      } else if (attempts++ >= maxAttempts) {
        clearInterval(tryFindButton);
      }
    }, interval);
  }

  /**
   * Set up observer to watch for DOM changes that indicate a new chat
   * This ensures we catch new chats created via the UI
   */
  function setupMutationObserver() {
    const targetNode = document.querySelector('#conversation-header-actions') || document.body;
    const observer = new MutationObserver(() => {
      const tempChatButton = getTempChatButton();
      if (tempChatButton && tempChatButton.getAttribute('aria-label') === 'Turn on temporary chat') {
        userDisabledTempChat = false; // Reset flag for new chat
        activateTemporaryMode();
      }
    });
    observer.observe(targetNode, { childList: true, subtree: true });
  }

  /**
   * Handle URL/history state changes to detect new chats
   * This ensures we catch new chats created via URL navigation
   */
  function handleStateChange() {
    if (window.location.pathname.startsWith('/c/')) {
      userDisabledTempChat = false;
      activateTemporaryMode();
    }
  }

  // Wrap history methods to detect URL-based chat changes
  if (!window._historyWrapped) {
    const originalPushState = history.pushState;
    history.pushState = function () {
      originalPushState.apply(this, arguments);
      handleStateChange();
    };

    const originalReplaceState = history.replaceState;
    history.replaceState = function () {
      originalReplaceState.apply(this, arguments);
      handleStateChange();
    };
    window._historyWrapped = true;
  }

  // Initialize when page loads
  window.addEventListener('load', function () {
    setupMutationObserver();
    activateTemporaryMode();
  });
})();