gpt4 book ai didi

electron - (Electron) BrowserWindow 的 baseURLForDataURL 选项期望什么?

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

我正在从 data-uri 加载一个窗口:

pref.loadURL('data:text/html;charset=utf-8,' + encodeURI(str), { baseURLForDataURL: 'file://' + app.getAppPath() } );

好消息是,开发工具控制台现在显示了应该加载但没有加载的 css/js 文件的错误,但我无法理解它的预期。任何地方都没有示例,甚至在启发此选项的 github 问题中也没有。

它是否需要绝对路径(如我上面的示例)?

最佳答案

它通常会期望

'file://' + app.getAppPath().replace("\\", "/") + "/"
但目前,似乎存在一个问题,至少在使用协议(protocol)“file://”时我们会收到错误。 ( https://github.com/electron/electron/issues/20700)
解决此问题的一种方法是生成自定义文件协议(protocol)。
const { app, BrowserWindow, screen, protocol  } = require('electron');
const path = require('path');

app.on('ready', () => {
const customProtocol = 'file2';

protocol.registerFileProtocol(customProtocol, (request, callback) => {
const url = request.url.substr(customProtocol.length + 2);
const file = { path: path.normalize(`${__dirname}/${url}`) }
callback(file)
});

let win = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: true
}
});

win.loadURL(`data:text/html;charset=UTF-8,${encodeURIComponent(indexHTML)}`, {
baseURLForDataURL: `${customProtocol}://${app.getAppPath().replace("\\", "/")}/`
});
});

关于electron - (Electron) BrowserWindow 的 baseURLForDataURL 选项期望什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54154745/

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