【发布时间】:2017-03-11 07:57:36
【问题描述】:
我正在使用 electron-react-boilerplate 创建一个 Electron-React 应用程序。开发工具默认显示在屏幕上。如何使开发工具仅在我请求时出现而不在启动时出现?
另外,控制台中没有显示错误,因此开发工具没有显示,因为有错误。
【问题讨论】:
我正在使用 electron-react-boilerplate 创建一个 Electron-React 应用程序。开发工具默认显示在屏幕上。如何使开发工具仅在我请求时出现而不在启动时出现?
另外,控制台中没有显示错误,因此开发工具没有显示,因为有错误。
【问题讨论】:
只需在此处添加这两行粗体代码即可。打包后看不到 devTool。
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
var debug = false
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html`)
// 打开开发者工具。
if(debug) mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
【讨论】:
只需注释或删除 main.js 文件中的这行代码(将 devTools 设置为 false)this.mainWindow.openDevTools();
(要么)
将以下代码添加到
mainWindow = new BrowserWindow({
width: 1024,
height: 768,
webPreferences: {
devTools: false
}
});
(或)
将 package.json 构建更改为 npm run build && build --win --x64
(要么)
再次安装 npm
【讨论】:
this.mainWindow.openDevTools();,它仍然可以在View/Toggle Developer Tools中打开。但是如果在webPreferences中设置devTools: false,Toggle Developer Tools将被禁用。
如果我们在webPreferences 中添加devTools: false,当您启动Electron 应用程序时,DevTools 将不会显示。但是,它仍然可以通过按 Ctrl + Shift + I 来打开。
webPreferences: {
devTools: false
}
看看 Slack。它是用 Electron 制作的,当你按下 Ctrl + Shift + I 时,DevTools 不会打开。
我查看了 Electron 的官方文档,我找到了一个解决方案,当您按下 Ctrl + Shift + 我。
const { app, globalShortcut } = require('electron');
app.on('ready', () => {
// Register a shortcut listener for Ctrl + Shift + I
globalShortcut.register('Control+Shift+I', () => {
// When the user presses Ctrl + Shift + I, this function will get called
// You can modify this function to do other things, but if you just want
// to disable the shortcut, you can just return false
return false;
});
});
但是,这会阻止所有其他浏览器的 Ctrl + Shift +I 因此,只要您的电子应用程序专注,您就可以编写上述代码。并且,当您的应用程序模糊时将其删除。这样您就可以找到解决此问题的正确方法。
【讨论】:
Ctrl+Shift+I 和devTools:false 打开开发工具。也许这在最近版本的电子中被禁用了(我在 v13 上)
keydown 侦听器作为组合键并阻止默认操作。
devTools:false 对我有用。
使开发者工具在应用启动时出现的是require('electron-debug')() 中的src/main/main.ts 行。这个函数有一个showDevTools option,默认是true,所以你应该把它改成false:
require('electron-debug')({ showDevTools: false });
您仍然可以使用快捷键 Ctrl + Shift + I 或按 F12 来显示开发者工具kbd>,如果您想完全禁用它,请将new BrowserWindow上的webPreferences.devTools设置为false:
mainWindow = new BrowserWindow({
// ... other settings
webPreferences: {
// ...
devTools: false,
},
});
【讨论】:
只想补充一点,如果您想仅在生产模式下禁用 devTools,您可以这样做:
new BrowserWindow({
webPreferences: {
devTools: !app.isPackaged,
},
})
附:这也可以防止使用诸如 Ctrl+Shift+I 之类的快捷键来切换 devTools。
【讨论】:
删除线
mainWindow.webContents.openDevTools(false);
【讨论】:
每个答案都提到键盘快捷键 CTRL + SHIFT + I 即使在使用 devTools: false 禁用开发工具后仍然有效。
这是因为在 Electron BrowserWindow 的默认菜单中注册了加速器。您所要做的就是使用mainWindow.removeMenu() 删除菜单,并且与开发相关的快捷键都不会再次起作用。甚至像 CTRL + R 这样会重新加载页面。
【讨论】: