-
Notifications
You must be signed in to change notification settings - Fork 0
/
tampermonkey.js
316 lines (284 loc) · 10.9 KB
/
tampermonkey.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// ==UserScript==
// @name Typing Mind Extension Script
// @namespace http://your-namespace.com/
// @version 1.5
// @description Enhance Typing Mind with various shortcuts and functionalities.
// @author
// @match *://www.typingmind.com/*
// @grant none
// @downloadURL https://update.greasyfork.org/scripts/your-script.user.js
// @updateURL https://update.greasyfork.org/scripts/your-script.meta.js
// ==/UserScript==
/*
* Typing Mind Extension Script - Version 1.5
* Date Updated: Updated on 2024-10-06
*
* Updates in this version:
* - Adjusted selectors for auto-play toggle switch to match the given button elements.
* - Ensured correct targeting of "Auto play assistant messages" switch.
* - Reintroduced missing functions for voice input, playing latest message, managing plugins, stopping, and editing messages.
*
* Shortcuts include:
* - Cmd+K: Reset character or start a new chat
* - Cmd+1: Toggle voice input
* - Cmd+, : Open Preferences
* - Cmd+Shift+R: Regenerate response
* - Cmd+L: Play latest message
* - Cmd+J: Open Models
* - Cmd+O: Open Manage Plugins
* - Cmd+3: Edit second-to-newest message
* - Cmd+U: Toggle auto-play setting in TTS and click Done
* - F2: Stop button
*/
// Function to wait for an element to appear
function waitForElement(selector, timeout = 3000) {
return new Promise((resolve, reject) => {
const element = document.querySelector(selector);
if (element) {
resolve(element);
}
const observer = new MutationObserver((mutations, me) => {
const element = document.querySelector(selector);
if (element) {
resolve(element);
me.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
setTimeout(() => {
observer.disconnect();
reject(new Error(`Element not found within timeout: ${selector}`));
}, timeout);
});
}
// Function to toggle auto-play setting without hovering
async function toggleAutoPlaySetting() {
try {
// Directly select the Settings button
const settingsButton = document.querySelector('button.group-hover\\:inline-block.sm\\:hidden.font-semibold.text-gray-500.hover\\:underline');
if (settingsButton) {
settingsButton.click();
console.log('Clicked Settings button');
// Wait for the modal to appear
const modal = await waitForElement('[data-element-id="pop-up-modal"]');
console.log('Modal appeared:', modal);
// Locate the "Auto play assistant messages" switch
const sections = Array.from(document.querySelectorAll('button[data-element-id="plugins-switch-disabled"], button[data-element-id="plugins-switch-enabled"]')).filter(button => {
return button.nextElementSibling && button.nextElementSibling.textContent.includes('Auto play assistant messages');
});
if (sections.length > 0) {
const toggleButton = sections[0];
console.log('Auto play switch found:', toggleButton);
toggleButton.click();
// Click the "Done" button
const doneButton = await waitForElement('button[type="submit"].inline-flex.items-center.px-4.py-2.border.border-transparent.text-base.font-medium.rounded-md.shadow-sm.text-white.bg-blue-600.hover\\:bg-blue-700.focus\\:outline-none.focus\\:ring-2.focus\\:ring-offset-2.focus\\:ring-blue-500.disabled\\:bg-gray-400.gap-2');
console.log('Done button found:', doneButton);
doneButton.click();
} else {
console.log('Auto play switch not found');
}
} else {
console.log('Settings button not found');
}
} catch (error) {
console.error('Error in toggling autoplay setting:', error);
}
}
// Function to check and click Reset Character or New Chat for Cmd+K
function handleCmdK() {
const resetButton = document.querySelector('button[data-element-id="reset-character-button"]');
if (resetButton) {
resetButton.click();
console.log('Clicked reset character button');
} else {
const newChatButton = document.querySelector('button[data-element-id="new-chat-button-in-side-bar"]');
if (newChatButton) {
newChatButton.click();
console.log('Clicked new chat button');
} else {
console.log('New chat button not found');
}
}
}
// Attach event listener for Cmd+K (or Ctrl+K on Windows/Linux)
document.addEventListener('keydown', function(event) {
if (event.metaKey && event.key === 'k') {
event.preventDefault();
handleCmdK();
}
});
// Function to toggle voice input
function toggleVoiceInput() {
const finishButton = Array.from(document.querySelectorAll('button'))
.find(button => button.textContent.includes('Finish'));
if (finishButton) {
finishButton.click();
} else {
clickElementBySelector('button[data-element-id="voice-input-button"]');
}
}
// Function to click latest play button
function clickLatestPlayButton() {
const playButtons = document.querySelectorAll('button[data-element-id="in-message-play-button"]');
if (playButtons.length > 0) {
playButtons[playButtons.length - 1].click();
console.log("Clicked the latest play button");
} else {
console.log("No play buttons found");
}
}
// Function to click manage plugins button
async function clickManagePluginsButton() {
console.log('Cmd+O pressed, attempting to open Manage Plugins');
const pluginsButton = document.querySelector('button svg.w-6.h-6.text-blue-500').closest('button');
if (pluginsButton) {
pluginsButton.click();
console.log('Clicked plugins button');
await new Promise(resolve => setTimeout(resolve, 100));
const menuItems = document.querySelectorAll('div[role="menuitem"]');
const managePluginsItem = Array.from(menuItems).find(item => {
const truncateDiv = item.querySelector('div.truncate');
return truncateDiv && truncateDiv.textContent.trim() === 'Manage Plugins';
});
if (managePluginsItem) {
managePluginsItem.click();
console.log('Clicked Manage Plugins option');
} else {
console.log('Manage Plugins option not found in menu');
}
} else {
console.log('Plugins button not found');
}
}
// Function to click stop button
function clickStopButton() {
const stopButton = Array.from(document.querySelectorAll('button')).find(button => button.textContent.trim() === "Stop");
if (stopButton) {
stopButton.click();
} else {
console.log("Stop button not found");
}
}
// Function to click second-to-newest edit message button
function clickEditMessageButton() {
const editButton = document.querySelector('button[data-element-id="edit-message-button"]');
if (editButton) {
editButton.click();
console.log('Clicked edit message button');
} else {
console.log('Edit message button not found');
}
}
// Additional supporting functions
function clickElementBySelector(selector) {
const element = document.querySelector(selector);
if (element) {
element.click();
return true;
} else {
console.log(`Element with selector ${selector} not found`);
return false;
}
}
// Attach additional keyboard shortcuts
document.addEventListener('keydown', function(event) {
if (event.metaKey) {
switch (event.key) {
case '1':
event.preventDefault();
toggleVoiceInput();
break;
case ',':
event.preventDefault();
clickSettingsAndPreferences('button[data-element-id="settings-button"]', "Preferences");
break;
case 'R':
if (event.shiftKey) {
event.preventDefault();
clickElementBySelector('button[data-element-id="regenerate-button"]');
}
break;
case 'l':
event.preventDefault();
clickLatestPlayButton();
break;
case 'j':
event.preventDefault();
clickSettingsAndPreferences('button[data-element-id="settings-button"]', "Models");
break;
case 'o':
event.preventDefault();
clickManagePluginsButton();
break;
case '3':
event.preventDefault();
clickEditMessageButton();
break;
case 'u':
event.preventDefault();
toggleAutoPlaySetting();
break;
default:
break;
}
}
if (event.key === 'F2') {
event.preventDefault();
clickStopButton();
}
});
// Set text area rows
function setTextareaRows() {
const textareas = [
document.querySelector('[data-element-id="ai-characters-system-instruction-input"]'),
];
textareas.forEach(textarea => {
if (textarea) {
textarea.setAttribute('rows', '30');
}
});
}
setTextareaRows();
const textareaObserver = new MutationObserver((mutations) => {
for (let mutation of mutations) {
if (mutation.type === 'childList') {
setTextareaRows();
}
}
});
textareaObserver.observe(document.body, {
childList: true,
subtree: true
});
// Supporting function to click settings and preferences
function clickSettingsAndPreferences(settingsButtonSelector, preferencesText) {
const settingsButton = document.querySelector(settingsButtonSelector);
if (settingsButton) {
settingsButton.click();
const observer = new MutationObserver((mutations, obs) => {
const preferencesOption = Array.from(document.querySelectorAll('button, div[role="menuitem"]'))
.find(el => el.textContent.trim() === preferencesText);
if (preferencesOption) {
preferencesOption.click();
obs.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
setTimeout(() => observer.disconnect(), 1000);
}
}
// Adjust model menu height
const menuObserver = new MutationObserver(() => {
const modelMenu = document.querySelector('div[role="menu"] .py-2.max-h-\\[300px\\].overflow-auto');
if (modelMenu) {
modelMenu.style.maxHeight = '500px';
}
});
menuObserver.observe(document.body, { childList: true, subtree: true });
console.log('Full enhanced script loaded with all functionalities, including Manage Plugins (Cmd+O), Edit Message (Cmd+3), model menu height adjustment, and various keyboard shortcuts.');