electron 如何使开发工具不显示在屏幕上默认在电子?

4bbkushb  于 6个月前  发布在  Electron
关注(0)|答案(9)|浏览(79)

我正在使用electron-react-boilerplate创建Electron-React应用。开发工具默认显示在屏幕上。如何使开发工具仅在我要求时显示,而不是在启动时显示?
另外,控制台中没有显示错误,所以开发工具没有显示,因为有一个错误。
x1c 0d1x的数据

zkure5ic

zkure5ic1#

只需在main.js文件中注解或删除这行代码(将devTools设置为false)this.mainWindow.openDevTools();(或)将以下代码添加到

mainWindow = new BrowserWindow({ 
     width: 1024,
     height: 768,
     webPreferences: {
      devTools: false
      }
   });

字符串
(or)将package.json build改为npm run build && build --win --x64(或)再次安装npm

vngu2lb8

vngu2lb82#

如果我们在webPreferences中添加devTools: false,当您启动Electron应用程序时,DevTools将不会显示。但是,它仍然可以通过按Ctrl + Shift + I打开。

webPreferences: {
   devTools: false
}

字符串
看看Slack。它是用Electron制作的,当你按Ctrl + Shift + I时,DevTools不会打开。
我看过Electron的官方文档,我发现了一个解决方案,当你按Ctrl + Shift + I时,它不允许DevTool打开。

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所以,您可以在电子应用程序聚焦时编写上述代码。并且,当您的应用程序模糊时将其删除。这样您就可以正确解决这个问题。

e4eetjau

e4eetjau3#

当应用程序启动时,使开发人员工具出现的是src/main/main. ts中的require('electron-debug')()行。此函数有一个showDevTools选项,默认为true,所以您应该将其更改为false

require('electron-debug')({ showDevTools: false });

字符串
您仍然可以使用快捷键Ctrl + Shift + I或按F12显示开发人员工具,如果您想完全禁用它,请在new BrowserWindow上将webPreferences.devTools设置为false

mainWindow = new BrowserWindow({
  // ... other settings
  webPreferences: {
    // ...
    devTools: false,
  },
});

pgpifvop

pgpifvop4#

只需添加这两行粗体代码。打包后您将看不到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`)


打开DevTools。

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.

lnxxn5zx

lnxxn5zx5#

每个答案都提到,即使在使用devTools: false禁用devtools之后,键盘快捷键CTRL + CTRL + I仍然有效。
这是因为在Electron BrowserWindow的默认菜单中注册了加速器。您所要做的就是使用mainWindow.removeMenu()删除菜单,并且与开发相关的快捷键都不会再次工作。即使是像CTRL + R这样重新加载页面的快捷键。

mmvthczy

mmvthczy6#

只要把线拿掉

mainWindow.webContents.openDevTools(false);

字符串

w8rqjzmb

w8rqjzmb7#

只是想补充一点,如果您想仅在生产模式下禁用devTools**,您可以执行以下操作:

new BrowserWindow({
  webPreferences: {
    devTools: !app.isPackaged,
  },
})

字符串
P.S.这也可以防止使用快捷键,如Ctrl+Shift+I来切换devTools。

pinkon5k

pinkon5k8#

2022年

加载devtools的代码位于src/main/main.ts
搜索以下代码:

const isDebug =
  process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';

if (isDebug) {
  require('electron-debug')();
}

字符串
你需要禁用电子调试通过注解它

//require('electron-debug')();

q9rjltbz

q9rjltbz9#

警告:以下方法仅在您禁用electron应用程序中的工具栏时有效,在加载html之前,在创建浏览器窗口的代码之后添加以下代码:

//Considering "window" is your electron window object
window.setMenuBarVisibility(false)

字符串
在electron加载的网页中,在html结束标记之后添加:

<script>
    window.addEventListener("keydown", function (event) {
        //Control shift i                                                                                                  
        if (event.ctrlKey && event.shiftKey && event.key === "I") {
            event.preventDefault()
        }
        //Control shift j
        if (event.ctrlKey && event.shiftKey && event.key === "J") {
            event.preventDefault()
        }
        //Control shift c
        if (event.ctrlKey && event.shiftKey && event.key === "C") {
            event.preventDefault()
        }
        //F12
        if (event.key === "F12") {
            event.preventDefault()
        }
        //CMD option i
        if (event.metaKey && event.altKey && event.key === "I") {
            event.preventDefault()
        }
        //CMD option j
        if (event.metaKey && event.altKey && event.key === "J") {
            event.preventDefault()
        }
        //CMD option c
        if (event.metaKey && event.altKey && event.key === "C") {
            event.preventDefault()
        }
    });
</script>


如果你还想阻止reload,在前面的代码或结束的html标签后添加以下内容(在electron加载的网页中):

<script>
    window.addEventListener("keydown", function (event) {
        //Control shift r                                                                                              
        if (event.ctrlKey && event.shiftKey && event.key === "R") {
            event.preventDefault()
        }
        //Control r
        if (event.ctrlKey && event.key === "R") {
            event.preventDefault()
        }
        //F5
        if (event.key === "F5") {
            event.preventDefault()
        }
        //Shift F5
        if (event.shiftKey && event.key === "F5") {
            event.preventDefault()
        }
        //Control F5
        if (event.ctrlKey && event.key === "F5") {
            event.preventDefault()
        }
    });
</script>

相关问题