javascript - 自定义电子标题栏 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”,但它不起作用。
解决方案
navigator
是一个浏览器 API,仅在 Renderer 进程中可用。您require('custom-electron-titlebar')
从主进程调用,该进程无权访问该 API。
您必须根据.custom-electron-titlebar
有关 Electron 进程模型的更多信息,您可以查看文档。
BrowserWindow
主进程通过创建实例来创建网页。每个BrowserWindow
实例在其自己的渲染器进程中运行网页。当一个BrowserWindow
实例被销毁时,相应的渲染器进程也被终止。主进程管理所有网页及其相应的渲染器进程。每个渲染器进程都是隔离的,只关心其中运行的网页。
在网页中,不允许调用原生 GUI 相关的 API,因为在网页中管理原生 GUI 资源非常危险,而且很容易泄露资源。如果要在网页中执行 GUI 操作,则网页的渲染器进程必须与主进程通信以请求主进程执行这些操作。
推荐阅读
- python - 导出到 EXE 后更新数据库
- here-api - 定义新高度后地图消失
- azure - Azure API 管理中的 AAD,避免注册对话框
- dart - 从列表中删除项目后更新 UI
- typo3 - 扩展不能覆盖默认(回退)templateRootPaths/partialRootPaths/LayoutRootPaths
- mysql - 搜索存储在 MySql 中的 JSON 数据
- frequency - MFCC:Librosa MFCC 中的 20 个滤波器组的频率范围是多少?
- c# - 启用 CORS 无法在全局范围内工作
- javascript - 如何使用emoji mart在textarea中设置emoji的简码?
- java-8 - 关于将字符串列表更改为每个元素和其他元素的映射列表