-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathelectron-main.js
721 lines (621 loc) · 20.2 KB
/
electron-main.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
const Sentry = require('@sentry/electron');
Sentry.init({
dsn: 'https://[email protected]/5910717',
});
const bytenode = require('bytenode');
const fs = require('fs');
const fetch = require('node-fetch');
const util = require('util');
const path = require('path');
const {qrScan} = require('./qrtest.js');
const WINDOWS = {
main: null,
tiktok: null,
settings: null,
camvas: null,
screenvas: null,
clips: null,
temp: null,
};
exports.WINDOWS = WINDOWS;
const awaitAllFileCompilations = async () => {
return Promise.all([
createBytecodeFile('main'),
createBytecodeFile('sendMailOnError'),
createBytecodeFile('pollclips'),
createBytecodeFile('downloadvideo'),
createBytecodeFile('settings'),
createBytecodeFile('editVideo'),
createBytecodeFile('clipqueue'),
createBytecodeFile('youtubeUpload'),
createBytecodeFile('helpers'),
createBytecodeFile('tiktokAPIUpload')
]);
};
async function createBytecodeFile(fileName) {
console.log('Attempting to compile ' + fileName);
if (fs.existsSync(path.join(__dirname, `../old/${fileName}.js`))) {
console.log(`Found ${fileName}.js`);
if (fs.existsSync(path.join(__dirname, `../compiled/${fileName}.jsc`))) {
console.log(`Deleting ${fileName}.jsc`);
fs.unlinkSync(path.join(__dirname, `../compiled/${fileName}.jsc`));
}
console.log('Actually compiling');
let compiled = await bytenode.compileFile(
path.join(__dirname, `../old/${fileName}.js`),
path.join(__dirname, `../compiled/${fileName}.jsc`)
);
console.log('Compilation: ' + compiled);
}
}
if (process.argv[2] == 'test') {
(async () => {
await awaitAllFileCompilations();
console.log('this is a test run');
process.exit(0);
})();
} else {
const {
app,
BrowserWindow,
ipcMain,
crashReporter,
globalShortcut,
Menu,
} = require('electron');
const AutoLaunch = require('auto-launch');
//Comment out next 2 lines to run local version - Delv
const { autoUpdater } = require('electron-updater');
crashReporter.start({
companyName: 'Rox Works',
productName: 'Clipbot',
ignoreSystemCrashHandler: true,
submitURL:
'https://o962298.ingest.sentry.io/api/5910717/minidump/?sentry_key=5ff48b9a970746fcb3f2cfdb33bf406b',
});
process?.crashReporter?.start({
companyName: 'Rox Works',
productName: 'Clipbot',
ignoreSystemCrashHandler: true,
submitURL:
'https://o962298.ingest.sentry.io/api/5910717/minidump/?sentry_key=5ff48b9a970746fcb3f2cfdb33bf406b',
});
let autoLaunchFunc = () => {
// jesus christ i think this is right
console.log('dir: ' + path.join(app.getAppPath(), '..\\..\\Clipbot.exe'));
let autoLaunch = new AutoLaunch({
name: 'Clipbot',
path: path.join(app.getAppPath(), '..\\..\\Clipbot.exe'), // maybe
});
autoLaunch.isEnabled().then((isEnabled) => {
if (!isEnabled) autoLaunch.enable();
});
};
const clearCache = async () => {
await WINDOWS.main.webContents.session.clearCache();
console.log('cache done');
return WINDOWS.main.webContents.session.clearStorageData();
}
const updateGlobalShortcut = (command = 'F10') => {
console.log('updating shortcut');
globalShortcut.unregisterAll();
// Register a 'CommandOrControl+X' shortcut listener.
const ret = globalShortcut.register(command, async () => {
//hit backend clip endpoint
console.log('creating clip from frontend');
let clipResponse = await fetch(`http://localhost:42074/clip`, {
method: 'post',
}).then((response) => response.json());
console.log('clip creation finished or failed');
if (clipResponse.status == 200) {
WINDOWS.main.webContents.send(
'clip_success',
JSON.stringify(clipResponse)
);
} else {
WINDOWS.main.webContents.send(
'clip_failed',
JSON.stringify(clipResponse)
);
}
console.log('Clip command pressed');
});
if (!ret) {
console.log('registration failed');
}
// Check whether a shortcut is registered.
console.log(globalShortcut.isRegistered(command));
app.on('will-quit', () => {
// Unregister a shortcut.
globalShortcut.unregister(command);
// Unregister all shortcuts.
globalShortcut.unregisterAll();
});
};
let mainWindow;
let server;
const setupServer = async () => {
if(server) {
return server;
}
const newServer = require('../compiled/main.jsc');
return Promise.resolve(newServer);
};
async function createWindow() {
if(WINDOWS.main) {
console.log('redirecting to existing window');
WINDOWS.main.focus();
return;
}
mainWindow = new BrowserWindow({
width: 900,
height: 850,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js'),
},
icon: './images/logo.png',
show: false,
});
WINDOWS.main = mainWindow;
WINDOWS.main.webContents.on('crashed', function () {
console.log('dont');
});
WINDOWS.main.useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36';
mainWindow.loadFile(path.join(__dirname, 'onboarding.html')); // index.html
mainWindow.on('closed', function () {
console.log('main window closed');
mainWindow = null;
WINDOWS.main = null;
});
mainWindow.setResizable(false);
autoLaunchFunc();
// copilot, write a function that crashes my electron app
mainWindow.once('ready-to-show', async () => {
autoUpdater.autoDownload = false;
// autoUpdater.allowPrerelease = true;
autoUpdater.checkForUpdatesAndNotify();
await setupServer();
mainWindow.show();
});
autoUpdater.on('update-available', () => {
mainWindow.webContents.send('update_available');
});
autoUpdater.on('update-downloaded', () => {
mainWindow.webContents.send('update_downloaded');
});
process.on('crash', (e) => {
console.log('rip: ' + e);
});
server = setupServer();
// const tiktokWindow = createTikTokWindow();
}
const createClipsWindow = () => {
if(WINDOWS.clips) {
console.log('redirecting to existing window');
WINDOWS.clips.focus();
return;
}
let clipsWindow = new BrowserWindow({
width: 900,
height: 875,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js'),
},
icon: './images/logo.png',
show: false
});
WINDOWS.clips = clipsWindow;
console.log('settings window set: ' + WINDOWS.settings);
clipsWindow.setResizable(false);
clipsWindow.loadFile(path.join(__dirname, 'clips.html'));
clipsWindow.on('ready-to-show', () => {
clipsWindow.show();
});
clipsWindow.on('closed', function () {
clipsWindow = null;
WINDOWS.clips = null;
});
mainWindow.on('closed', () => {
//also close tiktokWindow
if (clipsWindow) {
clipsWindow.close();
clipsWindow = null;
WINDOWS.clips = null;
}
});
};
const createSettingsWindow = () => {
if(WINDOWS.settings) {
console.log('redirecting to existing window');
WINDOWS.settings.focus();
return;
}
let settingsWindow = new BrowserWindow({
width: 550,
height: 925,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js'),
},
icon: './images/logo.png',
show: false,
});
WINDOWS.settings = settingsWindow;
console.log('settings window set: ' + WINDOWS.settings);
settingsWindow.setResizable(false);
settingsWindow.loadFile(path.join(__dirname, 'settings.html'));
settingsWindow.once('ready-to-show', async () => {
settingsWindow.show();
});
settingsWindow.on('closed', function () {
settingsWindow = null;
WINDOWS.settings = null;
});
mainWindow.on('closed', () => {
//also close tiktokWindow
if (settingsWindow) {
settingsWindow.close();
settingsWindow = null;
WINDOWS.settings = null;
}
});
};
const createTikTokWindow = async () => {
if(WINDOWS.tiktok) {
console.log('redirecting to existing window');
WINDOWS.tiktok.focus();
return;
}
let tiktokWindow = new BrowserWindow({
width: 550,
height: 900,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js'),
},
icon: './images/logo.png',
show: false,
});
// WINDOWS.tiktok = tiktokWindow;
// let mainPos = WINDOWS.main.getPosition();
// tiktokWindow.setPosition(mainPos[0] + 550, mainPos[1])
// tiktokWindow.setResizable(false);
const cookieClear = {
url: 'https://www.clipbot-mini-backend.com/',
name: 'clipbot_tiktok_sessionid',
value: '',
};
const cookieClear2 = {
url: 'https://www.clipbot-mini-backend.com/oauth',
name: 'clipbot_tiktok_sessionid',
value: '',
};
const cookieClear3 = {
url: 'https://www.clipbot-mini-backend.com/redirect',
name: 'clipbot_tiktok_sessionid',
value: '',
};
tiktokWindow.once('ready-to-show', function () {
tiktokWindow.show();
});
tiktokWindow.on('closed', function () {
tiktokWindow = null;
WINDOWS.tiktok = null;
});
mainWindow.on('closed', () => {
//also close tiktokWindow
if (WINDOWS.tiktok) {
tiktokWindow?.close();
tiktokWindow = null;
WINDOWS.tiktok = null;
}
});
//clear cookieClear from tiktokWindow
await tiktokWindow.webContents.session.clearStorageData([], (data) => {});
// await tiktokWindow.webContents.session.cookies.set(cookieClear2);
// let properAuthURL = 'https://clipbot-mini-backend.herokuapp.com/oauth';
let properAuthURL = 'https://clipbot-mini-backend-production.up.railway.app/oauth';
tiktokWindow.webContents.on('did-stop-loading', async () => {
if(!tiktokWindow) return;
let url = tiktokWindow.webContents.getURL();
if(!url.includes('qr')) {
return ;
}
let qrCodePath = process.resourcesPath;
await tiktokWindow.webContents.executeJavaScript(`(async () => {
console.log('pre-spaghet');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
await sleep(2000);
let src = document?.querySelector('img[alt=qrcode]')?.src || 'testin';
console.log(src || 'Spaghetti');
return src;
})()
`, true).then((qrData) => qrScan(qrData, qrCodePath)).then(
(qrCodeLocation) => {
// console.log('qrScan: ' + codeToRun);
let tempQrWindow = new BrowserWindow({
width: 164,
height: 164,
icon: './images/logo.png',
show: true,
});
WINDOWS.temp = tempQrWindow;
tempQrWindow.loadURL(qrCodeLocation);
tiktokWindow.webContents.on('page-title-updated', () => {
WINDOWS.temp?.close();
WINDOWS.temp = null;
});
// tiktokWindow.webContents.executeJavaScript(codeToRun);
}
);
})
await tiktokWindow.loadURL(properAuthURL);
// await tiktokWindow.webContents.session.cookies.set(cookieClear);
// await tiktokWindow.webContents.session.cookies.set(cookieClear2);
// await tiktokWindow.loadURL("https://www.tiktok.com/login");
// get data from qr code on screen
return tiktokWindow;
};
const createCamvasWindow = (cropData) => {
if(WINDOWS.camvas) {
console.log('redirecting to existing window');
WINDOWS.camvas.focus();
return;
}
let canvasWindow = new BrowserWindow({
width: 975,
height: 900,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js'),
},
icon: './images/logo.png',
show: false,
});
WINDOWS.camvas = canvasWindow;
canvasWindow.setResizable(false);
canvasWindow.loadFile(path.join(__dirname, 'canvas.html'));
canvasWindow.once('ready-to-show', async () => {
canvasWindow.show();
});
canvasWindow.on('closed', function () {
canvasWindow = null;
WINDOWS.camvas = null;
});
mainWindow.on('closed', () => {
//also close tiktokWindow
if (canvasWindow) {
canvasWindow.close();
canvasWindow = null;
WINDOWS.camvas = null;
}
});
canvasWindow.webContents.on('did-finish-load', function () {
canvasWindow.webContents.send('crop_data', cropData);
});
return canvasWindow;
};
const createScreenvasWindow = (camData) => {
if(WINDOWS.screenvas) {
console.log('redirecting to existing window');
WINDOWS.screenvas.focus();
return;
}
let canvasWindow = new BrowserWindow({
width: 1920 / 2,
height: 900,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js'),
},
icon: './images/logo.png',
show: false,
});
WINDOWS.screenvas = canvasWindow;
canvasWindow.setResizable(false);
canvasWindow.loadFile(path.join(__dirname, 'canvas.html'));
canvasWindow.once('ready-to-show', async () => {
canvasWindow.show();
});
canvasWindow.on('closed', function () {
canvasWindow = null;
WINDOWS.screenvas = null;
});
mainWindow.on('closed', () => {
//also close tiktokWindow
if (canvasWindow) {
canvasWindow.close();
canvasWindow = null;
WINDOWS.screenvas = null;
}
});
canvasWindow.webContents.on('did-finish-load', function () {
canvasWindow.webContents.send('crop_data', JSON.stringify(camData));
canvasWindow.webContents.send('screenvas_data', JSON.stringify(camData));
});
return canvasWindow;
};
app.on('ready', async () => {
//TODO: reenable this
if (!process.env.LETMEIN) {
Menu.setApplicationMenu(null);
}
// Must do this before window gets made so we
// have correct settings
// let appDir = app.getAppPath();
// if(!appDir.includes('system32')) {
// console.log("Settings folder: " + appDir);
// let result = await fetch(`http://localhost:42074/update?folder=${encodeURIComponent(appDir)}`);
// console.log(result);
// }
// else {
// console.log("Bad app dir");
// }
ipcMain.on('app_version', (event) => {
console.log('app version located: ' + app.getVersion());
event.sender.send('app_version', { version: app.getVersion() });
});
ipcMain.on('open-settings', (event) => {
console.log('open settings called, creating settings window');
createSettingsWindow();
});
ipcMain.on('open_clips', (event) => {
console.log('open clips called, creating clips window');
createClipsWindow();
});
ipcMain.on('close_clips', (event) => {
console.log('close clips called, close clips window');
WINDOWS.clips.close();
});
ipcMain.on('settings_updated', (event, data) => {
console.log('settings updated event');
WINDOWS.main.webContents.send('settings_updated', data);
});
ipcMain.on('youtube-auth-success', (event, data) => {
console.log('youtube auth successful');
WINDOWS.main.webContents.send('retry', data);
});
ipcMain.on('status_update', (event, data) => {
console.log('Status update from uploadClip');
// if(BrowserWindow.getFocusedWindow() == WINDOWS.clips) {
if(WINDOWS.clips) {
WINDOWS.clips.webContents.send('status_update', data);
}
// }
// else {
WINDOWS.main.webContents.send('status_update', data);
// }
});
ipcMain.on('camvas_closed', (event, data) => {
console.log('caught in main: ' + data);
console.log(util.inspect(WINDOWS));
WINDOWS.main.webContents.send('camvas_closed', data);
});
ipcMain.on('dashboard_open', (event, data) => {
console.log('dashboard open');
WINDOWS.main.loadFile(path.join(__dirname, 'index.html'));
});
ipcMain.on('screenvas_closed', (event, data) => {
console.log('caught in main: ' + data);
let parsedData = JSON.parse(data);
let callback = parsedData.callback;
console.log('found callback: ' + callback);
if (callback == 'custom_crop') {
WINDOWS.clips.webContents.send('custom_crop', data);
} else {
WINDOWS.main.webContents.send('screenvas_closed', data);
}
});
ipcMain.on('restart_app', () => {
console.log('restarting app');
autoUpdater.quitAndInstall();
});
ipcMain.on('just_restart', () => {
app.relaunch()
app.exit()
});
ipcMain.on('hotkey_changed', (event, data) => {
updateGlobalShortcut(data);
});
ipcMain.on('clear_cache', async (event, data) => {
console.log('clearing cache');
await clearCache();
console.log('cache cleared');
})
ipcMain.on('check_for_updates', () => {
console.log('checking for updates');
autoUpdater.checkForUpdatesAndNotify();
});
ipcMain.on('download_update', () => {
console.log('downloading update');
autoUpdater.downloadUpdate();
});
ipcMain.on('camvas_open', (event, cropData) => {
console.log('Opening camvas');
camvasWindow = createCamvasWindow(cropData);
});
ipcMain.on('screenvas_open', (event, camData) => {
console.log('Opening screenvas: ' + camData);
screenvasWindow = createScreenvasWindow(camData);
});
// Set tiktokwindow to open when a button is clicked in mainWindow
ipcMain.on('tiktok_open', async () => {
console.log('Opening tiktok');
let tiktokWindow = await createTikTokWindow();
console.log(
'TT cookies: ' +
JSON.stringify(tiktokWindow?.webContents?.session?.cookies)
);
// send cookies to main window session_token exists
// let tempQrWindow = new BrowserWindow({
// width: 164,
// height: 164,
// icon: './images/logo.png',
// show: true,
// });
// tempQrWindow.loadURL(path.join(__dirname, 'temp.png'));
let tiktokChecker = setInterval(() => {
console.log('Checking for TT Token');
tiktokWindow.webContents.session.cookies
.get({ name: 'clipbot_tiktok_sessionid' })
.then((cookies) => {
console.log('found anything: ' + cookies);
console.log(JSON.stringify(cookies));
console.log(tiktokWindow.webContents.session.cookies);
if (cookies?.length > 0 && cookies?.[0]?.value) {
clearInterval(tiktokChecker);
const initialCookieValue = cookies[0].value;
const properCookieValue = initialCookieValue.substring(4);
let sessionId = properCookieValue;
console.log('Sending TT Token:' + sessionId);
mainWindow.webContents.send('tiktok_login', sessionId);
console.log('Sent to main window: ' + sessionId);
setTimeout(() => {
tiktokWindow?.removeAllListeners();
tiktokWindow?.close();
if(WINDOWS.temp) {
WINDOWS.temp?.close();
WINDOWS.temp = null;
}
}, 3000);
}
})
.catch((error) => {
console.log('ERROR: ' + error);
});
}, 1000);
tiktokWindow.on('close', () => {
console.log('tiktokWindow is closed');
clearInterval(tiktokChecker);
if (mainWindow) {
mainWindow.webContents.send('tiktok_login', 'failed');
}
});
});
console.log('Making window');
return createWindow();
});
app.on('resize', function (e, x, y) {
mainWindow.setSize(x, y);
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
if (mainWindow === null) {
createWindow();
}
});
}