-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.js
More file actions
243 lines (215 loc) · 7.16 KB
/
main.js
File metadata and controls
243 lines (215 loc) · 7.16 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
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
const RECORDING_KEY = "recording";
const HISTORY_KEY = "history";
const POSTGRES_ACTIVE_KEY = "pg_active";
const RANDOM_CALL_KEY = "random_call";
document.addEventListener("DOMContentLoaded", async () => {
const containerDiv = document.getElementById("container");
const spinnerDiv = document.getElementById("spinner");
async function getPgStatus() {
return new Promise((resolve) => {
chrome.storage.local.get([POSTGRES_ACTIVE_KEY], (result) => {
resolve(result.pg_active);
});
});
}
chrome.runtime.onMessage.addListener(async (message) => {
if (message.pg?.loaded) {
containerDiv.style.display = "block";
spinnerDiv.style.display = "none";
}
if (message.codec?.context) {
window.close();
}
});
const pg_loaded = await getPgStatus();
// alert(JSON.stringify({ pg_loaded }))
if (!pg_loaded) {
containerDiv.style.display = "none";
spinnerDiv.style.display = "block";
} else {
containerDiv.style.display = "block";
spinnerDiv.style.display = "none";
}
const startCallButton = document.getElementById("startCall");
const toggleButton = document.getElementById("toggleRecording");
const toggleRandomCallButton = document.getElementById("toggleRandomCall");
const showHistoryButton = document.getElementById("showHistory");
const clearStorageButton = document.getElementById("clearStorage");
const githubButton = document.getElementById("github");
const twitterButton = document.getElementById("twitter");
const toast = document.getElementById("toast");
const historyModal = document.getElementById("historyModal");
const viewModal = document.getElementById("viewModal");
const historyList = document.getElementById("historyList");
const markdownContent = document.getElementById("markdownContent");
const closeButtons = document.querySelectorAll(".close");
for (let elem of [
startCallButton,
toggleButton,
toggleRandomCallButton,
showHistoryButton,
clearStorageButton,
githubButton,
twitterButton,
]) {
elem.addEventListener("mouseover", function () {
chrome.runtime.sendMessage({
play: { source: "media/audio/0x1F.wav", volume: 1 },
});
});
elem.addEventListener("click", function () {
chrome.runtime.sendMessage({
play: { source: "media/audio/0x20.wav", volume: 1 },
});
});
}
// Initialize button state
const recording = await getRecordingStatus();
updateToggleButton(recording);
const randomcall = await getRandomCall();
updateToggleRandomCallButton(randomcall);
startCallButton.addEventListener("click", async () => {
chrome.runtime.sendMessage({ codec: { call: true } });
containerDiv.style.display = "none";
spinnerDiv.style.display = "block";
});
toggleButton.addEventListener("click", async () => {
const currentStatus = await getRecordingStatus();
const newStatus = !currentStatus;
await setRecordingStatus(newStatus);
updateToggleButton(newStatus);
});
toggleRandomCallButton.addEventListener("click", async () => {
const currentStatus = await getRandomCall();
const newStatus = !currentStatus;
await setRandomCall(newStatus);
updateToggleRandomCallButton(newStatus);
if (newStatus) {
alert(
"WARNING :\n\n- random codec calls automatically start new openAI realtime sessions , at random intervals defined in your config\n\n- be careful about api consumption costs or disable it\n\nie. don't enable & forget about it,\nthen leave your chrome on, away from your computer\nthen come back to a find an open session that consumed all your (autorenewable ?) credits !",
);
}
});
showHistoryButton.addEventListener("click", async () => {
const history = await getHistory();
displayHistory(history);
historyModal.style.display = "block";
});
clearStorageButton.addEventListener("click", async () => {
const confirmClear = confirm(
"Are you sure you want to clear all stored data?",
);
if (confirmClear) {
await clearStorage();
}
});
// Close modals when close button is clicked
closeButtons.forEach((btn) => {
btn.onclick = () => {
btn.parentElement.parentElement.style.display = "none";
if (btn.parentElement.parentElement.id === "historyModal") {
historyList.innerHTML = "";
}
if (btn.parentElement.parentElement.id === "viewModal") {
markdownContent.textContent = "";
}
};
});
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == historyModal) {
historyModal.style.display = "none";
historyList.innerHTML = "";
}
if (event.target == viewModal) {
viewModal.style.display = "none";
markdownContent.textContent = "";
}
};
// Functions
async function getRecordingStatus() {
return new Promise((resolve) => {
chrome.storage.local.get([RECORDING_KEY], (result) => {
resolve(result.recording);
});
});
}
async function setRecordingStatus(status) {
return new Promise((resolve) => {
chrome.storage.local.set({ [RECORDING_KEY]: status }, () => {
resolve();
});
});
}
async function getRandomCall() {
return new Promise((resolve) => {
chrome.storage.local.get([RANDOM_CALL_KEY], (result) => {
resolve(result.random_call);
});
});
}
async function setRandomCall(status) {
return new Promise((resolve) => {
chrome.storage.local.set({ [RANDOM_CALL_KEY]: status }, () => {
resolve();
});
});
}
async function getHistory() {
return new Promise((resolve) => {
chrome.storage.local.get([HISTORY_KEY], (result) => {
resolve(result.history || []);
});
});
}
async function clearStorage() {
return new Promise((resolve) => {
chrome.runtime.sendMessage({ action: "clearStorage" }, (response) => {
if (response && response.success) {
resolve();
}
});
});
}
function updateToggleButton(isRecording) {
toggleButton.textContent = isRecording
? "Collect Enabled 🟢"
: "Collect Disabled 🔴";
}
function updateToggleRandomCallButton(isEnabled) {
toggleRandomCallButton.textContent = isEnabled
? "Random Calls Enabled 🟢⚠️"
: "Random Calls Disabled 🔴";
}
function showToast(message) {}
async function displayHistory(history) {
historyList.innerHTML = "";
if (history.length === 0) {
historyList.innerHTML = "<p>No history available.</p>";
return;
}
// Reverse to show latest first
const reversedHistory = [...history].reverse();
reversedHistory.forEach((entry, index) => {
const div = document.createElement("div");
div.style = "border-color:#444;";
div.className =
"url-item border-b p-1 pb-2 m-1 flex justify-between items-center whitespace-pre-wrap break-all";
const info = document.createElement("div");
const displayUrl =
entry.url.length > 30 ? entry.url.slice(0, 30) + "..." : entry.url;
info.innerHTML = `<strong>${displayUrl}</strong><br><em>${new Date(entry.timestamp).toLocaleString()}</em>`;
const viewButton = document.createElement("button");
viewButton.textContent = "View";
viewButton.className =
"bg-green-900 hover:bg-green-800 text-white font-bold py-1 px-3 rounded";
viewButton.addEventListener("click", () => {
markdownContent.textContent = entry.content;
viewModal.style.display = "block";
});
div.appendChild(info);
div.appendChild(viewButton);
historyList.appendChild(div);
});
}
});