-
-
Notifications
You must be signed in to change notification settings - Fork 747
/
debug.js
117 lines (98 loc) · 2.89 KB
/
debug.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
'use strict';
const electron = require('electron');
const localShortcut = require('electron-localshortcut');
const { app, BrowserWindow } = electron;
const isMacOS = process.platform === 'darwin';
const devToolsOptions = {
mode: 'bottom'
};
function toggleDevTools(win = BrowserWindow.getFocusedWindow()) {
if (win) {
const { webContents } = win;
if (webContents.isDevToolsOpened()) {
webContents.closeDevTools();
} else {
webContents.openDevTools(devToolsOptions);
}
}
}
function devTools(win = BrowserWindow.getFocusedWindow()) {
if (win) {
toggleDevTools(win);
}
}
function openDevTools(win = BrowserWindow.getFocusedWindow()) {
if (win) {
win.webContents.openDevTools(devToolsOptions);
}
}
function refresh(win = BrowserWindow.getFocusedWindow()) {
if (win) {
win.webContents.reloadIgnoringCache();
}
}
function inspectElements() {
const win = BrowserWindow.getFocusedWindow();
const inspect = () => {
win.devToolsWebContents.executeJavaScript('DevToolsAPI.enterInspectElementMode()');
};
if (win) {
if (win.webContents.isDevToolsOpened()) {
inspect();
} else {
win.webContents.once('devtools-opened', inspect);
win.openDevTools();
}
}
}
const addExtensionIfInstalled = (name, getPath) => {
const isExtensionInstalled = name => {
return BrowserWindow.getDevToolsExtensions &&
{}.hasOwnProperty.call(BrowserWindow.getDevToolsExtensions(), name);
};
try {
if (!isExtensionInstalled(name)) {
BrowserWindow.addDevToolsExtension(getPath(name));
}
} catch (_) {
}
};
module.exports = opts => {
opts = Object.assign({
enabled: null,
showDevTools: true,
devToolsMode: 'docked'
}, opts);
if (opts.enabled === false) {
return;
}
if (opts.devToolsMode !== 'previous') {
devToolsOptions.mode = opts.devToolsMode;
}
app.on('browser-window-created', (event, win) => {
if (opts.showDevTools) {
win.webContents.once('devtools-opened', () => {
// Workaround for https://github.com/electron/electron/issues/13095
setImmediate(() => {
win.focus();
});
});
/// Workaround for https://github.com/electron/electron/issues/12438
win.webContents.once('dom-ready', () => {
openDevTools(win, opts.showDevTools);
});
}
});
app.on('ready', () => {
addExtensionIfInstalled('devtron', name => require(name).path);
addExtensionIfInstalled('electron-react-devtools', name => require(name).path);
localShortcut.register('CmdOrCtrl+Shift+C', inspectElements);
localShortcut.register(isMacOS ? 'Cmd+Alt+I' : 'Ctrl+Shift+I', devTools);
localShortcut.register('F12', devTools);
localShortcut.register('CmdOrCtrl+R', refresh);
localShortcut.register('F5', refresh);
});
};
module.exports.refresh = refresh;
module.exports.devTools = devTools;
module.exports.openDevTools = openDevTools;