This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.js
684 lines (638 loc) · 30.6 KB
/
index.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
// @ts-check
'use strict'
const electron = require('electron')
const {app, BrowserWindow, ipcMain, Menu, MenuItem, Tray, dialog, Notification, globalShortcut} = electron
const consts = require('./src/consts.js')
const client = require('./src/client.js')
const readline = require('readline')
let rl;
let callbacks = {};
let counters = {};
let elements = {};
let windowOptions = {};
let menus = {};
let quittingApp = false;
// Single instance
let lastWindowId = null;
// App is quitting
const beforeQuit = () => {
quittingApp = true;
client.write(consts.targetIds.app,consts.eventNames.appCmdQuit);
};
// App is ready
function onReady () {
// Init
const screen = electron.screen
Menu.setApplicationMenu(null)
// Listen to screen events
screen.on('display-added', function() {
client.write(consts.targetIds.app, consts.eventNames.displayEventAdded, {displays: {all: screen.getAllDisplays(), primary: screen.getPrimaryDisplay()}})
})
screen.on('display-metrics-changed', function() {
client.write(consts.targetIds.app, consts.eventNames.displayEventMetricsChanged, {displays: {all: screen.getAllDisplays(), primary: screen.getPrimaryDisplay()}})
})
screen.on('display-removed', function() {
client.write(consts.targetIds.app, consts.eventNames.displayEventRemoved, {displays: {all: screen.getAllDisplays(), primary: screen.getPrimaryDisplay()}})
})
const powerMonitor = electron.powerMonitor
// Listen to power events
powerMonitor.on('suspend', function() {
client.write(consts.targetIds.app, consts.eventNames.powerEventSuspend)
})
powerMonitor.on('resume', function() {
client.write(consts.targetIds.app, consts.eventNames.powerEventResume)
})
powerMonitor.on('on-ac', function() {
client.write(consts.targetIds.app, consts.eventNames.powerEventOnAC)
})
powerMonitor.on('on-battery', function () {
client.write(consts.targetIds.app, consts.eventNames.powerEventOnBattery)
})
powerMonitor.on('shutdown', function() {
client.write(consts.targetIds.app, consts.eventNames.powerEventShutdown)
})
powerMonitor.on('lock-screen', function() {
client.write(consts.targetIds.app, consts.eventNames.powerEventLockScreen)
})
powerMonitor.on('unlock-screen', function() {
client.write(consts.targetIds.app, consts.eventNames.powerEventUnlockScreen)
})
powerMonitor.on('user-did-become-active', function() {
client.write(consts.targetIds.app, consts.eventNames.powerEventUserDidBecomeActive)
})
powerMonitor.on('user-did-resign-active', function() {
client.write(consts.targetIds.app, consts.eventNames.powerEventUserDidResignActive)
})
// Listen on main ipcMain
ipcMain.on(consts.eventNames.ipcEventMessage, (event, arg) => {
let payload = {message: arg.message};
if (typeof arg.callbackId !== "undefined") payload.callbackId = arg.callbackId;
client.write(arg.targetID, consts.eventNames.windowEventMessage, payload)
});
ipcMain.on(consts.eventNames.ipcEventMessageCallback, (event, arg) => {
let payload = {message: arg.message};
if (typeof arg.callbackId !== "undefined") payload.callbackId = arg.callbackId;
client.write(arg.targetID, consts.eventNames.windowEventMessageCallback, payload)
});
// Read from client
rl.on('line', function(line){
// Parse the JSON
let json = JSON.parse(line)
// Switch on event name
let window;
switch (json.name) {
// App
case consts.eventNames.appCmdQuit:
app.quit();
break;
// Dock
case consts.eventNames.dockCmdBounce:
let id = 0;
if (typeof app.dock !== "undefined") {
id = app.dock.bounce(json.bounceType);
}
client.write(consts.targetIds.dock, consts.eventNames.dockEventBouncing, {id: id});
break;
case consts.eventNames.dockCmdBounceDownloads:
if (typeof app.dock !== "undefined") {
app.dock.downloadFinished(json.filePath);
}
client.write(consts.targetIds.dock, consts.eventNames.dockEventDownloadsBouncing);
break;
case consts.eventNames.dockCmdCancelBounce:
if (typeof app.dock !== "undefined") {
app.dock.cancelBounce(json.id);
}
client.write(consts.targetIds.dock, consts.eventNames.dockEventBouncingCancelled);
break;
case consts.eventNames.dockCmdHide:
if (typeof app.dock !== "undefined") {
app.dock.hide();
}
client.write(consts.targetIds.dock, consts.eventNames.dockEventHidden);
break;
case consts.eventNames.dockCmdSetBadge:
if (typeof app.dock !== "undefined") {
app.dock.setBadge(json.badge);
}
client.write(consts.targetIds.dock, consts.eventNames.dockEventBadgeSet);
break;
case consts.eventNames.dockCmdSetIcon:
if (typeof app.dock !== "undefined") {
app.dock.setIcon(json.image);
}
client.write(consts.targetIds.dock, consts.eventNames.dockEventIconSet);
break;
case consts.eventNames.dockCmdShow:
if (typeof app.dock !== "undefined") {
app.dock.show();
}
client.write(consts.targetIds.dock, consts.eventNames.dockEventShown);
break;
// Menu
case consts.eventNames.menuCmdCreate:
menuCreate(json.menu)
menus[json.menu.rootId] = json.targetID
setMenu(json.menu.rootId)
client.write(json.targetID, consts.eventNames.menuEventCreated)
break;
case consts.eventNames.menuCmdDestroy:
elements[json.targetID] = null
if (menus[json.menu.rootId] === json.targetID) {
menus[json.menu.rootId] = null
setMenu(json.menu.rootId)
}
client.write(json.targetID, consts.eventNames.menuEventDestroyed)
break;
// Menu item
case consts.eventNames.menuItemCmdSetChecked:
elements[json.targetID].checked = json.menuItemOptions.checked
client.write(json.targetID, consts.eventNames.menuItemEventCheckedSet)
break;
case consts.eventNames.menuItemCmdSetEnabled:
elements[json.targetID].enabled = json.menuItemOptions.enabled
client.write(json.targetID, consts.eventNames.menuItemEventEnabledSet)
break;
case consts.eventNames.menuItemCmdSetLabel:
elements[json.targetID].label = json.menuItemOptions.label
client.write(json.targetID, consts.eventNames.menuItemEventLabelSet)
break;
case consts.eventNames.menuItemCmdSetVisible:
elements[json.targetID].visible = json.menuItemOptions.visible
client.write(json.targetID, consts.eventNames.menuItemEventVisibleSet)
break;
// Notification
case consts.eventNames.notificationCmdCreate:
notificationCreate(json);
break;
case consts.eventNames.notificationCmdShow:
if (Notification.isSupported()) {
elements[json.targetID].show();
}
break;
// Session
case consts.eventNames.sessionCmdClearCache:
elements[json.targetID].clearCache().then(() => {
client.write(json.targetID, consts.eventNames.sessionEventClearedCache)
})
break;
case consts.eventNames.sessionCmdFlushStorage:
elements[json.targetID].flushStorageData();
client.write(json.targetID, consts.eventNames.sessionEventFlushedStorage)
break;
case consts.eventNames.sessionCmdLoadExtension:
elements[json.targetID].loadExtension(json.path).then(() => {
client.write(json.targetID, consts.eventNames.sessionEventLoadedExtension)
})
break;
// Sub menu
case consts.eventNames.subMenuCmdAppend:
elements[json.targetID].append(menuItemCreate(json.menuItem))
setMenu(json.menuItem.rootId)
client.write(json.targetID, consts.eventNames.subMenuEventAppended)
break;
case consts.eventNames.subMenuCmdClosePopup:
window = null
if (typeof json.windowId !== "undefined") {
window = elements[json.windowId]
}
elements[json.targetID].closePopup(window)
client.write(json.targetID, consts.eventNames.subMenuEventClosedPopup)
break;
case consts.eventNames.subMenuCmdInsert:
elements[json.targetID].insert(json.menuItemPosition, menuItemCreate(json.menuItem))
setMenu(json.menuItem.rootId)
client.write(json.targetID, consts.eventNames.subMenuEventInserted)
break;
case consts.eventNames.subMenuCmdPopup:
window = null
if (typeof json.windowId !== "undefined") {
window = elements[json.windowId]
}
json.menuPopupOptions.async = true
elements[json.targetID].popup(window, json.menuPopupOptions)
client.write(json.targetID, consts.eventNames.subMenuEventPoppedUp)
break;
// Tray
case consts.eventNames.trayCmdCreate:
trayCreate(json)
break;
case consts.eventNames.trayCmdDestroy:
elements[json.targetID].destroy()
elements[json.targetID] = null
client.write(json.targetID, consts.eventNames.trayEventDestroyed)
break;
case consts.eventNames.trayCmdSetImage:
elements[json.targetID].setImage(json.image);
client.write(json.targetID, consts.eventNames.trayEventImageSet)
break;
case consts.eventNames.trayCmdPopupContextMenu:
trayPopUpContextMenu(json);
client.write(json.targetID, consts.eventNames.trayEventPoppedUpContextMenu);
break;
// Web contents
case consts.eventNames.webContentsEventLoginCallback:
executeCallback(consts.callbackNames.webContentsLogin, json, [json.username, json.password]);
break;
// Window
case consts.eventNames.windowCmdBlur:
elements[json.targetID].blur()
break;
case consts.eventNames.windowCmdCenter:
elements[json.targetID].center()
break;
case consts.eventNames.windowCmdClose:
elements[json.targetID].close()
break;
case consts.eventNames.windowCmdCreate:
windowCreate(json)
break;
case consts.eventNames.windowCmdDestroy:
elements[json.targetID].destroy()
elements[json.targetID] = null
break;
case consts.eventNames.windowCmdFocus:
elements[json.targetID].focus()
break;
case consts.eventNames.windowCmdHide:
elements[json.targetID].hide()
break;
case consts.eventNames.windowCmdLog:
elements[json.targetID].webContents.send(consts.eventNames.ipcCmdLog, json.message)
break;
case consts.eventNames.windowCmdMaximize:
elements[json.targetID].maximize()
break;
case consts.eventNames.windowCmdMessage:
case consts.eventNames.windowCmdMessageCallback:
let m = {message: json.message}
if (typeof json.callbackId !== "undefined") m.callbackId = json.callbackId
const targetElement = elements[json.targetID]
if (targetElement) targetElement.webContents.send(json.name === consts.eventNames.windowCmdMessageCallback ? consts.eventNames.ipcCmdMessageCallback : consts.eventNames.ipcCmdMessage, m)
break;
case consts.eventNames.windowCmdMinimize:
elements[json.targetID].minimize()
break;
case consts.eventNames.windowCmdMove:
elements[json.targetID].setPosition(json.windowOptions.x, json.windowOptions.y, true)
break;
case consts.eventNames.windowCmdMoveTop:
elements[json.targetID].moveTop()
client.write(json.targetID, consts.eventNames.windowEventMovedTop)
break;
case consts.eventNames.windowCmdResize:
elements[json.targetID].setSize(json.windowOptions.width, json.windowOptions.height, true)
break;
case consts.eventNames.windowCmdResizeContent:
elements[json.targetID].setContentSize(json.windowOptions.width, json.windowOptions.height, true)
break;
case consts.eventNames.windowCmdSetAlwaysOnTop:
elements[json.targetID].setAlwaysOnTop(json.enable ? json.enable : false)
client.write(json.targetID, consts.eventNames.windowEventAlwaysOnTopChanged)
break;
case consts.eventNames.windowCmdSetBounds:
elements[json.targetID].setBounds(json.bounds, true);
break;
case consts.eventNames.windowCmdSetFullScreen:
elements[json.targetID].setFullScreen(json.enable ? json.enable : false)
break;
case consts.eventNames.windowCmdRestore:
elements[json.targetID].restore()
break;
case consts.eventNames.windowCmdShow:
elements[json.targetID].show()
break;
case consts.eventNames.windowCmdSetContentProtection:
elements[json.targetID].setContentProtection(json.enable ? json.enable : false)
client.write(json.targetID, consts.eventNames.windowEventContentProtectionSet)
break;
case consts.eventNames.windowCmdWebContentsCloseDevTools:
elements[json.targetID].webContents.closeDevTools()
break;
case consts.eventNames.windowCmdWebContentsOpenDevTools:
elements[json.targetID].webContents.openDevTools()
break;
case consts.eventNames.windowCmdUnmaximize:
elements[json.targetID].unmaximize()
break;
case consts.eventNames.windowCmdUpdateCustomOptions:
windowOptions[json.targetID] = json.windowOptions
client.write(json.targetID, consts.eventNames.windowEventUpdatedCustomOptions, json.windowOptions)
break;
case consts.eventNames.windowCmdWebContentsExecuteJavascript:
elements[json.targetID].webContents.executeJavaScript(json.code).then(() => client.write(json.targetID, consts.eventNames.windowEventWebContentsExecutedJavaScript));
break;
// Global Shortcut
case consts.eventNames.globalShortcutsCmdRegister:
const isRegistered = globalShortcut.register(json.globalShortcuts.accelerator, () => {
client.write(json.targetID, consts.eventNames.globalShortcutsEventTriggered, {globalShortcuts:{accelerator: json.globalShortcuts.accelerator}});
});
client.write(json.targetID, consts.eventNames.globalShortcutsEventRegistered, {globalShortcuts:{isRegistered: isRegistered}});
break;
case consts.eventNames.globalShortcutsCmdIsRegistered:
client.write(json.targetID, consts.eventNames.globalShortcutsEventIsRegistered, {globalShortcuts:{isRegistered: globalShortcut.isRegistered(json.globalShortcuts.accelerator)}});
break;
case consts.eventNames.globalShortcutsCmdUnregister:
globalShortcut.unregister(json.globalShortcuts.accelerator);
client.write(json.targetID, consts.eventNames.globalShortcutsEventUnregistered);
break
case consts.eventNames.globalShortcutsCmdUnregisterAll:
globalShortcut.unregisterAll();
client.write(json.targetID, consts.eventNames.globalShortcutsEventUnregisteredAll);
break
}
});
// Send electron.ready event
client.write(consts.targetIds.app, consts.eventNames.appEventReady, {
displays: {
all: screen.getAllDisplays(),
primary: screen.getPrimaryDisplay()
},
supported: {
notification: Notification.isSupported()
}
})
};
// start begins listening to go-astilectron.
function start(address = process.argv[2]) {
client.init(address);
rl = readline.createInterface({ input: client.socket });
app.on("before-quit", beforeQuit);
if (app.isReady()) {
onReady();
} else {
app.on("ready", onReady);
}
app.on("window-all-closed", app.quit);
}
// menuCreate creates a new menu
function menuCreate(menu) {
if (typeof menu !== "undefined") {
elements[menu.id] = new Menu()
for(let i = 0; i < menu.items.length; i++) {
elements[menu.id].append(menuItemCreate(menu.items[i]))
}
return elements[menu.id]
}
return null
}
// menuItemCreate creates a menu item
function menuItemCreate(menuItem) {
const itemId = menuItem.id
menuItem.options.click = function(menuItem) {
client.write(itemId, consts.eventNames.menuItemEventClicked, {menuItemOptions: menuItemToJSON(menuItem)})
}
if (typeof menuItem.submenu !== "undefined") {
menuItem.options.type = 'submenu'
menuItem.options.submenu = menuCreate(menuItem.submenu)
}
elements[itemId] = new MenuItem(menuItem.options)
return elements[itemId]
}
// menuItemToJSON returns the proper fields not to raise an exception
function menuItemToJSON(menuItem) {
return {
checked: menuItem.checked,
enabled: menuItem.enabled,
label: menuItem.label,
visible: menuItem.visible,
}
}
// setMenu sets a menu
function setMenu(rootId) {
let menu = null
if (typeof menus[rootId] !== "undefined" && typeof elements[menus[rootId]] !== "undefined") {
menu = elements[menus[rootId]]
}
if (rootId === consts.targetIds.app) {
Menu.setApplicationMenu(menu)
} else if (rootId === consts.targetIds.dock && typeof app.dock !== "undefined") {
app.dock.setMenu(menu)
} else if (elements[rootId].constructor === Tray) {
elements[rootId].setContextMenu(menu);
} else {
elements[rootId].setMenu(menu);
}
}
// notificationCreate creates a notification
function notificationCreate(json) {
if (Notification.isSupported()) {
elements[json.targetID] = new Notification(json.notificationOptions);
elements[json.targetID].on('action', (event, index) => { client.write(json.targetID, consts.eventNames.notificationEventActioned, {index: index}) })
elements[json.targetID].on('click', () => { client.write(json.targetID, consts.eventNames.notificationEventClicked) })
elements[json.targetID].on('close', () => { client.write(json.targetID, consts.eventNames.notificationEventClosed) })
elements[json.targetID].on('reply', (event, reply) => { client.write(json.targetID, consts.eventNames.notificationEventReplied, {reply: reply}) })
elements[json.targetID].on('show', () => { client.write(json.targetID, consts.eventNames.notificationEventShown) })
}
client.write(json.targetID, consts.eventNames.notificationEventCreated)
}
// trayCreate creates a tray
function trayCreate(json) {
elements[json.targetID] = new Tray(json.trayOptions.image);
if (typeof json.trayOptions.tooltip !== "undefined") {
elements[json.targetID].setToolTip(json.trayOptions.tooltip);
}
elements[json.targetID].on('click', (index, event) => { client.write(json.targetID, consts.eventNames.trayEventClicked, {"bounds":{x:event.x, y:event.y,width:event.width,height:event.height}})})
elements[json.targetID].on('double-click', (index, event) => { client.write(json.targetID, consts.eventNames.trayEventDoubleClicked, {"bounds":{x:event.x, y:event.y,width:event.width,height:event.height}})})
elements[json.targetID].on('right-click', (index, event) => { client.write(json.targetID, consts.eventNames.trayEventRightClicked, {"bounds":{x:event.x, y:event.y,width:event.width,height:event.height}})})
client.write(json.targetID, consts.eventNames.trayEventCreated)
}
// trayPopUpContextMenu pops up the context menu of the tray
function trayPopUpContextMenu(json) {
let menu = menuCreate(json.menu);
let position = json.menuPopupOptions;
if (!menu && !position) elements[json.targetID].popUpContextMenu();
else if (menu && !position) elements[json.targetID].popUpContextMenu(menu);
else if (!menu && position) elements[json.targetID].popUpContextMenu(position);
else elements[json.targetID].popUpContextMenu(menu, position);
}
// windowCreate creates a new window
function windowCreate(json) {
if (!json.windowOptions.webPreferences) {
json.windowOptions.webPreferences = {}
}
json.windowOptions.webPreferences.contextIsolation = false
json.windowOptions.webPreferences.nodeIntegration = true
elements[json.targetID] = new BrowserWindow(json.windowOptions)
windowOptions[json.targetID] = json.windowOptions
if (typeof json.windowOptions.proxy !== "undefined") {
elements[json.targetID].webContents.session.setProxy(json.windowOptions.proxy)
.then(() => windowCreateFinish(json))
} else {
windowCreateFinish(json)
}
}
// windowCreateFinish finishes creating a new window
function windowCreateFinish(json) {
elements[json.targetID].setMenu(null)
elements[json.targetID].loadURL(json.url, (typeof json.windowOptions.load !== "undefined" ? json.windowOptions.load : {}));
elements[json.targetID].on('blur', () => { client.write(json.targetID, consts.eventNames.windowEventBlur) })
elements[json.targetID].on('close', (e) => {
if (typeof windowOptions[json.targetID] !== "undefined" && typeof windowOptions[json.targetID].custom !== "undefined") {
if (typeof windowOptions[json.targetID].custom.messageBoxOnClose !== "undefined") {
let buttonId = dialog.showMessageBoxSync(null, windowOptions[json.targetID].custom.messageBoxOnClose)
if (typeof windowOptions[json.targetID].custom.messageBoxOnClose.confirmId !== "undefined" && windowOptions[json.targetID].custom.messageBoxOnClose.confirmId !== buttonId) {
e.preventDefault()
return
}
}
if (!quittingApp) {
if (windowOptions[json.targetID].custom.minimizeOnClose) {
e.preventDefault();
elements[json.targetID].minimize();
} else if (windowOptions[json.targetID].custom.hideOnClose) {
e.preventDefault();
elements[json.targetID].hide();
}
}
}
})
elements[json.targetID].on('closed', () => {
client.write(json.targetID, consts.eventNames.windowEventClosed)
delete elements[json.targetID]
})
elements[json.targetID].on('enter-full-screen', () => { client.write(json.targetID, consts.eventNames.windowEventEnterFullScreen, {windowOptions: {fullscreen: true}})} )
elements[json.targetID].on('focus', () => { client.write(json.targetID, consts.eventNames.windowEventFocus) })
elements[json.targetID].on('hide', () => { client.write(json.targetID, consts.eventNames.windowEventHide) })
elements[json.targetID].on('leave-full-screen', () => { client.write(json.targetID, consts.eventNames.windowEventLeaveFullScreen, {windowOptions: {fullscreen: false}})} )
elements[json.targetID].on('maximize', () => {
let bounds = elements[json.targetID].getBounds();
client.write(json.targetID, consts.eventNames.windowEventMaximize, {bounds: bounds});
})
elements[json.targetID].on('minimize', () => { client.write(json.targetID, consts.eventNames.windowEventMinimize) })
elements[json.targetID].on('move', () => {
let bounds = elements[json.targetID].getBounds();
client.write(json.targetID, consts.eventNames.windowEventMove, {bounds: bounds});
})
elements[json.targetID].on('moved', () => {
let bounds = elements[json.targetID].getBounds();
client.write(json.targetID, consts.eventNames.windowEventMoved, {bounds: bounds});
})
elements[json.targetID].on('ready-to-show', () => { client.write(json.targetID, consts.eventNames.windowEventReadyToShow) })
elements[json.targetID].on('resize', () => {
let bounds = elements[json.targetID].getBounds();
client.write(json.targetID, consts.eventNames.windowEventResize, {bounds: bounds});
})
elements[json.targetID].on('resize-content', () => {
let bounds = elements[json.targetID].getBounds();
client.write(json.targetID, consts.eventNames.windowEventResizeContent, {bounds: bounds})
})
elements[json.targetID].on('restore', () => { client.write(json.targetID, consts.eventNames.windowEventRestore) })
elements[json.targetID].on('show', () => { client.write(json.targetID, consts.eventNames.windowEventShow) })
elements[json.targetID].on('unmaximize', () => {
let bounds = elements[json.targetID].getBounds();
client.write(json.targetID, consts.eventNames.windowEventUnmaximize, {bounds: bounds});
})
elements[json.targetID].on('unresponsive', () => { client.write(json.targetID, consts.eventNames.windowEventUnresponsive) })
elements[json.targetID].on('will-move', () => {
let bounds = elements[json.targetID].getBounds();
client.write(json.targetID, consts.eventNames.windowEventWillMove, {bounds: bounds});
})
elements[json.targetID].webContents.on('did-finish-load', () => {
elements[json.targetID].webContents.executeJavaScript(
`const {ipcRenderer} = require('electron')
var astilectron = {
onMessageOnce: false,
onMessage: function(callback) {
if (astilectron.onMessageOnce) {
return
}
ipcRenderer.on('`+ consts.eventNames.ipcCmdMessage +`', function(event, message) {
let v = callback(message.message)
if (typeof message.callbackId !== "undefined") {
let e = {callbackId: message.callbackId, targetID: '`+ json.targetID +`'}
if (typeof v !== "undefined") e.message = v
ipcRenderer.send('`+ consts.eventNames.ipcEventMessageCallback +`', e)
}
})
astilectron.onMessageOnce = true
},
callbacks: {},
counters: {},
registerCallback: function(k, e, c, n) {
e.targetID = '`+ json.targetID +`';
if (typeof c !== "undefined") {
if (typeof astilectron.counters[k] === "undefined") {
astilectron.counters[k] = 1;
}
e.callbackId = String(astilectron.counters[k]++);
if (typeof astilectron.callbacks[k] === "undefined") {
astilectron.callbacks[k] = {};
}
astilectron.callbacks[k][e.callbackId] = c;
}
ipcRenderer.send(n, e);
},
executeCallback: function(k, message, args) {
if (typeof astilectron.callbacks[k][message.callbackId] !== "undefined") {
astilectron.callbacks[k][message.callbackId].apply(null, args);
}
},
sendMessage: function(message, callback) {
astilectron.registerCallback('` + consts.callbackNames.webContentsMessage + `', {message: message}, callback, '`+ consts.eventNames.ipcEventMessage +`');
}
};
ipcRenderer.on('`+ consts.eventNames.ipcCmdMessageCallback +`', function(event, message) {
astilectron.executeCallback('` + consts.callbackNames.webContentsMessage + `', message, [message.message]);
});
ipcRenderer.on('`+ consts.eventNames.ipcCmdLog+`', function(event, message) {
console.log(message)
});
` + (typeof json.windowOptions.custom !== "undefined" && typeof json.windowOptions.custom.script !== "undefined" ? json.windowOptions.custom.script : "") + `
document.dispatchEvent(new Event('astilectron-ready'))`
)
sessionCreate(elements[json.targetID].webContents, json.sessionId)
let bounds = elements[json.targetID].getBounds();
client.write(json.targetID, consts.eventNames.windowEventDidFinishLoad, {bounds: bounds})
})
elements[json.targetID].webContents.on('did-get-redirect-request', (event, oldUrl, newUrl) => {
client.write(json.targetID, consts.eventNames.windowEventDidGetRedirectRequest, {
newUrl: newUrl,
oldUrl: oldUrl
})
})
elements[json.targetID].webContents.on('login', (event, request, authInfo, callback) => {
event.preventDefault();
registerCallback(json, consts.callbackNames.webContentsLogin, {authInfo: authInfo, request: request}, consts.eventNames.webContentsEventLogin, callback);
})
elements[json.targetID].webContents.on('will-navigate', (event, url) => {
client.write(json.targetID, consts.eventNames.windowEventWillNavigate, {
url: url
})
})
if (typeof json.windowOptions.appDetails !== "undefined" && process.platform === "win32"){
elements[json.targetID].setThumbarButtons([]);
elements[json.targetID].setAppDetails(json.windowOptions.appDetails);
}
lastWindowId = json.targetID
}
function registerCallback(json, k, e, n, c) {
if (typeof counters[k] === "undefined") {
counters[k] = 1;
}
e.callbackId = String(counters[k]++);
if (typeof callbacks[k] === "undefined") {
callbacks[k] = {};
}
callbacks[k][e.callbackId] = c;
client.write(json.targetID, n, e);
}
function executeCallback(k, json, args) {
if (typeof callbacks[k][json.callbackId] !== "undefined") {
callbacks[k][json.callbackId].apply(null, args);
}
}
function sessionCreate(webContents, sessionId) {
elements[sessionId] = webContents.session
elements[sessionId].on('will-download', () => { client.write(sessionId, consts.eventNames.sessionEventWillDownload) })
}
function getLastWindow() {
if (elements[lastWindowId]) return elements[lastWindowId]
return null
}
module.exports = {
getLastWindow,
start,
client,
consts
}