-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
384 lines (336 loc) · 13.2 KB
/
main.js
File metadata and controls
384 lines (336 loc) · 13.2 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
const { app, BrowserWindow, ipcMain, desktopCapturer } = require('electron');
const path = require('path');
const io = require('socket.io-client');
let mainWindow;
let floatingWindow;
let captureWindow;
let socket;
let isCapturing = false;
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 400,
height: 200,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
// Create a simple HTML file for the main window
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<style>
body {
background: #1a1a1a;
color: white;
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
padding: 20px;
}
.button {
background: #4F46E5;
color: white;
padding: 12px 24px;
border-radius: 50px;
border: none;
cursor: pointer;
font-size: 16px;
margin: 10px;
transition: all 0.3s ease;
}
.button:hover {
background: #4338CA;
transform: translateY(-2px);
}
.button:disabled {
background: #9CA3AF;
cursor: not-allowed;
transform: none;
}
#status {
color: #9CA3AF;
margin-top: 10px;
font-size: 14px;
}
</style>
</head>
<body>
<button id="startBtn" class="button" disabled>Start Detection</button>
<button id="stopBtn" class="button" style="display: none;">Stop Detection</button>
<div id="status">Connecting to server...</div>
</body>
<script>
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const status = document.getElementById('status');
startBtn.addEventListener('click', () => {
window.electronAPI.startCapture();
});
stopBtn.addEventListener('click', () => {
window.electronAPI.stopCapture();
});
// Update UI based on connection status
function updateConnectionStatus(isConnected) {
console.log('Updating connection status:', isConnected);
startBtn.disabled = !isConnected;
status.textContent = isConnected ? 'Connected to server' : 'Disconnected from server';
status.style.color = isConnected ? '#4F46E5' : '#EF4444';
}
// Initial state
updateConnectionStatus(false);
</script>
</html>
`;
// Write the HTML content to a temporary file
const fs = require('fs');
const tempHtmlPath = path.join(__dirname, 'temp.html');
fs.writeFileSync(tempHtmlPath, htmlContent);
// Load the HTML file
mainWindow.loadFile(tempHtmlPath);
// Clean up the temporary file after loading
mainWindow.webContents.on('did-finish-load', () => {
fs.unlinkSync(tempHtmlPath);
});
// Initialize socket connection with explicit IPv4 address
socket = io('http://127.0.0.1:5000', {
reconnection: true,
reconnectionAttempts: 5,
reconnectionDelay: 1000,
timeout: 10000,
transports: ['websocket', 'polling']
});
socket.on('connect', () => {
console.log('Connected to server');
mainWindow.webContents.executeJavaScript(`
console.log('Socket connected, enabling button');
updateConnectionStatus(true);
`);
});
socket.on('connect_error', (error) => {
console.error('Connection error:', error);
mainWindow.webContents.executeJavaScript(`
console.log('Socket connection error, disabling button');
updateConnectionStatus(false);
`);
});
socket.on('disconnect', () => {
console.log('Disconnected from server');
mainWindow.webContents.executeJavaScript(`
console.log('Socket disconnected, disabling button');
updateConnectionStatus(false);
`);
stopCapture();
});
socket.on('detection_result', (data) => {
console.log('Received detection result:', data);
if (floatingWindow) {
floatingWindow.webContents.send('update-detection', data);
}
});
// Force a connection status check after a short delay
setTimeout(() => {
mainWindow.webContents.executeJavaScript(`
console.log('Checking initial connection status');
updateConnectionStatus(${socket.connected});
`);
}, 1000);
// Add error handler for the main window
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.executeJavaScript(`
document.getElementById('startBtn').disabled = true;
console.log('Waiting for server connection...');
`);
});
}
function createFloatingWindow() {
floatingWindow = new BrowserWindow({
width: 250,
height: 100,
frame: false,
transparent: true,
alwaysOnTop: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
floatingWindow.loadFile('floating_window.html');
floatingWindow.setAlwaysOnTop(true, 'floating');
}
async function startCapture() {
try {
const sources = await desktopCapturer.getSources({
types: ['screen'],
thumbnailSize: { width: 1920, height: 1080 }
});
if (sources.length === 0) {
throw new Error('No screen sources found');
}
captureWindow = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
// Create a temporary HTML file for the capture window
const captureHtmlContent = `
<!DOCTYPE html>
<html>
<body>
<video id="video" autoplay></video>
<canvas id="canvas" style="display: none;" width="320" height="240"></canvas>
<script>
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let captureInterval;
async function startCapture() {
try {
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: '${sources[0].id}',
minWidth: 320,
maxWidth: 320,
minHeight: 240,
maxHeight: 240
}
}
});
video.srcObject = stream;
await video.play();
// Clear any existing interval
if (captureInterval) {
clearInterval(captureInterval);
}
// Start capturing frames
captureInterval = setInterval(() => {
if (video.videoWidth === 0 || video.videoHeight === 0) return;
// Draw the current frame to canvas
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
// Convert to JPEG with lower quality for better performance
const frame = canvas.toDataURL('image/jpeg', 0.5);
// Send the frame to the main process
window.electronAPI.sendFrame(frame);
}, 1000); // Capture every second
} catch (err) {
console.error('Capture error:', err);
window.electronAPI.sendError(err.message);
}
}
// Start capture when the page loads
startCapture();
// Cleanup when the window is closed
window.addEventListener('beforeunload', () => {
if (captureInterval) {
clearInterval(captureInterval);
}
if (video.srcObject) {
video.srcObject.getTracks().forEach(track => track.stop());
}
});
</script>
</body>
</html>
`;
// Write the HTML content to a temporary file
const fs = require('fs');
const tempCaptureHtmlPath = path.join(__dirname, 'temp_capture.html');
fs.writeFileSync(tempCaptureHtmlPath, captureHtmlContent);
// Load the HTML file
await captureWindow.loadFile(tempCaptureHtmlPath);
// Clean up the temporary file after loading
captureWindow.webContents.on('did-finish-load', () => {
fs.unlinkSync(tempCaptureHtmlPath);
});
isCapturing = true;
mainWindow.webContents.executeJavaScript(`
document.getElementById('startBtn').style.display = 'none';
document.getElementById('stopBtn').style.display = 'block';
`);
if (!floatingWindow) {
createFloatingWindow();
}
} catch (err) {
console.error('Error starting capture:', err);
mainWindow.webContents.executeJavaScript(`
alert('Error starting capture: ' + ${JSON.stringify(err.message)});
`);
}
}
function stopCapture() {
if (captureWindow) {
// Send a message to the capture window to cleanup
captureWindow.webContents.executeJavaScript(`
if (window.captureInterval) {
clearInterval(window.captureInterval);
}
if (document.getElementById('video').srcObject) {
document.getElementById('video').srcObject.getTracks().forEach(track => track.stop());
}
`);
captureWindow.close();
captureWindow = null;
}
isCapturing = false;
mainWindow.webContents.executeJavaScript(`
document.getElementById('startBtn').style.display = 'block';
document.getElementById('stopBtn').style.display = 'none';
`);
}
app.whenReady().then(() => {
createMainWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
ipcMain.on('start-capture', () => {
startCapture();
});
ipcMain.on('stop-capture', () => {
stopCapture();
});
ipcMain.on('close-floating-window', () => {
if (floatingWindow) {
floatingWindow.close();
floatingWindow = null;
}
});
ipcMain.on('frame-captured', (event, frame) => {
if (isCapturing && socket && socket.connected) {
console.log('Sending frame to server');
socket.emit('screen_frame', frame, (response) => {
console.log('Server response:', response);
});
} else {
console.log('Socket not connected or not capturing');
}
});
ipcMain.on('capture-error', (event, error) => {
console.error('Capture error:', error);
mainWindow.webContents.executeJavaScript(`
alert('Error capturing screen: ' + ${JSON.stringify(error)});
`);
stopCapture();
});