admin管理员组文章数量:1403221
Webview tag that is present in the renderer process, somewhere in <body>
:
<webview src="; preload="somescript.js">
somescript.js
is executed in somewebpage, but if somewebpage has <iframe>
s in it, the script will not run in the iframe.
How can I make it run? And before any other script in the iframe?
I found this issue on github that seems related: but it doesn't make any sense...
I tried adding nodeintegrationinsubframes
and changing values from false to true
<webview src="somewebpage" preload="somescript.js" nodeintegrationinsubframes="false">
but it has no effect :(
Webview tag that is present in the renderer process, somewhere in <body>
:
<webview src="http://somewebpage." preload="somescript.js">
somescript.js
is executed in somewebpage, but if somewebpage has <iframe>
s in it, the script will not run in the iframe.
How can I make it run? And before any other script in the iframe?
I found this issue on github that seems related: https://github./electron/electron/pull/19260 but it doesn't make any sense...
I tried adding nodeintegrationinsubframes
and changing values from false to true
<webview src="somewebpage" preload="somescript.js" nodeintegrationinsubframes="false">
but it has no effect :(
Share Improve this question asked Sep 11, 2020 at 11:03 AlexAlex 66.2k185 gold badges459 silver badges651 bronze badges3 Answers
Reset to default 4 +500main.js
mainWindow = new BrowserWindow({
width: 1024,
height: 728,
webPreferences: {
nodeIntegrationInSubFrames: true,
webviewTag: true,
nodeIntegration: true
}
});
renderer
<webview
src="https://www.w3schools./tags/tryit.asp?filename=tryhtml_iframe"
preload="./preload.js"
style='width: 100%; height: 800px'
nodeIntegrationInSubFrames
/>
preload.js
process.once("loaded", () => {
alert(window.location);
});
You can specify where you are going to execute javascript based on the window.location
This above code will show the locations of every sub iframes.
This works for me very well.
I was having the same problems with my project and updating the electron to the latest beta version solved for me.
I assume you know how to do this:
npm install [email protected]
You still have to consider the stability concerns of using a development version of the package.
By scratching on electron's
documentation perhaps this could be helpful as an alternative.
app.js
let win
app.whenReady().then(() => {
win = new BrowserWindow({
webPreferences: {
nodeIntegrationInSubFrames: true,
webviewTag: true,
nodeIntegration: false,
preload: path.join(app.getAppPath(), 'preload.js') // Specifies a script that will be loaded before other scripts run in the page. This script will always have access to node APIs no matter whether node integration is turned on or off. The value should be the absolute file path to the script. When node integration is turned off, the preload script can reintroduce Node global symbols back to the global scope.
}
})
...
})
renderrer
<webview
src="https://somewebpage."
preload="./preload.js"
nodeintegrationinsubframes>
preload.js
/* It can be used by the preload script to add removed Node global symbols back to the global scope when node integration is turned off */
const _setImmediate = setImmediate
const _clearImmediate = clearImmediate
process.once('loaded', () => {
global.setImmediate = _setImmediate
global.clearImmediate = _clearImmediate
})
My answer is based on the following resources:
- Electron Documentation: Tag
- Electron Documentation: BrowserWindow
- Electron Documentation: process
- Electron Documentation: Web embeds in Electron - WebViews
- Electron Documentation: Preload Example
本文标签: javascriptPreload scripts not being executed in webview iframesStack Overflow
版权声明:本文标题:javascript - Preload scripts not being executed in webview iframes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744386782a2603779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论