我正在使用Electron框架在webview中加载页面。我希望在浏览器加载页面时加载页面而不关闭node-integration
。我尝试了在网上找到的各种解决方案,但无济于事。
我已经开始electron-quick-start.
这是我修改后的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
<webview id="foo" preload="./preload.js" src="https://www.icicibank.com/" style="display:inline-flex; width:1000px; height:800px" nodeIntegration></webview>
</body>
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</html>
此preload.js
位于同一根文件夹
window.onload = function() {
window.$ = window.jQuery = require('jquery');
console.log([$, jQuery ]);
// I also tried the below:
// but with no success
// var script = document.createElement("script");
// script.src = "https://code.jquery.com/jquery-2.1.4.min.js";
// document.body.appendChild(script);
};
console.log
语句打印值但是当我通过在index.html
页面的devtools控制台中键入以下内容打开webview的devtools时,我收到了错误
var wv = document.getElementById("foo");
wv.openDevTools();
我也尝试了这里描述的解决方案Electron: jQuery is not defined,但结果相同。请帮忙。这是完整的代码https://github.com/ajaykgp/electron-start
答案 0 :(得分:0)
这样做:
main.js
const path = require('path')
const mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: false,
preload: path.join(__dirname, 'renderer.js')
}
})
renderer.js
const path = require('path')
window.addEventListener('load', () => {
//inject jquery to page
window.$ = window.jQuery = require(path.join(__dirname, '/jquery-3.2.1.slim.min.js'));
});