javascript - 是否可以从内存中的 html 字符串加载 Electron webContents?

标签 javascript node.js electron

假设我有 2 个进程:

进程 1 正在向进程 2 发送一个有效的 html 字符串:

ipcRenderer.send('open-window-from-string', 
'<!DOCTYPE html>' + '<html>' + htmlElement.innerHTML + '</html>');

进程 2(Electron 主进程)正在尝试从该字符串打开一个新窗口:

ipc.on('open-window-from-string', (event, htmlString) => {
  const windowFromString= BrowserWindow.fromWebContents(htmlString);
}

我知道我可以将 html 保存为实际的 html 文件。这样一切都在使用时工作:

loadURL(`file://${__dirname}/windowFromString.html`);

然而,这会导致不必要的读/写操作。

这就是为什么我试图从内存中的 htmlString 加载一个新窗口。 所以问题又是: 是否可以从内存中的 html 字符串加载 electron webContents?

在此先感谢您的帮助。

问候, 巨人

最佳答案

在 Chrome 中,您可以通过使用数据导航 URL 来显示内联 html 页面:协议(protocol),例如 data:text/html;charset=utf-8,<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>MyYTitle</title> <style type="text/css"> </style></head> <body>Hello world from Lyon, FR</body>

它在 Electron 中的工作方式相同。 您可以尝试用 loadURL('data:text/html;charset=utf-8,<YOUR HTML/>'); 打开一个窗口吗? ?

关于javascript - 是否可以从内存中的 html 字符串加载 Electron webContents?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42022712/

相关文章:

java - Node.js MongoDb 确保集合异步工作流程

javascript - 用Electron.js加载一个Nunjucks模板

javascript - 替换字符(将 python 代码翻译为 JavaScript)

javascript - 默认设置data-html5值

javascript - 运行 jquery 媒体查询而不重新加载

mysql - 序列化具有相同外键的多条记录的连接表

Javascript - 鼠标悬停/移出事件同时在所有同级元素上触发

node.js - Node ,postgres pg-promise : Leaking active handles ONLY in azure webapp container

node.js - 使用 Keycloak 保护 Electron 应用程序

angularjs - 复制并替换 Electron 版中的数据库文件