- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的 fs
中使用 Node 模块(在本例中为 renderer
)。过程,像这样:
// main_window.js
const fs = require('fs')
function action() {
console.log(fs)
}
action
当我按下我的
main_window
中的按钮时,函数会被调用.
Uncaught ReferenceError: require is not defined
at main_window.js:1
main.js
初始化
main_window
时:
// main.js
main_window = new BrowserWindow({
width: 650,
height: 550,
webPreferences: {
nodeIntegration: true
}
})
preload.js
文件并在那里加载这些 Node 模块,然后在我的所有
renderer
中使用它过程。像这样:
main.js
:
main_window = new BrowserWindow({
width: 650,
height: 550,
webPreferences: {
preload: path.join(app.getAppPath(), 'preload.js')
}
})
preload.js
:
const fs = require('fs')
window.test = function() {
console.log(fs)
}
main_window.js
:
function action() {
window.test()
}
renderer
的大部分代码不是违反直觉吗?
preload.js
中的进程(因为只有在
preload.js
我可以访问 Node 模块)然后只调用每个
renderer.js
中的函数文件(例如这里,
main_window.js
)?我在这里不明白什么?
最佳答案
编辑 2022
我有 published a larger post关于 Electron 的历史(整个 Electron 版本的安全性如何变化)以及 Electron 开发人员可以做出的其他安全考虑,以确保预加载文件在新应用程序中正确使用。
编辑 2020
正如另一位用户所问,让我在下面解释我的答案。preload.js
的正确使用方法在 Electron 中,是在您的应用程序可能需要的任何模块周围公开列入白名单的 package 器 require
.
安全方面,暴露 require
很危险,或您通过 require
检索到的任何内容调用您的preload.js
(参见 my comment here 了解更多解释原因)。如果您的应用程序加载远程内容(很多人会这样做),则尤其如此。
为了做正确的事情,您需要在 BrowserWindow
上启用许多选项。正如我在下面详述的那样。设置这些选项会强制您的 Electron 应用程序通过 IPC(进程间通信)进行通信,并将两个环境相互隔离。像这样设置您的应用程序可以让您验证可能是 require
的任何内容'd 模块在您的后端,它不受客户端篡改。
下面,您将找到一个简短示例,说明我所说的内容以及它在您的应用中的外观。如果您刚开始,我可能会建议使用 secure-electron-template
(我是其中的作者)在构建 Electron 应用程序时从一开始就包含了所有这些安全最佳实践。
This page还提供了有关使用 preload.js 制作安全应用程序时所需的架构的良好信息。
main.js
const {
app,
BrowserWindow,
ipcMain
} = require("electron");
const path = require("path");
const fs = require("fs");
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
async function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false, // is default value after Electron v5
contextIsolation: true, // protect against prototype pollution
enableRemoteModule: false, // turn off remote
preload: path.join(__dirname, "preload.js") // use a preload script
}
});
// Load app
win.loadFile(path.join(__dirname, "dist/index.html"));
// rest of code..
}
app.on("ready", createWindow);
ipcMain.on("toMain", (event, args) => {
fs.readFile("path/to/file", (error, data) => {
// Do something with file contents
// Send result back to renderer process
win.webContents.send("fromMain", responseObj);
});
});
preload.js
const {
contextBridge,
ipcRenderer
} = require("electron");
// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld(
"api", {
send: (channel, data) => {
// whitelist channels
let validChannels = ["toMain"];
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);
}
},
receive: (channel, func) => {
let validChannels = ["fromMain"];
if (validChannels.includes(channel)) {
// Deliberately strip event as it includes `sender`
ipcRenderer.on(channel, (event, ...args) => func(...args));
}
}
}
);
index.html
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8"/>
<title>Title</title>
</head>
<body>
<script>
window.api.receive("fromMain", (data) => {
console.log(`Received ${data} from main process`);
});
window.api.send("toMain", "some data");
</script>
</body>
</html>
关于javascript - 如何在 Electron 中正确使用 preload.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57807459/
我遇到的“预加载”的唯一设置是“无” - 有哪些选择? 我有一部很长的电影,需要一段时间才能开始(我假设是因为大小),我希望能够让它更快地播放。我认为让视频在页面加载后立即开始加载会给观众一个更快的
我正在为 PWA 开发一个 gatsby 主题/启动器,但我似乎无法摆脱以下控制台警告: The resource https://davidde.github.io/gatsby-starter-s
我正在使用 使资源加载速度更快的技术,但我没有看到对页面加载时间的可衡量影响。我不确定这是因为资源实际上没有被预加载,还是因为其他原因。 如果我使用 Firefox 进行调试,如何在 Firefox
如何正确预加载谷歌字体,而不在控制台中发出警告? 这是我的代码: 我已经在预加载链接中添加了 crossorigin 属性,但警告仍然存在 最佳答案 只需将跨域属性添加到主样式表
我正在从头开始学习 webpack。我已经学会了如何将 javascript 文件与 require 链接起来。我正在捆绑和缩小我的 js 文件,我正在用 watch 监听变化。我正在设置加载程序以将
我的 Rails 3.2 应用程序使用两个不同的数据库。主操作系统是 MySQL,而其他操作系统是 sqlserver 数据库: development: adapter: mysql2
我的响应式 flexslider 插件有问题。除非您在实际幻灯片中有许多图像,否则该插件可以正常工作。加载行为是 Not Acceptable 。 我希望有人可以帮助我使用以下 flexslider
我正在尝试修改页面上的控件以减少 session 依赖性。我目前无法访问页面本身,只能访问主要由 DataGrid 组成的控件。我正在尝试在回发时检索 DataGrid 信息,以便我可以操作数据并重新
我发现该指令在 Controls.pas(以及其他单元)中声明,我很高兴知道它的含义。 {$C PRELOAD} 据我所知 $C 表示断言控制,但是 PRELOAD 关键字是什么?是不是类似于“在预加
为了在加载远程内容时提供适当的安全级别,声明必须分别启用和禁用BrowserWindow的contextIsolation和nodeIntegration选项。在这种情况下,Node/Electron
为什么 Gorm 中的 Preload 函数是一个“好的实践”,因为每次调用它时它都会检索给定表的所有记录(热切加载)? 我看到很多人在处理关系时将预加载视为一个很好的解决方案,但我不明白为什么。 最
我已经安装了webpack@2.1.0-beta.27。之前,我使用的是 webpack@2.1.0-beta.22。在我的配置文件中,我使用了 preLoaders 和 postLoaders: p
我有代码: jQuery(img).each(function() { jQuery(this).animate({opacity: 0}); jQuery(t
这是 JPA2 实体监听器注释: @PrePersist @PreRemove @PostPersist @PostRemove @PreUpdate @PostUpdate @PostLoad 但是
Android MapView.preLoad() 方法的真正作用是什么? “API 声明在 map 当前中心预加载图 block ”。这是什么意思,我能否以某种方式使用它来加快 map 在我的 Ma
我正在开发一个 JavaFX 应用程序,该应用程序需要在启动主应用程序阶段之前从文件中加载资源。我完成此任务的解决方案是使用 PreLoader,以便用户在加载资源之前无法与应用程序交互(非常标准的东
好的,首先,这是我的代码。 var loader = document.getElementById("loader"); window.addEventListener("loader", func
在 CSS 中,我为转换编写了一些肮脏的代码,它加载了 24 个不同的蒙版图像。第一次播放时,加载时有延迟,但如果我有一个 div“预加载”它,即在页面加载时播放它,它似乎被缓存了,因此不会再次延迟,
我正在为我的网站制作一个自定义预加载器。它在本地主机上运行良好,但在实时站点上,div 不显示动画,文本仅放置在站点底部。
我使用 CSS 创建了一个预加载器。我想在预加载器完成后在幻灯片放映中显示图像。预加载完成后,图像应该会平滑淡入。这是我的代码结构。 #container{
我是一名优秀的程序员,十分优秀!