-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.js
91 lines (77 loc) · 2.28 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
import process from 'node:process';
import {app, BrowserWindow} from 'electron';
import localShortcut from 'electron-localshortcut';
import isDev from 'electron-is-dev';
const isMacOS = process.platform === 'darwin';
const developmentToolsOptions = {};
function toggleDevelopmentTools(win = BrowserWindow.getFocusedWindow()) {
if (win) {
const {webContents} = win;
if (webContents.isDevToolsOpened()) {
webContents.closeDevTools();
} else {
webContents.openDevTools(developmentToolsOptions);
}
}
}
// eslint-disable-next-line unicorn/prevent-abbreviations
export function devTools(win = BrowserWindow.getFocusedWindow()) {
if (win) {
toggleDevelopmentTools(win);
}
}
// eslint-disable-next-line unicorn/prevent-abbreviations
export function openDevTools(win = BrowserWindow.getFocusedWindow()) {
if (win) {
win.webContents.openDevTools(developmentToolsOptions);
}
}
export 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();
}
}
}
export default function debug(options) {
options = {
isEnabled: null,
showDevTools: true,
devToolsMode: 'previous',
...options,
};
if (options.isEnabled === false || (options.isEnabled === null && !isDev)) {
return;
}
if (options.devToolsMode !== 'previous') {
developmentToolsOptions.mode = options.devToolsMode;
}
app.on('browser-window-created', (event, win) => {
if (options.showDevTools) {
/// Workaround for https://github.com/electron/electron/issues/12438
win.webContents.once('dom-ready', () => {
openDevTools(win, options.showDevTools, false);
});
}
});
(async () => {
await app.whenReady();
localShortcut.register('CommandOrControl+Shift+C', inspectElements);
localShortcut.register(isMacOS ? 'Command+Alt+I' : 'Control+Shift+I', devTools);
localShortcut.register('F12', devTools);
localShortcut.register('CommandOrControl+R', refresh);
localShortcut.register('F5', refresh);
})();
}