首页 > 解决方案 > 自定义电子标题栏 ReferenceError:未定义导航器

问题描述

当我尝试在 index.js 中执行 custom-electron-titlebar 时,出现错误。

我的 index.js 代码:

    const { app, BrowserWindow } = require('electron');
    const customTitlebar = require('custom-electron-titlebar');
    var path = require('path');

    let mainWindow;

    function onClosed() {
    mainWindow = null;
}
app.on('ready', () => {

    mainWindow = new BrowserWindow({
        width: 350,
        height: 210,
        frame: false
    })
    new customTitlebar.Titlebar({
        backgroundColor: customTitlebar.Color.fromHex('#444')
    });

    customTitlebar.setTitle('asd')

    mainWindow.setMenuBarVisibility(false)
    mainWindow.loadURL(`file:\\${__dirname}\\index.html`)
    mainWindow.on('closed', onClosed)
});

如果我运行这个我得到这个错误:

ReferenceError: navigator is not defined
at Object.<anonymous> (<mypath>\node_modules\custom- 
electron-titlebar\lib\browser\browser.js:130:19)
at Module._compile (internal/modules/cjs/loader.js:968:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:986:10)
at Module.load (internal/modules/cjs/loader.js:816:32)
at Module._load (internal/modules/cjs/loader.js:728:14)
at Module._load (electron/js2c/asar.js:717:26)
at Function.Module._load (electron/js2c/asar.js:717:26)
at Module.require (internal/modules/cjs/loader.js:853:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Programing\Projects\ElectronProjects\Calculator\node_modules\custom- 
electron-titlebar\lib\common\dom.js:7:17)

我导入了“custom-electron-titlebar”,但它不起作用。

标签: javascriptelectrontitlebar

解决方案


navigator是一个浏览器 API,仅在 Renderer 进程中可用。您require('custom-electron-titlebar')从主进程调用,该进程无权访问该 API。

您必须根据.custom-electron-titlebar

有关 Electron 进程模型的更多信息,您可以查看文档

BrowserWindow主进程通过创建实例来创建网页。每个BrowserWindow实例在其自己的渲染器进程中运行网页。当一个BrowserWindow实例被销毁时,相应的渲染器进程也被终止。

主进程管理所有网页及其相应的渲染器进程。每个渲染器进程都是隔离的,只关心其中运行的网页。

在网页中,不允许调用原生 GUI 相关的 API,因为在网页中管理原生 GUI 资源非常危险,而且很容易泄露资源。如果要在网页中执行 GUI 操作,则网页的渲染器进程必须与主进程通信以请求主进程执行这些操作。


推荐阅读