Electron提供了webview标签,用来嵌入Web页面。
它作用上类似于HTML里的iframe标签,但跑在独立进程中,主要出于安全性考虑。它不拥有渲染进程的权限,并且应用和嵌入内容之间的交互全部都是异步的.因为这能保证应用的安全性不受嵌入内容的影响。
main.js
- webPreferences: {
- webviewTag: true, // 开启webview
- }
在其他脚本执行之前预加载一个指定的脚本。
渲染进程:
index.html
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Hello World!title>
- head>
- <body>
- <h1>父窗口h1>
- <div><button onclick="confirm()">确认button>div>
- <webview src="http://www.baidu.com" preload="./render.js">webview>
- body>
- html>
render.js
- setTimeout(function(){
- alert(document.getElementById("su").value);
- document.getElementById("su").onclick = function(){
- alert("您点击了搜索按钮!");
- }
- }, 5000)