【问题标题】:custom-electron-titlebar ReferenceError: navigator is not defined自定义电子标题栏 ReferenceError:未定义导航器
【发布时间】:2020-07-11 02:15:39
【问题描述】:

当我尝试在 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”,但它不起作用。

【问题讨论】:

    标签: javascript electron titlebar


    【解决方案1】:

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

    您必须根据 custom-electron-titlebarUsage docs 运行从渲染器进程中导入库或将其添加到 HTML 脚本标记中。

    有关 Electron 流程模型的更多信息,您可以查看docs

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多