javascript - 未在 webview iframe 中执行的预加载脚本

标签 javascript node.js iframe webview electron

呈现器进程中存在的 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/

相关文章:

javascript - 在javascript中查找html元素的结束标记

javascript - 使用 AngularJS $http 时检查 php 插入是否成功

javascript - 文件无法复制到另一个目录(fs.access 和 copyFile)

jquery - 使用 jQuery 获取 iframe 中元素的高度

javascript - webpack config如何根据环境更改插件

javascript - 对使用 SQL Server 数据库的 ASP 站点的攻击

mysql - 无法从 Node js插入mysql

javascript - Crypto++ 和 node.js 组合

javascript - Firefox element.event 有效,但 element.addEventlistener 无效

javascript - 不安全的 JavaScript 尝试访问框架...协议(protocol)必须匹配