Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preload scripts not being executed in webview iframes

Webview tag that is present in the renderer process, somewhere in <body>:

<webview src="http://somewebpage.com" 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.com/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 :(

like image 385
Alex Avatar asked Sep 11 '20 11:09

Alex


2 Answers

main.js

mainWindow = new BrowserWindow({
    width: 1024,
    height: 728,
    webPreferences: {
        nodeIntegrationInSubFrames: true,
        webviewTag: true,
        nodeIntegration: true
    }
});

renderer

<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);
});

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.

like image 105
tpikachu Avatar answered Oct 13 '22 02:10

tpikachu


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.

like image 31
Jedson G. Avatar answered Oct 13 '22 04:10

Jedson G.