-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentScript.js
280 lines (211 loc) · 7.7 KB
/
contentScript.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
/* global DirectoryItemManager, GTools, ContextMenu, displayDirectoryItems, BackendData, Constants */
/* global addDragHandlers */
let directoryItems = null;
let backendData = null;
(
()=>
{
init();
chrome.runtime.onMessage.addListener(async (obj, sender, sendResponse) =>
{
// load the root folder
if (directoryItems == null)
{
directoryItems = new DirectoryItemManager();
await directoryItems.loadDirectoryItems();
}
// deconstruct parameters
const { type,url } = obj;
console.log ("Current chat is ",url);
switch (type)
{
case "at_chat_openai": onNewChatGPTWindow(); break;
case "authReceived": onAuthReceived(); break;
case "command": onCommandReceived (obj.command); break;
}
});
function onCommandReceived (cmd)
{
switch (cmd)
{
case "toogle_root_collapse": return rootCollapse();
}
function rootCollapse()
{
let root = directoryItems.getRoot();
let rootElement = document.getElementById ("folder_"+root.folderId);
let innerOL = rootElement.parentNode.querySelector ("ol");
GTools.toggleCollapse (innerOL);
}
}
async function init ()
{
backendData = new BackendData();
// work first with the cached data
await backendData.loadCachedConversationData();
await backendData.loadMiscData();
observeChatList (document.body);
displayDirectoryItems ();
}
async function onAuthReceived()
{
// now load it remotely
await backendData.downloadConversationData()
console.log ("auth received");
// modify the chats, adding an ID for them (it's been already done, it will fill the new chats)
observeChatList (document.body);
// finally populate folders again (should fix this, as it's already been populated now?)
displayDirectoryItems();
}
function onNewChatGPTWindow()
{
GTools.easyObserver (mainObserverFunc);
}
// All functions that modify the DOM go here.
function mainObserverFunc(element)
{
//console.log ("mainObserverFunc",element);
// note: those functions will be changing the DOM at their own pace
insertModelOnBottom (document.body);
createOurNavBar (document.body);
displayDirectoryItems();
observeChatList();
}
function observeChatList(newElement)
{
// never return true, as chat list must always be observed
if (backendData?.conversationData == null) return false;
const chatList = document.querySelectorAll ("a.flex.py-3.px-3.items-center.gap-3.relative");
if (chatList == null || chatList.length == 0) return false;
for (let chat of chatList)
{
// for each present chat
// make it draggable
chat.setAttribute ("draggable",true);
// get its title
const innerDiv = chat.querySelector ("div");
const innerText = innerDiv.innerText;
// search the title in the backendData
// Fixme: if two conversations have the same title, this will only work for the first one
const found = backendData.conversationData.find (item => item.title == innerText);
// if found, add the ID to the chat
if (found != null)
{
if (backendData.getConversationById (found.id) == null) console.log ("ERROR: backendData.getConversationById (found.id) is null");
chat.setAttribute (Constants.DraggableId,"aiChat_"+found.id);
addDragHandlers (chat);
// To ease filtering the chat, add a class to it
chat.classList.add ("systemChat");
}
}
return false;
}
function insertModelOnBottom(newElement)
{
// model already on the bottom, do nothing
if (newElement.querySelector ("#ourModelText") != null) return true;
// search for the model on the top (it may not be present)
// there's probably a better way, but I haven't coded in JS for 20 years.
let modelNameElement = newElement.querySelector(".flex.items-center.justify-center.gap-1.border-b.border-black\\/10.bg-gray-50.p-3.text-gray-500.dark\\:border-gray-900\\/50.dark\\:bg-gray-700.dark\\:text-gray-300");
let modelNameElement2 = document.querySelector(".flex.items-center.justify-center.gap-1.border-b.border-black\\/10.bg-gray-50.p-3.text-gray-500.dark\\:border-gray-900\\/50.dark\\:bg-gray-700.dark\\:text-gray-300");
if(modelNameElement2 != null && modelNameElement == null) console.log ("-------------------------- HERE fail");
if (modelNameElement == null) return false;
// search for a span with the ChatGPT warning text
const allSpans = Array.from (newElement.querySelectorAll("span"));
const chatGPTSpan = allSpans.find (span => span.textContent.includes ("ChatGPT may produce inaccurate information"));
// create a text node with the model name
const container=document.createElement ("div");
container.innerHTML=`
<div id='ourModelText'>
Current model : <b>${modelNameElement.textContent}</b>
</div>`;
chatGPTSpan.appendChild (container);
// save data about this chat and the model
// get current window.history state
let url = window.history.state.url;
let chatId = url.substring (url.lastIndexOf ("/")+1);
backendData.addModelData (modelNameElement.textContent, chatId);
return true;
}
function createOurNavBar(newElement)
{
// already created
if (newElement.querySelector ("#ourNavBar") != null) return;
// search nav (it may be not present sometimes? JS is weird)
const navElement = newElement.querySelector("nav[aria-label='Chat history']");
if (navElement == null) return;
// Create the toolbar div
const toolbar = document.createElement('div');
toolbar.setAttribute('id', 'ourNavBar');
toolbar.setAttribute('class', 'jtoolbar');
// create new folder button
let toolCreateFolder = document.createElement ("img");
toolCreateFolder.classList.add ("jtoolbar-icon");
toolbar.appendChild(toolCreateFolder);
toolCreateFolder.src = chrome.runtime.getURL (Constants.AddFolder);
toolCreateFolder.addEventListener ("click", newFolderClickedEventHandler);
// create a debug button
if (Constants.DEBUG)
{
let debugButton = document.createElement ("img");
debugButton.classList.add ("jtoolbar-icon");
debugButton.src = chrome.runtime.getURL ("images/folder.svg");
debugButton.setAttribute ("margin-left","40px");
toolbar.appendChild(debugButton);
debugButton.addEventListener ("click", ()=>
{
// refresh the folders
directoryItems.loadDirectoryItems();
});
}
// create a text box
let textBox = document.createElement ("input");
textBox.setAttribute ("id","jtoolbar-search");
textBox.setAttribute ("type","text");
textBox.setAttribute ("placeholder","Search");
toolbar.appendChild(textBox);
textBox.addEventListener ("input", ()=>
{
filterChats (textBox.value);
});
// insert the toolbar
navElement.insertBefore (toolbar, navElement.children[0].nextSibling);
console.log ("Added ourNavBar :"+toolbar);
}
function filterChats(text)
{
text = text.toLowerCase();
let chats = document.querySelectorAll (".systemChat");
for (let chat of chats)
{
let chatTitle = chat.querySelector ("div").innerText;
if (chatTitle.toLowerCase().includes (text) || text == "")
{
// show
chat.style.display = "";
}
else
{
// hide
chat.style.display = "none";
}
}
}
function newFolderClickedEventHandler (event)
{
// ask for the name of the folder as a popup
let userInput = prompt ("Enter the name of the folder", "New Folder");
if (userInput == null || userInput.trim() === "") return;
userInput = userInput.trim();
if (directoryItems.containsName (userInput))
{
alert ("A folder with that name already exists");
return;
}
directoryItems.addFolderChild (userInput);
directoryItems.saveDirectoryItems();
// refresh the display
displayDirectoryItems();
}
}
)();