呈现器进程中存在的 Webview 标记,位于 <body>
中的某处:
<webview src="http://somewebpage.com" preload="somescript.js">
somescript.js
在某个网页中执行,但如果某个网页有 <iframe>
s 在其中,脚本将不会在 iframe 中运行。我怎样才能让它运行?在 iframe 中的任何其他脚本之前?
我在 github 上发现了这个似乎相关的问题:
https://github.com/electron/electron/pull/19260
但这没有任何意义...
我尝试添加
nodeintegrationinsubframes
并将值从 false 更改为 true<webview src="somewebpage" preload="somescript.js" nodeintegrationinsubframes="false">
但它没有效果:(
最佳答案
main.js
mainWindow = new BrowserWindow({
width: 1024,
height: 728,
webPreferences: {
nodeIntegrationInSubFrames: true,
webviewTag: true,
nodeIntegration: true
}
});
渲染器 <webview
src="https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe"
preload="./preload.js"
style='width: 100%; height: 800px'
nodeIntegrationInSubFrames
/>
preload.js process.once("loaded", () => {
alert(window.location);
});
您可以根据 window.location
指定要执行 javascript 的位置。上面的代码将显示每个子 iframe 的位置。这对我很有效。
关于javascript - 未在 webview iframe 中执行的预加载脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63845805/