-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
147 lines (133 loc) · 4.84 KB
/
popup.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
document.addEventListener("DOMContentLoaded", function () {
document.querySelector("#submit").addEventListener("click", onClickSubmit);
document.querySelector("#key").addEventListener("click", onClickKey);
document.querySelector("#saveBtn").addEventListener('click', onClickSave);
document.querySelector("#loadBtn").addEventListener('click', onClickLoad);
document.querySelector("#renameBtn").addEventListener('click', onClickRename);
document.querySelector("#deleteBtn").addEventListener('click', onClickDelete);
document.querySelector("#loading").style.display = "none";
var savedInputs = JSON.parse(localStorage.getItem('savedInputs')) || [];
updateSavedInputsDropdown(savedInputs);
});
function onClickSubmit() {
var queryInput = document.querySelector("#queryInput").value;
if (localStorage.getItem("key") === null) {
alert(
"No Key saved. click the key in the bottom right corner, and enter your key."
);
return;
}
const key = localStorage.getItem("key");
const openaiApiKey = key;
console.log("Talking to OpenAI API...");
console.log(`Key is: ${openaiApiKey}, Query is ${queryInput}`);
document.querySelector("#loading").style.display = "block";
document.querySelector("#menu").style.display = "none";
chrome.runtime.sendMessage(
{
method: "POST",
url: "https://api.openai.com/v1/chat/completions",
headers: {
Authorization: `Bearer ${openaiApiKey}`,
"Content-Type": "application/json",
},
data: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{"role": "system", "content": "You are a helpful assistant. output everything in HTML format."},
{
"role": "user",
"content": `${queryInput}`
}
],
}),
},
handleResponse
);
}
function onClickKey() {
var key = prompt(
"Enter your OpenAI API key. You can find it at https://beta.openai.com/account/api-keys"
);
if (key === "") {
alert(
"You have deleted your key. You will need to enter it again if you wish to continue."
);
localStorage.removeItem("key");
} else {
alert(
"You have saved your key!"
);
localStorage.setItem("key", key);
}
}
function onClickSave() {
var query = document.getElementById('queryInput').value;
var savedInputs = JSON.parse(localStorage.getItem('savedInputs')) || [];
var name = prompt('Enter a name for this input:', query);
if (name) {
savedInputs.push({ name: name, input: query });
localStorage.setItem('savedInputs', JSON.stringify(savedInputs));
updateSavedInputsDropdown(savedInputs);
}
}
function onClickLoad() {
var savedInputsDropdown = document.getElementById('savedInputs');
var selectedIndex = savedInputsDropdown.selectedIndex;
if (selectedIndex >= 0) {
var savedInputs = JSON.parse(localStorage.getItem('savedInputs')) || [];
document.getElementById('queryInput').value = savedInputs[selectedIndex].input;
}
}
function onClickRename() {
var savedInputsDropdown = document.getElementById('savedInputs');
var selectedIndex = savedInputsDropdown.selectedIndex;
if (selectedIndex >= 0) {
var newName = prompt('Enter the new name:', savedInputsDropdown.options[selectedIndex].text);
if (newName) {
var savedInputs = JSON.parse(localStorage.getItem('savedInputs')) || [];
savedInputs[selectedIndex].name = newName;
localStorage.setItem('savedInputs', JSON.stringify(savedInputs));
updateSavedInputsDropdown(savedInputs);
}
}
}
function onClickDelete() {
var savedInputsDropdown = document.getElementById('savedInputs');
var selectedIndex = savedInputsDropdown.selectedIndex;
if (selectedIndex >= 0) {
var confirmation = confirm('Are you sure you want to delete this item?');
if (confirmation) {
var savedInputs = JSON.parse(localStorage.getItem('savedInputs')) || [];
savedInputs.splice(selectedIndex, 1);
localStorage.setItem('savedInputs', JSON.stringify(savedInputs));
updateSavedInputsDropdown(savedInputs);
}
}
}
function updateSavedInputsDropdown(savedInputs) {
var dropdown = document.getElementById('savedInputs');
dropdown.innerHTML = '';
for (var i = 0; i < savedInputs.length; i++) {
var option = document.createElement('option');
option.text = savedInputs[i].name;
option.value = i;
dropdown.add(option);
}
}
function handleResponse(response) {
if (chrome.runtime.lastError) {
alert(`Error: ${chrome.runtime.lastError.message}`);
return;
}
if (response.error) {
alert(`Error: ${response.error}`);
return;
}
let answer = response.choices[0].message.content;
console.log(`Answer is: ${answer}`);
document.querySelector("#loading").style.display = "none";
document.querySelector("#key").style.display = "none";
answer = "<br>" + answer;
document.querySelector("#result").innerHTML = answer;
}