-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathapi.js
More file actions
576 lines (496 loc) · 15.1 KB
/
api.js
File metadata and controls
576 lines (496 loc) · 15.1 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
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
const apiCache = {};
function Api(app, requireName) {
this.app = app;
this.requireName = requireName;
}
Api.prototype.initialize = function () {
const self = this;
return self.load().then(self.addApiCommands.bind(self));
};
Api.prototype.addApiCommands = function (api) {
if (!this.nodeIntegration) return;
this.addRenderProcessApis(api.electron);
this.addMainProcessApis(api.electron.remote);
this.addBrowserWindowApis(api.browserWindow);
this.addWebContentsApis(api.webContents);
this.addProcessApis(api.rendererProcess);
this.api = {
browserWindow: api.browserWindow,
electron: api.electron,
rendererProcess: api.rendererProcess,
webContents: api.webContents
};
this.addClientProperties();
};
Api.prototype.load = function () {
const self = this;
return this.isNodeIntegrationEnabled().then(function (nodeIntegration) {
self.nodeIntegration = nodeIntegration;
if (!nodeIntegration) {
return {
electron: { remote: {} },
browserWindow: {},
webContents: {},
rendererProcess: {}
};
}
return self.getVersion().then(function (version) {
const api = apiCache[version];
if (api) return api;
return self.loadApi().then(function (api) {
if (version) apiCache[version] = api;
return api;
});
});
});
};
Api.prototype.isNodeIntegrationEnabled = function () {
const self = this;
return self.app.client.execute(function (requireName) {
return typeof window[requireName] === 'function';
}, self.requireName);
};
Api.prototype.getVersion = function () {
return this.app.client.execute(function (requireName) {
const process = window[requireName]('process');
if (process != null && process.versions != null) {
return process.versions.electron;
}
}, this.requireName);
};
Api.prototype.loadApi = function () {
return this.app.client.execute(function (requireName) {
if (typeof window[requireName] !== 'function') {
throw new Error(
'Could not find global require method with name: ' + requireName
);
}
const electron = window[requireName]('electron');
const process = window[requireName]('process');
const api = {
browserWindow: {},
electron: {},
rendererProcess: {},
webContents: {}
};
function ignoreModule(moduleName) {
switch (moduleName) {
case 'CallbacksRegistry':
case 'deprecate':
case 'deprecations':
case 'hideInternalModules':
case 'Tray':
return true;
case 'inAppPurchase':
return process.platform !== 'darwin';
}
return false;
}
function isRemoteFunction(name) {
switch (name) {
case 'BrowserWindow':
case 'Menu':
case 'MenuItem':
return false;
}
return typeof electron.remote[name] === 'function';
}
function ignoreApi(apiName) {
switch (apiName) {
case 'prototype':
return true;
default:
return apiName[0] === '_';
}
}
function addModule(parent, parentName, name, api) {
api[name] = {};
for (const key in parent[name]) {
if (ignoreApi(key)) continue;
api[name][key] = parentName + '.' + name + '.' + key;
}
}
function addRenderProcessModules() {
Object.getOwnPropertyNames(electron).forEach(function (key) {
if (ignoreModule(key)) return;
if (key === 'remote') return;
addModule(electron, 'electron', key, api.electron);
});
}
function addMainProcessModules() {
api.electron.remote = {};
if (typeof electron.remote !== 'object') return;
Object.getOwnPropertyNames(electron.remote).forEach(function (key) {
if (ignoreModule(key)) return;
if (isRemoteFunction(key)) {
api.electron.remote[key] = 'electron.remote.' + key;
} else {
addModule(
electron.remote,
'electron.remote',
key,
api.electron.remote
);
}
});
addModule(
electron.remote,
'electron.remote',
'process',
api.electron.remote
);
}
function addBrowserWindow() {
if (typeof electron.remote !== 'object') return;
const currentWindow = electron.remote.getCurrentWindow();
for (const name in currentWindow) {
if (ignoreApi(name)) continue;
const value = currentWindow[name];
if (typeof value === 'function') {
api.browserWindow[name] = 'browserWindow.' + name;
}
}
}
function addWebContents() {
if (typeof electron.remote !== 'object') return;
const webContents = electron.remote.getCurrentWebContents();
for (const name in webContents) {
if (ignoreApi(name)) continue;
const value = webContents[name];
if (typeof value === 'function') {
api.webContents[name] = 'webContents.' + name;
}
}
}
function addProcess() {
if (typeof process !== 'object') return;
for (const name in process) {
if (ignoreApi(name)) continue;
api.rendererProcess[name] = 'process.' + name;
}
}
addRenderProcessModules();
addMainProcessModules();
addBrowserWindow();
addWebContents();
addProcess();
return api;
}, this.requireName);
};
Api.prototype.addClientProperty = function (name) {
const self = this;
const clientPrototype = Object.getPrototypeOf(self.app.client);
Object.defineProperty(clientPrototype, name, {
get: function () {
const client = this;
return transformObject(self.api[name], {}, function (value) {
return client[value].bind(client);
});
}
});
};
Api.prototype.addClientProperties = function () {
this.addClientProperty('electron');
this.addClientProperty('browserWindow');
this.addClientProperty('webContents');
this.addClientProperty('rendererProcess');
Object.defineProperty(Object.getPrototypeOf(this.app.client), 'mainProcess', {
get: function () {
return this.electron.remote.process;
}
});
};
Api.prototype.addRenderProcessApis = function (api) {
const app = this.app;
const self = this;
const electron = {};
app.electron = electron;
Object.keys(api).forEach(function (moduleName) {
if (moduleName === 'remote') return;
electron[moduleName] = {};
const moduleApi = api[moduleName];
Object.keys(moduleApi).forEach(function (key) {
const commandName = moduleApi[key];
app.client.addCommand(commandName, function () {
const args = Array.prototype.slice.call(arguments);
return this.execute(
callRenderApi,
moduleName,
key,
args,
self.requireName
);
});
electron[moduleName][key] = function () {
return app.client[commandName].apply(app.client, arguments);
};
});
});
};
Api.prototype.addMainProcessApis = function (api) {
const app = this.app;
const self = this;
const remote = {};
app.electron.remote = remote;
Object.keys(api)
.filter(function (propertyName) {
return typeof api[propertyName] === 'string';
})
.forEach(function (name) {
const commandName = api[name];
app.client.addCommand(commandName, function () {
const args = Array.prototype.slice.call(arguments);
return this.execute(callMainApi, '', name, args, self.requireName);
});
remote[name] = function () {
return app.client[commandName].apply(app.client, arguments);
};
});
Object.keys(api)
.filter(function (moduleName) {
return typeof api[moduleName] === 'object';
})
.forEach(function (moduleName) {
remote[moduleName] = {};
const moduleApi = api[moduleName];
Object.keys(moduleApi).forEach(function (key) {
const commandName = moduleApi[key];
app.client.addCommand(commandName, function () {
const args = Array.prototype.slice.call(arguments);
return this.execute(
callMainApi,
moduleName,
key,
args,
self.requireName
);
});
remote[moduleName][key] = function () {
return app.client[commandName].apply(app.client, arguments);
};
});
});
};
Api.prototype.addBrowserWindowApis = function (api) {
const app = this.app;
const self = this;
app.browserWindow = {};
const asyncApis = {
'browserWindow.capturePage': true
};
Object.keys(api)
.filter(function (name) {
return !Object.prototype.hasOwnProperty.call(asyncApis, api[name]);
})
.forEach(function (name) {
const commandName = api[name];
app.client.addCommand(commandName, function () {
const args = Array.prototype.slice.call(arguments);
return this.execute(callBrowserWindowApi, name, args, self.requireName);
});
app.browserWindow[name] = function () {
return app.client[commandName].apply(app.client, arguments);
};
});
this.addCapturePageSupport();
};
Api.prototype.addCapturePageSupport = function () {
const app = this.app;
const self = this;
app.client.addCommand('browserWindow.capturePage', function (rect) {
return this.executeAsync(
async function (rect, requireName, done) {
const args = [];
if (rect != null) args.push(rect);
const browserWindow = window[requireName](
'electron'
).remote.getCurrentWindow();
const image = await browserWindow.capturePage.apply(
browserWindow,
args
);
if (image != null) {
done(image.toPNG().toString('base64'));
} else {
done(image);
}
},
rect,
self.requireName
).then(function (image) {
return Buffer.from(image, 'base64');
});
});
app.browserWindow.capturePage = function () {
return app.client['browserWindow.capturePage'].apply(app.client, arguments);
};
};
Api.prototype.addWebContentsApis = function (api) {
const app = this.app;
const self = this;
app.webContents = {};
const asyncApis = {
'webContents.savePage': true,
'webContents.executeJavaScript': true
};
Object.keys(api)
.filter(function (name) {
return !Object.prototype.hasOwnProperty.call(asyncApis, api[name]);
})
.forEach(function (name) {
const commandName = api[name];
app.client.addCommand(commandName, function () {
const args = Array.prototype.slice.call(arguments);
return this.execute(callWebContentsApi, name, args, self.requireName);
});
app.webContents[name] = function () {
return app.client[commandName].apply(app.client, arguments);
};
});
this.addSavePageSupport();
this.addExecuteJavaScriptSupport();
};
Api.prototype.addSavePageSupport = function () {
const app = this.app;
const self = this;
app.client.addCommand('webContents.savePage', function (fullPath, saveType) {
return this.executeAsync(
async function (fullPath, saveType, requireName, done) {
const webContents = window[requireName](
'electron'
).remote.getCurrentWebContents();
await webContents.savePage(fullPath, saveType);
done();
},
fullPath,
saveType,
self.requireName
).then(function (rawError) {
if (rawError) {
const error = new Error(rawError.message);
if (rawError.name) error.name = rawError.name;
throw error;
}
});
});
app.webContents.savePage = function () {
return app.client['webContents.savePage'].apply(app.client, arguments);
};
};
Api.prototype.addExecuteJavaScriptSupport = function () {
const app = this.app;
const self = this;
app.client.addCommand('webContents.executeJavaScript', function (
code,
useGesture
) {
return this.executeAsync(
async function (code, useGesture, requireName, done) {
const webContents = window[requireName](
'electron'
).remote.getCurrentWebContents();
const result = await webContents.executeJavaScript(code, useGesture);
done(result);
},
code,
useGesture,
self.requireName
);
});
app.webContents.executeJavaScript = function () {
return app.client['webContents.executeJavaScript'].apply(
app.client,
arguments
);
};
};
Api.prototype.addProcessApis = function (api) {
const app = this.app;
app.rendererProcess = {};
Object.keys(api).forEach(function (name) {
const commandName = api[name];
app.client.addCommand(commandName, function () {
const args = Array.prototype.slice.call(arguments);
return this.execute(callProcessApi, name, args);
});
app.rendererProcess[name] = function () {
return app.client[commandName].apply(app.client, arguments);
};
});
app.mainProcess = app.electron.remote.process;
};
Api.prototype.transferPromiseness = function (target, promise) {
if (!this.nodeIntegration) return;
const addProperties = function (source, target, moduleName) {
const sourceModule = source[moduleName];
if (!sourceModule) return;
target[moduleName] = transformObject(sourceModule, {}, function (
value,
parent
) {
return value.bind(parent);
});
};
addProperties(promise, target, 'webContents');
addProperties(promise, target, 'browserWindow');
addProperties(promise, target, 'electron');
addProperties(promise, target, 'mainProcess');
addProperties(promise, target, 'rendererProcess');
};
Api.prototype.logApi = function () {
const fs = require('fs');
const path = require('path');
const json = JSON.stringify(this.api, null, 2);
fs.writeFileSync(path.join(__dirname, 'api.json'), json);
};
function transformObject(input, output, callback) {
Object.keys(input).forEach(function (name) {
const value = input[name];
if (typeof value === 'object') {
output[name] = {};
transformObject(value, output[name], callback);
} else {
output[name] = callback(value, input);
}
});
return output;
}
function callRenderApi(moduleName, api, args, requireName) {
const module = window[requireName]('electron')[moduleName];
if (typeof module[api] === 'function') {
return module[api].apply(module, args);
} else {
return module[api];
}
}
function callMainApi(moduleName, api, args, requireName) {
let module = window[requireName]('electron').remote;
if (moduleName) {
module = module[moduleName];
}
if (typeof module[api] === 'function') {
return module[api].apply(module, args);
} else {
return module[api];
}
}
function callWebContentsApi(name, args, requireName) {
const webContents = window[requireName](
'electron'
).remote.getCurrentWebContents();
return webContents[name].apply(webContents, args);
}
function callBrowserWindowApi(name, args, requireName) {
const browserWindow = window[requireName](
'electron'
).remote.getCurrentWindow();
return browserWindow[name].apply(browserWindow, args);
}
function callProcessApi(name, args) {
if (typeof process[name] === 'function') {
return process[name].apply(process, args);
} else {
return process[name];
}
}
module.exports = Api;