![](/img/trans.png)
[英]Electron : How to access webview inside a webpage which is rendered in the render process from the main process (toPrint , etc)?
[英]How to access jquery from the render process inside the preload script on Electron
我在窗口中将Node集成配置为false,因为如文档所述,禁用是一种很好的做法。
我想在预加载脚本中访问渲染过程的jQuery,但是我不知道该怎么做。
在预加载脚本中发生某些事件之后,是否可以激活模式(例如)?
链接到我在Github中的示例代码: https : //github.com/JhonatanRSantos/Sample
正如Janith所说的,您不需要使用预加载脚本。 这是一个基于您的Github存储库的工作示例:
Index.html-在HEADER部分,像这样修改:
<script>window.$ = window.jQuery = require('jquery');</script>
<script src="./node_modules/popper.js/dist/umd/popper.min.js"></script>
<script src="./node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
请注意,您必须使用路径./node_modules/popper.js/dist/umd/popper.min.js
否则您将在控制台中收到错误消息(请参阅引导程序4中的popper.js提供了SyntaxError意外令牌导出 )
Index.html-页脚,添加:
<script src="./index.js"></script>
将preload.js重命名为index.js
index.js可以是:
console.log('Preload.js');
setTimeout(() => {
console.log('Open Boostrap Modal');
$('#myModal').modal('show');
}, 5000);
最后,main.js可以是:
const { app, BrowserWindow } = require('electron');
const path = require('path')
const url = require('url')
let createWindow = () => {
let win = new BrowserWindow({
width: 800,
height: 500,
center: true,
resizable: false,
show: false
});
win.setMenu(null);
const mainUrl = url.format({ // https://electronjs.org/docs/api/browser-window#winloadurlurl-options
protocol: 'file',
slashes: true,
pathname: path.join(__dirname, 'index.html')
})
win.loadURL(mainUrl);
win.once('ready-to-show', () => {
win.show();
win.webContents.openDevTools();
});
win.on('closed', () => {
win = null;
});
};
app.on('ready', createWindow);
我向您的回购请求请求以获取更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.