-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt-temp-chat-by-default.user.js
More file actions
110 lines (98 loc) · 3.58 KB
/
chatgpt-temp-chat-by-default.user.js
File metadata and controls
110 lines (98 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// ==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/
// @downloadURL https://raw.githubusercontent.com/nisc/chatgpt-userscripts/main/chatgpt-temp-chat-by-default.user.js
// @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();
});
})();