-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpopup.js
More file actions
144 lines (122 loc) · 5.03 KB
/
popup.js
File metadata and controls
144 lines (122 loc) · 5.03 KB
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
document.addEventListener('DOMContentLoaded', function() {
const promptInput = document.getElementById('prompt');
const checkboxes = document.querySelectorAll('input[type="checkbox"][name="ai_options"]');
const historyContainer = document.getElementById('historyContainer');
const clearHistoryBtn = document.createElement('button');
document.body.appendChild(historyContainer);
document.body.appendChild(clearHistoryBtn);
clearHistoryBtn.textContent = 'Clear History';
clearHistoryBtn.style.cursor = 'pointer';
clearHistoryBtn.addEventListener('click', function() {
chrome.storage.local.set({ 'promptHistory': [] }, function() {
updatePromptHistoryDisplay([]);
});
});
function updatePromptHistoryDisplay(history) {
historyContainer.innerHTML = '<h3>History<h3>'; // Add a title to the history container
history.forEach((prompt, index) => {
let promptDiv = document.createElement('div');
promptDiv.textContent = prompt.length > 90 ? prompt.substring(0, 90) + '...' : prompt;
promptDiv.style.cursor = 'pointer';
// When clicking in the history item
promptDiv.addEventListener('click', () => {
promptInput.value = prompt; // Populate the prompt input on click
savePromptText();
handleSubmit(false); // Send the prompt and do not save history for this
});
historyContainer.appendChild(promptDiv);
});
}
function savePromptHistory(prompt) {
chrome.storage.local.get(['promptHistory'], function(result) {
let promptHistory = result.promptHistory || [];
promptHistory.unshift(prompt); // Add the new prompt to the beginning of the array
if (promptHistory.length > 20) { // Keep only the last 20 prompts
// Since we're adding to the beginning, we trim from the end
promptHistory = promptHistory.slice(0, 20);
}
chrome.storage.local.set({ 'promptHistory': promptHistory }, function() {
updatePromptHistoryDisplay(promptHistory);
});
});
}
document.getElementById('closeBtn').addEventListener('click', function() {
window.close(); // Close the popup window
});
function saveCheckboxState() {
let services = [];
checkboxes.forEach(checkbox => {
if (checkbox.checked) {
services.push(checkbox.value);
}
});
chrome.storage.local.set({ 'services': services }, function() {
console.log('Checkbox state saved.');
});
}
function savePromptText() {
const promptText = promptInput.value;
chrome.storage.local.set({ 'promptText': promptText }, function() {
console.log('Prompt text saved.');
});
}
chrome.storage.local.get(['services', 'promptText', 'promptHistory'], function(result) {
if (result.services) {
checkboxes.forEach(checkbox => {
checkbox.checked = result.services.includes(checkbox.value);
});
}
if (result.promptText) {
promptInput.value = result.promptText;
promptInput.focus();
promptInput.select();
}
if (result.promptHistory) {
updatePromptHistoryDisplay(result.promptHistory);
}
});
checkboxes.forEach(checkbox => {
checkbox.addEventListener('change', function() {
saveCheckboxState();
savePromptText();
});
});
promptInput.addEventListener('input', savePromptText);
function handleSubmit(saveHistory) {
const prompt = promptInput.value.trim();
if (prompt === '') return;
if (saveHistory) {
savePromptHistory(prompt);
}
let services = [];
checkboxes.forEach(checkbox => {
if (checkbox.checked) {
services.push(checkbox.value);
}
});
chrome.runtime.sendMessage({ prompt: prompt, services: services });
window.close(); // Close the popup after submission
}
promptInput.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
if (!event.shiftKey) {
event.preventDefault();
handleSubmit(true);
}
}
});
function insertNewLine() {
const currentValue = promptInput.value;
const currentCaretPosition = promptInput.selectionStart;
const newText = currentValue.substring(0, currentCaretPosition) + '\n' + currentValue.substring(currentCaretPosition);
promptInput.value = newText;
promptInput.selectionStart = currentCaretPosition + 1;
promptInput.selectionEnd = currentCaretPosition + 1;
}
promptInput.addEventListener('keydown', function(event) {
if (event.key === 'Enter' && event.shiftKey) {
event.preventDefault();
insertNewLine();
}
});
});