-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.user.js
125 lines (108 loc) · 5.15 KB
/
script.user.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
// ==UserScript==
// @name The return of the Clear Chats button
// @icon https://raw.githubusercontent.com/madkarmaa/chatgpt-clear-chats-btn/main/images/icon.png
// @namespace aB3Rt8yZuX6sDcV9nMjKq1wSxLpOnH4bUiGtRfEyWqAzXsCvBnMjKlQwPxErT7y
// @source https://github.com/madkarmaa/chatgpt-clear-chats-btn
// @supportURL https://github.com/madkarmaa/chatgpt-clear-chats-btn
// @updateURL https://github.com/madkarmaa/chatgpt-clear-chats-btn/raw/main/script.user.js
// @downloadURL https://github.com/madkarmaa/chatgpt-clear-chats-btn/raw/main/script.user.js
// @version 1.3.0
// @description OpenAI removed the shortcut button to clear all chats. I didn't like this change
// @author mk_
// @match *://chat.openai.com/*
// @require https://cdn.jsdelivr.net/gh/kudoai/chatgpt.js@06d15c2e7421874a6c8743e51116872417af5e85/dist/chatgpt-2.5.2.min.js
// @grant GM_addStyle
// @run-at document-end
// ==/UserScript==
(async () => {
'use strict';
function createEl(tag, attrs) {
const el = document.createElement(tag);
Object.entries(attrs).forEach(([key, value]) => (el[key] = value));
return el;
}
GM_addStyle(`
.is-awaiting-confirm {
display: flex;
align-items: center;
justify-content: center;
background-color: #dc143c !important;
border: 1px solid hsla(0, 0%, 100%, .2);
border-radius: .375rem;
margin: 0 5px;
}
.is-awaiting-confirm:hover {
background-color: #B91C1C !important;
}
#trash-bin-svg {
margin-left: 2.4px;
margin-right: 1.35px;
}
`);
await chatgpt.isLoaded();
const menuBtn = document.querySelector('nav button[id*="headless"]');
const clearButton = createEl('a', {
id: 'clear-button',
textContent: 'Clear chats',
});
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', '1em');
svg.setAttribute('height', '1em');
svg.setAttribute('viewBox', '0 0 408.483 408.483');
svg.setAttribute('fill', 'currentColor');
svg.setAttribute('id', 'trash-bin-svg');
const g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
const innerG = document.createElementNS('http://www.w3.org/2000/svg', 'g');
const path1 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
path1.setAttribute(
'd',
'M87.748,388.784c0.461,11.01,9.521,19.699,20.539,19.699h191.911c11.018,0,20.078-8.689,20.539-19.699l13.705-289.316H74.043L87.748,388.784z M247.655,171.329c0-4.61,3.738-8.349,8.35-8.349h13.355c4.609,0,8.35,3.738,8.35,8.349v165.293c0,4.611-3.738,8.349-8.35,8.349h-13.355c-4.61,0-8.35-3.736-8.35-8.349V171.329z M189.216,171.329c0-4.61,3.738-8.349,8.349-8.349h13.355c4.609,0,8.349,3.738,8.349,8.349v165.293c0,4.611-3.737,8.349-8.349,8.349h-13.355c-4.61,0-8.349-3.736-8.349-8.349V171.329L189.216,171.329z M130.775,171.329c0-4.61,3.738-8.349,8.349-8.349h13.356c4.61,0,8.349,3.738,8.349,8.349v165.293c0,4.611-3.738,8.349-8.349,8.349h-13.356c-4.61,0-8.349-3.736-8.349-8.349V171.329z'
);
const path2 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
path2.setAttribute(
'd',
'M343.567,21.043h-88.535V4.305c0-2.377-1.927-4.305-4.305-4.305h-92.971c-2.377,0-4.304,1.928-4.304,4.305v16.737H64.916c-7.125,0-12.9,5.776-12.9,12.901V74.47h304.451V33.944C356.467,26.819,350.692,21.043,343.567,21.043z'
);
innerG.appendChild(path1);
innerG.appendChild(path2);
g.appendChild(innerG);
svg.appendChild(g);
clearButton.insertBefore(svg, clearButton.firstChild);
// Clear chats on click
clearButton.addEventListener('click', () => {
if (clearButton.textContent === 'Clear chats') {
clearButton.textContent = 'Confirm';
clearButton.classList.add('is-awaiting-confirm');
} else if (clearButton.textContent === 'Confirm') {
chatgpt.clearChats();
menuBtn.click();
}
});
function addElementsToDOM() {
const optionButtons = document.querySelectorAll('a[role="menuitem"]');
let separator;
for (let navLink of optionButtons)
if (navLink.textContent.match(/.*Settings/)) {
separator = navLink.nextSibling.cloneNode(true);
clearButton.setAttribute('class', navLink.classList);
break;
}
const headlessNav = optionButtons[0].parentNode;
if (!headlessNav.contains(clearButton) && separator)
try {
headlessNav.insertBefore(clearButton, headlessNav.querySelector('a'));
headlessNav.insertBefore(separator, headlessNav.querySelector(`a:not(#${clearButton.id})`));
} catch (error) {
console.error(error);
}
}
// Add the button to the headless navigator when opened
menuBtn.addEventListener('click', () => {
if (clearButton.textContent === 'Confirm') {
clearButton.textContent = 'Clear chats';
clearButton.insertBefore(svg, clearButton.firstChild);
clearButton.classList.remove('is-awaiting-confirm');
}
setTimeout(addElementsToDOM, 25);
});
})();