-
Notifications
You must be signed in to change notification settings - Fork 30
/
electron-mocks.js
147 lines (102 loc) · 3.78 KB
/
electron-mocks.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
import { Client } from '../ipc';
import resolveFileUrl from '../utils/resolve-file-url';
import CONSTANTS from '../constants';
const URL_QUERY_RE = /\?.*$/;
const NAVIGATION_EVENTS = ['will-navigate', 'did-navigate'];
var ipc = null;
var loadingTimeout = null;
var openedUrls = [];
var contextMenuHandler = { menu: null };
var windowHandler = { window: null };
var dialogHandler = {
fn: null,
handledDialog: false,
hadUnexpectedDialogs: false,
hadNoExpectedDialogs: false
};
function startLoadingTimeout () {
if (loadingTimeout)
return;
loadingTimeout = setTimeout(() => {
ipc.sendInjectingStatus({ completed: false, openedUrls });
}, CONSTANTS.loadingTimeout);
}
function stopLoadingTimeout () {
clearTimeout(loadingTimeout);
loadingTimeout = 0;
}
function handleDialog (type, args) {
if (!dialogHandler.fn) {
dialogHandler.hadUnexpectedDialogs = true;
return void 0;
}
dialogHandler.handledDialog = true;
var handlerFunction = dialogHandler.fn;
var handlerResult = handlerFunction(type, ...args);
var lastArg = args.length ? args[args.length - 1] : null;
if (typeof lastArg === 'function')
lastArg(handlerResult);
return handlerResult;
}
module.exports = function install (config, testPageUrl) {
ipc = new Client(config, { dialogHandler, contextMenuHandler, windowHandler });
ipc.connect();
var { Menu, dialog, app } = require('electron');
var WebContents;
if ( process.atomBinding ) {
// NOTE: < Electron 6
WebContents = process.atomBinding('web_contents').WebContents;
}
else {
// NOTE: >= Electron 6
WebContents = process.electronBinding('web_contents').WebContents;
}
var origLoadURL = WebContents.prototype.loadURL;
var origGetAppPath = app.getAppPath;
function stripQuery (url) {
return url.replace(URL_QUERY_RE, '');
}
function isFileProtocol (url) {
return url.indexOf('file:') === 0;
}
WebContents.prototype.loadURL = function (url, options) {
startLoadingTimeout(config.mainWindowUrl);
let testUrl = stripQuery(url);
if (isFileProtocol(url))
testUrl = resolveFileUrl(config.appEntryPoint, testUrl);
openedUrls.push(testUrl);
if (testUrl.toLowerCase() === config.mainWindowUrl.toLowerCase()) {
stopLoadingTimeout();
ipc.sendInjectingStatus({ completed: true });
WebContents.prototype.loadURL = origLoadURL;
url = testPageUrl;
windowHandler.window = this;
if (config.openDevTools)
this.openDevTools();
}
return origLoadURL.call(this, url, options);
};
app.getAppPath = function () {
return config.appPath || origGetAppPath.call(this);
};
Menu.prototype.popup = function () {
contextMenuHandler.menu = this;
};
Menu.prototype.closePopup = function () {
contextMenuHandler.menu = null;
};
if (!config.enableNavigateEvents) {
var origOn = WebContents.prototype.on;
WebContents.prototype.on = function (event, listener) {
if (NAVIGATION_EVENTS.indexOf(event) > -1)
return;
origOn.call(this, event, listener);
};
}
dialog.showOpenDialog = (...args) => handleDialog('open-dialog', args);
dialog.showSaveDialog = (...args) => handleDialog('save-dialog', args);
dialog.showMessageBox = (...args) => handleDialog('message-box', args);
dialog.showErrorBox = (...args) => handleDialog('error-box', args);
dialog.showCertificateTrustDialog = (...args) => handleDialog('certificate-trust-dialog', args);
process.argv.splice(1, 2);
};