-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
188 lines (160 loc) · 6.25 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
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
let apiKey;
let configData; // Declare this to hold the loaded JSON data.
// Load the API key when the popup is opened.
chrome.storage.sync.get('apiKey', (data) => {
apiKey = data.apiKey || '';
document.getElementById('api-key').value = apiKey;
});
// Load configurations when the popup is opened.
loadConfigurations().then(() => {
// After configurations are loaded, get the team selection and set it.
chrome.storage.sync.get('team', (data) => {
const team = data.team || 'all';
document.getElementById('team-selector').value = team;
hideOptionsBasedOnTeam(team);
});
});
async function loadConfigurations() {
try {
const response = await fetch("config.json");
configData = await response.json(); // This line sets the global configData variable.
// Populate the team selector
let teamSelector = document.getElementById("team-selector");
teamSelector.innerHTML = ''; // Clear current options
configData.teamOptions.forEach(option => {
let opt = document.createElement("option");
opt.value = option.value;
opt.textContent = option.text;
teamSelector.appendChild(opt);
});
// Populate the role selector
let roleSelector = document.getElementById("role-selector");
roleSelector.innerHTML = ''; // Clear current options
configData.roleOptions.forEach(option => {
let opt = document.createElement("option");
opt.value = option.value;
opt.setAttribute("data-team", option['data-team']);
opt.textContent = option.text;
roleSelector.appendChild(opt);
});
} catch(err) {
console.error("Error reading config.json", err);
}
}
function hideOptionsBasedOnTeam(team) {
// Get the select dropdown with the id 'role-selector'
const roleSelector = document.getElementById('role-selector');
// Get all option elements
const options = roleSelector.querySelectorAll('option');
// This will track the first visible option when filtering based on the team
let firstVisibleOptionValue = null;
options.forEach(option => {
// If team is set to 'all', display all options and return early
if (team === 'all') {
option.style.display = '';
if (firstVisibleOptionValue === null) firstVisibleOptionValue = option.value;
return;
}
// Check the data-team attribute
const optionTeam = option.getAttribute('data-team');
if (optionTeam && optionTeam !== team) {
// Hide option if the data-team doesn't match the given team
option.style.display = 'none';
} else {
// Ensure that other options are visible if they match the team or if no data-team is set
option.style.display = '';
if (firstVisibleOptionValue === null) firstVisibleOptionValue = option.value;
}
});
// Set the roleSelector value to the first visible option value
roleSelector.value = firstVisibleOptionValue;
}
document.getElementById('team-selector').addEventListener('change', (e) => {
hideOptionsBasedOnTeam(e.target.value);
});
document.getElementById('save-settings').addEventListener('click', () => {
apiKey = document.getElementById('api-key').value;
chrome.storage.sync.set({ apiKey });
team = document.getElementById('team-selector').value;
chrome.storage.sync.set({ team });
});
document.getElementById('settings-button').addEventListener('click', () => {
const settings = document.getElementById('settings');
settings.style.display = settings.style.display === 'none' ? 'block' : 'none';
});
document.querySelectorAll('input[name="input-option"]').forEach((elem) => {
elem.addEventListener('change', () => {
document.getElementById('input-text').style.display =
document.querySelector('input[name="input-option"]:checked').value === 'enter'
? 'block'
: 'none';
});
});
document.getElementById('generate-response').addEventListener('click', async () => {
document.getElementById('generate-response').disabled = true;
const inputOption = document.querySelector('input[name="input-option"]:checked').value;
let inputText;
if (inputOption === 'highlight') {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const [result] = await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['getSelection.js'],
});
inputText = result.result;
} else {
inputText = document.getElementById('input-text').value;
}
if (!inputText) {
document.getElementById('response-container').innerText =
'Please highlight some text or enter some text before generating a response.';
return;
}
const roleSelector = document.getElementById('role-selector');
const selectedRole = roleSelector.value;
// Use the stored configData to fetch the role instructions
let selectedRoleConfig = configData.roleOptions.find(role => role.value === selectedRole);
let systemRoleContent = selectedRoleConfig ? selectedRoleConfig.role_instructions : '';
document.getElementById('spinner').style.display = 'block';
try {
const response = await getChatGPTResponse(inputText, systemRoleContent);
document.getElementById('response-container').innerText = response;
document.getElementById('copy-response').style.display = 'inline-block';
} catch (error) {
console.error(error);
document.getElementById('response-container').innerText =
'Error generating response. Please try again later.';
} finally {
document.getElementById('spinner').style.display = 'none';
}
});
document.getElementById('copy-response').addEventListener('click', async () => {
const responseText = document.getElementById('response-container').innerText;
if (responseText) {
await navigator.clipboard.writeText(responseText);
}
});
async function getChatGPTResponse(prompt, systemRoleContent) {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: 'gpt-4',
messages: [
{
role: 'system',
content: systemRoleContent,
},
{
role: 'user',
content: prompt,
},
],
temperature: 0.7,
}),
});
const data = await response.json();
return data.choices[0].message.content.trim();
}