gpt4 book ai didi

javascript - 自定义 Electron 标题栏ReferenceError : navigator is not defined

转载 作者:行者123 更新时间:2023-12-03 12:27:57 27 4
gpt4 key购买 nike

当我尝试在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是仅在Renderer进程中可用的浏览器API。您正在从Main进程调用require('custom-electron-titlebar'),而该进程无权访问该API。
根据custom-electron-titlebarUsage docs,您必须从Renderer进程运行导入库,或将其添加到HTML脚本标签中。
有关 Electron 过程模型的更多信息,可以 checkout docs

The main process creates web pages by creating BrowserWindow instances. Each BrowserWindow instance runs the web page in its own renderer process. When a BrowserWindow instance is destroyed, the corresponding renderer process is also terminated.

The main process manages all web pages and their corresponding renderer processes. Each renderer process is isolated and only cares about the web page running in it.

In web pages, calling native GUI related APIs is not allowed because managing native GUI resources in web pages is very dangerous and it is easy to leak resources. If you want to perform GUI operations in a web page, the renderer process of the web page must communicate with the main process to request that the main process perform those operations.

关于javascript - 自定义 Electron 标题栏ReferenceError : navigator is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60935163/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com