-
Notifications
You must be signed in to change notification settings - Fork 6
/
Options.js
480 lines (404 loc) · 15.9 KB
/
Options.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Define a variable to store the profiles
let profiles = [];
const apiKeyInput = document.getElementById('apikey');
const temperatureInput = document.getElementById('temperature');
const maxTokenInput = document.getElementById('max_token');
const topPInput = document.getElementById('top_p');
const modelsInput = document.getElementById('models');
const autosubmitBox = document.getElementById('autosubmit');
const autocontextBox = document.getElementById('autocontext');
const contextmenuonlyBox = document.getElementById('contextmenuonly');
// Get the button elements
const saveButton = document.getElementById('save');
const restoreButton = document.getElementById('restore');
const newprofileButton = document.getElementById('newprofile');
const exportprofileButton = document.getElementById('exportprofile');
const importprofileButton = document.getElementById('importprofile');
loadsaveProfiles();
loadsaveOptions();
// Attach click event listeners to the buttons
saveButton.addEventListener('click', saveProfilesAndOptions);
restoreButton.addEventListener('click', restoreDefaults);
newprofileButton.addEventListener('click', addNewProfile);
exportprofileButton.addEventListener('click', exportProfiles);
importprofileButton.addEventListener('click', importProfiles);
function addNewProfile() {
const profileList = document.getElementById('GPTExtensionprofiles-list');
const listItem = document.createElement('li');
const index = profileList.childElementCount;
const newindex = index + 1;
listItem.id = `profile-${index}`;
listItem.className = 'GPTExtensionli';
listItem.draggable = true;
const buttonContainer = document.createElement('div');
buttonContainer.className = 'GPTExtensionbutton-container';
const nameInput = document.createElement('input');
nameInput.className = 'GPTExtensionnameinput';
nameInput.type = 'text';
nameInput.id = `custom${index}`;
nameInput.name = `custom${index}`;
nameInput.value = `Profile ${newindex}`;
nameInput.addEventListener("blur", function () {
this.style.width = "30%";
});
nameInput.addEventListener("focus", function () {
this.style.width = "100%";
});
buttonContainer.appendChild(nameInput);
const userInput = document.createElement('textarea');
userInput.className = 'GPTExtensiontextareablur';
userInput.id = `user${index}`;
userInput.name = `user${index}`;
userInput.value = 'Acting as an Assistant';
userInput.style.height = "auto"; // set the initial height to auto
userInput.style.height = "30px";
userInput.addEventListener("input", function () {
this.style.height = "auto";
this.style.height = (this.scrollHeight) + "px";
});
userInput.addEventListener("blur", function () {
this.style.removeProperty('height'); // remove the height property when the element loses focus
});
userInput.addEventListener("focus", function () {
this.style.height = "auto";
this.style.height = (this.scrollHeight) + "px";
});
buttonContainer.appendChild(userInput);
const removeButton = document.createElement('button');
removeButton.className = 'GPTExtensionbutton';
removeButton.type = 'button';
removeButton.id = `removeProfile${index}`;
const removeButtonText = document.createElement('b');
removeButtonText.textContent = '-';
removeButton.appendChild(removeButtonText);
buttonContainer.appendChild(removeButton);
listItem.appendChild(buttonContainer);
// Add the list item to the profile list
profileList.insertBefore(listItem, profileList.firstChild);
listItem.addEventListener('dragstart', handleDragStart);
listItem.addEventListener('dragenter', handleDragEnter);
listItem.addEventListener('dragover', handleDragOver);
listItem.addEventListener('drop', handleDrop);
document.getElementById(`removeProfile${index}`).addEventListener('click', handleDeleteProfile);
}
function handleDeleteProfile(event) {
const listItem = event.target.closest('.GPTExtensionli');
if (listItem) {
listItem.remove();
}
}
// Loads the profiles from the Chrome storage
function loadsaveProfiles() {
chrome.storage.local.get(['saveprofiles'], function (result) {
console.log(result);
if (!result.saveprofiles||result.saveprofiles.length==0) {
createDefaultProfiles();
// save the new created profile
chrome.storage.local.set({ saveprofiles: profiles }, function () {
// Notify the user that the profiles were saved.
showStatus('Default profiles created.');
reloadProfileUI(profiles);
});
}
else {
profiles = result.saveprofiles;
reloadProfileUI(profiles);
}
});
};
function createDefaultProfiles() {
profiles = [];
//console.log("create default profiles");
// Define a new profile object
const newProfile1 = {
savecustom: 'translator',
saveuser: 'Rewrite following content in authentic English:'
};
// Add the new profile to the profiles array
profiles.push(newProfile1);
// Define a new profile object
const newProfile2 = {
savecustom: 'proofreader',
saveuser: 'Proofread following content using content\'s language:'
};
// Add the new profile to the profiles array
profiles.push(newProfile2);
// Define a new profile object
const newProfile3 = {
savecustom: 'summarizer',
saveuser: 'Summarize following content in less than 20 words:'
};
// Add the new profile to the profiles array
profiles.push(newProfile3);
return profiles;
};
function reloadProfileUI(newprofiles) {
// Get the profile list element
const profileList = document.getElementById('GPTExtensionprofiles-list');
profileList.innerHTML = '';
// Loop through each profile and create a list item element for it
newprofiles.forEach(function (profile, index) {
// Create a new list item element
const listItem = document.createElement('li');
// Set the ID of the list item to the index of the profile
listItem.id = 'profile-' + index;
listItem.className = 'GPTExtensionli';
listItem.draggable = true;
const buttonContainer = document.createElement('div');
buttonContainer.className = 'GPTExtensionbutton-container';
const nameInput = document.createElement('input');
nameInput.className = 'GPTExtensionnameinput';
nameInput.type = 'text';
nameInput.id = `custom${index}`;
nameInput.name = `custom${index}`;
nameInput.value = `${profile.savecustom}`;
nameInput.addEventListener("blur", function () {
this.style.width = "30%";
});
nameInput.addEventListener("focus", function () {
this.style.width = "100%";
});
buttonContainer.appendChild(nameInput);
const userInput = document.createElement('textarea');
userInput.className = 'GPTExtensiontextareablur';
userInput.id = `user${index}`;
userInput.name = `user${index}`;
userInput.value = `${profile.saveuser}`;
userInput.style.height = "auto"; // set the initial height to auto
userInput.style.height = "30px";
userInput.addEventListener("input", function () {
this.style.height = "auto";
this.style.height = (this.scrollHeight) + "px";
});
userInput.addEventListener("blur", function () {
this.style.removeProperty('height'); // remove the height property when the element loses focus
this.style.height = "30px";
});
userInput.addEventListener("focus", function () {
this.style.height = "auto";
this.style.height = (this.scrollHeight) + "px";
});
buttonContainer.appendChild(userInput);
const removeButton = document.createElement('button');
removeButton.className = 'GPTExtensionbutton';
removeButton.type = 'button';
removeButton.id = `removeProfile${index}`;
const removeButtonText = document.createElement('b');
removeButtonText.textContent = '-';
removeButton.appendChild(removeButtonText);
buttonContainer.appendChild(removeButton);
listItem.appendChild(buttonContainer);
// Add the list item to the profile list
profileList.appendChild(listItem);
listItem.addEventListener('dragstart', handleDragStart);
listItem.addEventListener('dragenter', handleDragEnter);
listItem.addEventListener('dragover', handleDragOver);
listItem.addEventListener('drop', handleDrop);
document.getElementById(`removeProfile${index}`).addEventListener('click', handleDeleteProfile);
});
};
function loadsaveOptions() {
// Load the saved options from storage
chrome.storage.local.get(['options'], (result) => {
// Set the saved options as the default values
const options = result.options;
if (options) {
reloadOptionUI(options);
}
else {
const defaultoptions = createDefaultOptions();
reloadOptionUI(defaultoptions);
saveOptions();
}
});
};
function createDefaultOptions() {
const options = {
saveapiKey: '',
savetemperature: '1',
savemaxToken: '512',
savetopP: '1',
savemodels: 'gpt-3.5-turbo',
saveautosubmit: '',
saveautocontext: '',
savecontextmenuonly: ''
};
return options;
};
function reloadOptionUI(options) {
apiKeyInput.value = options.saveapiKey;
temperatureInput.value = options.savetemperature;
maxTokenInput.value = options.savemaxToken;
topPInput.value = options.savetopP;
modelsInput.value = options.savemodels;
autosubmitBox.checked = options.saveautosubmit == "checked" ? "checked" : "";
autocontextBox.checked = options.saveautocontext == "checked" ? "checked" : "";
contextmenuonlyBox.checked = options.savecontextmenuonly == "checked" ? "checked" : "";
};
// Restore the default options
function restoreDefaults() {
reloadProfileUI(createDefaultProfiles());
reloadOptionUI(createDefaultOptions());
};
// Saves the profiles to the Chrome storage
function saveProfiles() {
const profilesList = document.getElementById('GPTExtensionprofiles-list');
profiles = [];
for (let i = 0; i < profilesList.children.length; i++) {
const customInput = profilesList.children[i].querySelector('.GPTExtensionnameinput');
const userInput = profilesList.children[i].querySelector('.GPTExtensiontextareablur');
const newprofile = {
savecustom: customInput.value,
saveuser: userInput.value
};
profiles.push(newprofile);
}
chrome.storage.local.set({ saveprofiles: profiles }, function () {
// Notify the user that the profiles were saved.
showStatus('Profiles saved.');
});
};
// Save the options to storage
function saveOptions() {
var apiKey = apiKeyInput.value;
var temperature = parseFloat(temperatureInput.value);
var maxToken = parseInt(maxTokenInput.value);
var topP = parseFloat(topPInput.value);
var models = modelsInput.value;
var autosubmit = document.getElementById('autosubmit').checked ? 'checked' : '';
var autocontext = document.getElementById('autocontext').checked ? 'checked' : '';
var contextmenuonly = document.getElementById('contextmenuonly').checked ? 'checked' : '';
// validate temperature input
if (temperature < 0 || temperature > 1 || isNaN(temperature)) {
alert('Temperature must be a number between 0 and 1.');
return;
};
// validate maxToken input
if (maxToken < 1 || maxToken > 2048 || isNaN(maxToken)) {
alert('Max Tokens must be a number between 1 and 2048.');
return;
};
// validate topP input
if (topP < 0 || topP > 1 || isNaN(topP)) {
alert('Top P must be a number between 0 and 1.');
return;
};
const optionstobesave = {
saveapiKey: apiKey,
savetemperature: temperature,
savemaxToken: maxToken,
savetopP: topP,
savemodels: models,
saveautosubmit: autosubmit,
saveautocontext: autocontext,
savecontextmenuonly: contextmenuonly
};
chrome.storage.local.set({ options: optionstobesave }, function () {
// Notify the user that the options were saved.
showStatus('Options saved.');
});
};
function saveProfilesAndOptions() {
saveProfiles();
saveOptions();
}
function handleDragStart(event) {
// Store the item's id in the dataTransfer object
event.dataTransfer.setData('text/plain', event.target.id);
};
function handleDragEnter(event) {
// Prevent default to allow drop
event.preventDefault();
};
function handleDragOver(event) {
// Prevent default to allow drop
event.preventDefault();
};
function handleDrop(event) {
// Get the id of the dragged item
const id = event.dataTransfer.getData('text/plain');
//console.log(id);
// Get the elements for the dragged and dropped items
const draggedItem = document.getElementById(id);
const droppedItem = event.target.closest('.GPTExtensionli');
const dropzone = event.target.closest('#GPTExtensionprofiles-list');
const droppedIndex = Array.from(dropzone.children).indexOf(droppedItem);
//console.log(droppedItem);
//console.log(dropzone);
// Move the dragged item to the dropzone
// dropzone.appendChild(draggedItem);
// Get the index of the dragged item
const draggedIndex = Array.from(dropzone.children).indexOf(draggedItem);
// Move the dragged item to the dropzone
if (draggedIndex < droppedIndex) {
// Insert the dragged item after the dropped item
dropzone.insertBefore(draggedItem, droppedItem.nextSibling);
} else {
// Insert the dragged item before the dropped item
dropzone.insertBefore(draggedItem, droppedItem);
}
};
function exportProfiles() {
// Convert the profiles array to a JSON string
const profilesJSON = JSON.stringify({ profiles });
// Create a Blob object with the JSON data
const blob = new Blob([profilesJSON], { type: "application/json" });
// Create a download link for the user
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "profiles.json";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
function importProfiles() {
const input = document.createElement("input");
input.type = "file";
input.accept = ".json,.csv";
// Listen for the change event on the input element
input.addEventListener("change", (event) => {
const file = event.target.files[0];
// Create a new FileReader object
const reader = new FileReader();
// Listen for the load event on the FileReader object
reader.addEventListener("load", () => {
// Parse the data based on the file type
let importedProfiles;
if (file.name.endsWith(".json")) {
importedProfiles = JSON.parse(reader.result).profiles;
} else if (file.name.endsWith(".csv")) {
importedProfiles = [];
const lines = reader.result.split("\n");
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
const regex = /"([^"]*)","([^"]*)"/;
const matches = line.match(regex);
if (matches) {
const savecustom = matches[1].trim();
const saveuser = matches[2].trim();
importedProfiles.push({ savecustom, saveuser });
};
}
};
// Merge the imported data into the profiles array
profiles = [...importedProfiles, ...profiles];
// Do something with the merged profiles data
reloadProfileUI(profiles);
});
// Read the contents of the selected file as a text string
reader.readAsText(file);
});
// Click the input element to trigger the file selection dialog
input.click();
}
// In your options.js file
document.getElementById('openTab').addEventListener('click', function () {
chrome.tabs.create({ url: chrome.runtime.getURL('playground.html') });
});
function showStatus(infostring) {
document.getElementById('status').textContent = infostring;
setTimeout(function () {
document.getElementById('status').textContent = '';
}, 3000);
}