forked from collabora/WhisperLive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
186 lines (161 loc) · 6.02 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
// Wait for the DOM content to be fully loaded
document.addEventListener("DOMContentLoaded", function () {
const startButton = document.getElementById("startCapture");
const stopButton = document.getElementById("stopCapture");
const useServerCheckbox = document.getElementById("useServerCheckbox");
const useVadCheckbox = document.getElementById("useVadCheckbox");
const languageDropdown = document.getElementById('languageDropdown');
const taskDropdown = document.getElementById('taskDropdown');
const modelSizeDropdown = document.getElementById('modelSizeDropdown');
let selectedLanguage = null;
let selectedTask = taskDropdown.value;
let selectedModelSize = modelSizeDropdown.value;
// Add click event listeners to the buttons
startButton.addEventListener("click", startCapture);
stopButton.addEventListener("click", stopCapture);
// Retrieve capturing state from storage on popup open
chrome.storage.local.get("capturingState", ({ capturingState }) => {
if (capturingState && capturingState.isCapturing) {
toggleCaptureButtons(true);
} else {
toggleCaptureButtons(false);
}
});
// Retrieve checkbox state from storage on popup open
chrome.storage.local.get("useServerState", ({ useServerState }) => {
if (useServerState !== undefined) {
useServerCheckbox.checked = useServerState;
}
});
chrome.storage.local.get("useVadState", ({ useVadState }) => {
if (useVadState !== undefined) {
useVadCheckbox.checked = useVadState;
}
});
chrome.storage.local.get("selectedLanguage", ({ selectedLanguage: storedLanguage }) => {
if (storedLanguage !== undefined) {
languageDropdown.value = storedLanguage;
selectedLanguage = storedLanguage;
}
});
chrome.storage.local.get("selectedTask", ({ selectedTask: storedTask }) => {
if (storedTask !== undefined) {
taskDropdown.value = storedTask;
selectedTask = storedTask;
}
});
chrome.storage.local.get("selectedModelSize", ({ selectedModelSize: storedModelSize }) => {
if (storedModelSize !== undefined) {
modelSizeDropdown.value = storedModelSize;
selectedModelSize = storedModelSize;
}
});
// Function to handle the start capture button click event
async function startCapture() {
// Ignore click if the button is disabled
if (startButton.disabled) {
return;
}
// Get the current active tab
const currentTab = await getCurrentTab();
// Send a message to the background script to start capturing
let host = "localhost";
let port = "9090";
const useCollaboraServer = useServerCheckbox.checked;
if (useCollaboraServer){
host = "transcription.kurg.org"
port = "7090"
}
chrome.runtime.sendMessage(
{
action: "startCapture",
tabId: currentTab.id,
host: host,
port: port,
language: selectedLanguage,
task: selectedTask,
modelSize: selectedModelSize,
useVad: useVadCheckbox.checked,
}, () => {
// Update capturing state in storage and toggle the buttons
chrome.storage.local.set({ capturingState: { isCapturing: true } }, () => {
toggleCaptureButtons(true);
});
}
);
}
// Function to handle the stop capture button click event
function stopCapture() {
// Ignore click if the button is disabled
if (stopButton.disabled) {
return;
}
// Send a message to the background script to stop capturing
chrome.runtime.sendMessage({ action: "stopCapture" }, () => {
// Update capturing state in storage and toggle the buttons
chrome.storage.local.set({ capturingState: { isCapturing: false } }, () => {
toggleCaptureButtons(false);
});
});
}
// Function to get the current active tab
async function getCurrentTab() {
return new Promise((resolve) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
resolve(tabs[0]);
});
});
}
// Function to toggle the capture buttons based on the capturing state
function toggleCaptureButtons(isCapturing) {
startButton.disabled = isCapturing;
stopButton.disabled = !isCapturing;
useServerCheckbox.disabled = isCapturing;
useVadCheckbox.disabled = isCapturing;
modelSizeDropdown.disabled = isCapturing;
languageDropdown.disabled = isCapturing;
taskDropdown.disabled = isCapturing;
startButton.classList.toggle("disabled", isCapturing);
stopButton.classList.toggle("disabled", !isCapturing);
}
// Save the checkbox state when it's toggled
useServerCheckbox.addEventListener("change", () => {
const useServerState = useServerCheckbox.checked;
chrome.storage.local.set({ useServerState });
});
useVadCheckbox.addEventListener("change", () => {
const useVadState = useVadCheckbox.checked;
chrome.storage.local.set({ useVadState });
});
languageDropdown.addEventListener('change', function() {
if (languageDropdown.value === "") {
selectedLanguage = null;
} else {
selectedLanguage = languageDropdown.value;
}
chrome.storage.local.set({ selectedLanguage });
});
taskDropdown.addEventListener('change', function() {
selectedTask = taskDropdown.value;
chrome.storage.local.set({ selectedTask });
});
modelSizeDropdown.addEventListener('change', function() {
selectedModelSize = modelSizeDropdown.value;
chrome.storage.local.set({ selectedModelSize });
});
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
if (request.action === "updateSelectedLanguage") {
const detectedLanguage = request.detectedLanguage;
if (detectedLanguage) {
languageDropdown.value = detectedLanguage;
chrome.storage.local.set({ selectedLanguage: detectedLanguage });
}
}
});
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
if (request.action === "toggleCaptureButtons") {
toggleCaptureButtons(false);
chrome.storage.local.set({ capturingState: { isCapturing: false } })
}
});
});