-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
348 lines (295 loc) · 10.1 KB
/
renderer.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// renderer.js
const monitoredProcesses = new Map();
let monitoringInterval = null;
function initialize() {
// Initialize notifications
if (
Notification.permission !== 'granted' &&
Notification.permission !== 'denied'
) {
Notification.requestPermission();
}
// Sidebar Navigation Event Listeners
document.querySelectorAll('.sidebar-item').forEach((item) => {
item.addEventListener('click', () => {
const tabName = item.getAttribute('data-tab');
activateTab(tabName);
});
// Enable keyboard navigation
item.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
const tabName = item.getAttribute('data-tab');
activateTab(tabName);
}
});
});
// Save preferences when the user clicks "Save"
document.getElementById('savePreferences').addEventListener('click', () => {
savePreferences();
});
// Initialize notifications
if (Notification.permission !== 'granted') {
Notification.requestPermission();
}
// Window Control Buttons
/*
document.getElementById('minimize-button').addEventListener('click', () => {
window.electronAPI.windowControl('minimize');
});
document.getElementById('maximize-button').addEventListener('click', () => {
window.electronAPI.windowControl('maximize');
});
document.getElementById('close-button').addEventListener('click', () => {
window.electronAPI.windowControl('close');
});
*/
document.getElementById('quit-app-button').addEventListener('click', () => {
window.electronAPI.quitApp();
});
// Enable keyboard navigation for the Quit App button
document
.getElementById('quit-app-button')
.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
window.electronAPI.quitApp();
}
});
// Initial tab
activateTab('processes');
}
function activateTab(tabName) {
// Hide all tab contents
document.querySelectorAll('.tab-content').forEach((tab) => {
tab.classList.remove('active');
tab.style.display = 'none';
});
// Show the selected tab content
const activeTab = document.getElementById(`${tabName}-tab`);
activeTab.style.display = 'block';
setTimeout(() => {
activeTab.classList.add('active');
}, 0);
// Update active sidebar item
document.querySelectorAll('.sidebar-item').forEach((item) => {
item.classList.remove('active');
});
document
.querySelector(`.sidebar-item[data-tab="${tabName}"]`)
.classList.add('active');
if (tabName === 'processes') {
listProcesses();
} else if (tabName === 'preferences') {
loadPreferences();
}
}
function loadPreferences() {
window.electronAPI.getPreferences().then((preferences) => {
document.getElementById('autoLaunch').checked = preferences.autoLaunch;
document.getElementById('prefilterRegex').value =
preferences.prefilterRegex || '';
});
}
function savePreferences() {
const autoLaunch = document.getElementById('autoLaunch').checked;
const prefilterRegex = document.getElementById('prefilterRegex').value.trim();
window.electronAPI.savePreferences({ autoLaunch, prefilterRegex });
// Reload processes after saving preferences
if (document.getElementById('processes-tab').style.display === 'block') {
listProcesses();
}
}
async function listProcesses() {
const processes = await window.electronAPI.getProcesses();
const filterValue = document
.getElementById('filter-input')
.value.toLowerCase();
// Get the prefilter regex from preferences
const prefs = await window.electronAPI.getPreferences();
const { prefilterRegex } = prefs;
console.log('Prefilter Regex:', prefilterRegex);
let prefilterPattern = null;
if (prefilterRegex && prefilterRegex.trim() !== '') {
try {
prefilterPattern = new RegExp(prefilterRegex.trim(), 'i'); // 'i' for case-insensitive
console.log('Prefilter Pattern:', prefilterPattern);
} catch (e) {
console.error('Invalid prefilter regex:', e);
alert('Invalid prefilter regular expression in preferences.');
return;
}
} else {
console.log('No prefilter regex provided; displaying all processes.');
}
// Filter processes based on the prefilter regex and filter input
const scriptProcesses = processes.filter((proc) => {
const nameMatchesFilter =
filterValue === '' ||
proc.name.toLowerCase().includes(filterValue) ||
proc.cmd.toLowerCase().includes(filterValue);
if (prefilterPattern) {
const prefilterMatch =
prefilterPattern.test(proc.name) || prefilterPattern.test(proc.cmd);
return prefilterMatch && nameMatchesFilter;
}
return nameMatchesFilter;
});
const processTableBody = document.querySelector('#process-table-body');
processTableBody.innerHTML = '';
if (scriptProcesses.length === 0) {
const noDataRow = document.createElement('tr');
const noDataCell = document.createElement('td');
noDataCell.colSpan = 4;
noDataCell.classList.add('text-center');
noDataCell.textContent = 'No scripts found.';
noDataRow.appendChild(noDataCell);
processTableBody.appendChild(noDataRow);
} else {
scriptProcesses.forEach((proc) => {
const row = document.createElement('tr');
if (monitoredProcesses.has(proc.pid)) {
row.classList.add('highlighted-row');
}
// Checkbox Cell
const checkboxCell = document.createElement('td');
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.value = proc.pid;
checkbox.checked = monitoredProcesses.has(proc.pid);
checkbox.addEventListener('change', (e) => {
const pid = parseInt(e.target.value);
const processName = proc.name;
if (e.target.checked) {
if (!monitoredProcesses.has(pid)) {
monitoredProcesses.set(pid, processName);
console.log(
`Added process ${processName} (PID ${pid}) to monitoring.`
);
if (monitoringInterval === null) {
startMonitoring();
}
}
} else {
monitoredProcesses.delete(pid);
console.log(`Removed process (PID ${pid}) from monitoring.`);
if (monitoredProcesses.size === 0 && monitoringInterval !== null) {
clearInterval(monitoringInterval);
monitoringInterval = null;
}
}
// Update monitoring status
updateMonitoringStatus();
});
checkboxCell.appendChild(checkbox);
row.appendChild(checkboxCell);
// PID Cell
const pidCell = document.createElement('td');
pidCell.textContent = proc.pid;
row.appendChild(pidCell);
// Name Cell
const nameCell = document.createElement('td');
nameCell.textContent = proc.name;
row.appendChild(nameCell);
// Command Cell
const cmdCell = document.createElement('td');
cmdCell.textContent = proc.cmd;
row.appendChild(cmdCell);
processTableBody.appendChild(row);
});
}
}
// Auto-refresh the process list every 2 seconds
setInterval(() => {
if (document.getElementById('processes-tab').style.display === 'block') {
listProcesses();
}
}, 2000);
// Debounce function to limit the rate of function calls
function debounce(func, wait) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
const filterInput = document.getElementById('filter-input');
filterInput.addEventListener(
'input',
debounce(() => {
if (filterInput.value.length >= 2 || filterInput.value.length === 0) {
listProcesses();
}
}, 300)
);
function startMonitoring() {
if (monitoringInterval !== null) {
return;
}
monitoringInterval = setInterval(async () => {
const processes = await window.electronAPI.getProcesses();
const runningPIDs = processes.map((proc) => proc.pid);
monitoredProcesses.forEach((processName, pid) => {
if (!runningPIDs.includes(pid)) {
// Process has ended
notifyProcessEnded(pid, processName);
// Remove from monitoredProcesses
monitoredProcesses.delete(pid);
// Update the UI
const checkbox = document.querySelector(
`input[type="checkbox"][value="${pid}"]`
);
if (checkbox) {
checkbox.checked = false;
// Remove highlight
const row = checkbox.closest('tr');
if (row) {
row.classList.remove('highlighted-row');
}
}
// Update monitoring status
updateMonitoringStatus();
}
});
if (monitoredProcesses.size === 0) {
clearInterval(monitoringInterval);
monitoringInterval = null;
}
}, 5000); // Check every 5 seconds
}
function notifyProcessEnded(pid, processName) {
console.log(`Process "${processName}" (PID ${pid}) has ended.`);
// Display a desktop notification
if (Notification.permission === 'granted') {
new Notification('Process Ended', {
body: `Process "${processName}" (PID ${pid}) has ended.`,
});
} else if (Notification.permission !== 'denied') {
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
new Notification('Process Ended', {
body: `Process "${processName}" (PID ${pid}) has ended.`,
});
} else {
alert(`Process "${processName}" (PID ${pid}) has ended.`);
}
});
} else {
// If permission was denied
alert(`Process "${processName}" (PID ${pid}) has ended.`);
}
// Play a sound notification
playNotificationSound();
}
function playNotificationSound() {
const audio = new Audio('notification-sound.mp3'); // Ensure you have this file in your project's directory
audio.play();
}
function updateMonitoringStatus() {
// Send the number of monitored processes to main process
window.electronAPI.updateTrayTooltip(monitoredProcesses.size);
// Also send the monitoredProcesses map
window.electronAPI.updateMonitoredProcesses(
Array.from(monitoredProcesses.entries())
);
}
// Initialize the app when the content is loaded
document.addEventListener('DOMContentLoaded', initialize);