Skip to content

Instantly share code, notes, and snippets.

@t57ser
Created February 7, 2022 13:12
Show Gist options
  • Save t57ser/ce254e6e2e828949809bf872ed4168b6 to your computer and use it in GitHub Desktop.
Save t57ser/ce254e6e2e828949809bf872ed4168b6 to your computer and use it in GitHub Desktop.
const { app, BrowserWindow } = require( "electron" )
app.whenReady().then(()=>{
let main = new BrowserWindow({
"height" : 600,
"width" : 800
})
main.loadURL( "https://www.youtube.com" )
main.webContents.on( "did-finish-load", ()=>{
let iframe = main.webContents.mainFrame.frames[ 0 ]
let injection = "new Promise(( resolve )=>{ resolve('hello') })"
let handler = ( result ) => { console.log( result ) }
//this code does not evaluate the same:
iframe.executeJavaScript( injection ).then( handler ) //returns "{}"
main.webContents.executeJavaScript( injection ).then( handler ) //returns "hello"
})
})
{
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "17.0.0"
}
}
@pinuke
Copy link

pinuke commented Feb 8, 2022

Just to outline the potentially obvious...

This gist only works and reproduces the bug if the URL on line 10 meets the following criteria...:

  • has an iframe
  • iframe exists* prior to the "did-finish-load" event

* - This is based on my personal understanding of the executeJavaScript method. I believe it can be executed without having anything loaded in the iframe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment