custom-electron-titlebar ReferenceError:导航器未定义

时间:2020-03-30 16:35:01

标签: javascript electron titlebar

当我尝试在index.js中创建自定义电子标题栏时,出现错误。

我的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”,但无法正常工作。

1 个答案:

答案 0 :(得分:0)

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

根据custom-electron-titlebar的{​​{3}},您必须从Renderer进程运行导入库,或将其添加到HTML脚本标记中。

有关电子过程模型的更多信息,您可以查看Usage docs

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

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

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