2

Im working with electron, i need to return value from executeJavaScript like below, anyway to get value of body? thanks for read

let mainWin;
function createMainWin(){
    mainWin = new BrowserWindow({
        width: 1920, 
        height:1080, 
        backgroundColor:'#ccc', 
        title:'Test',
        webPreferences: {
            nativeWindowOpen: true,
        }
    });

    mainWin.loadURL('https://example.com');
    mainWin.webContents.executeJavaScript(`
        var body = document.querySelector('body').innerHTML;//value need to get
        `
    )
    mainWin.webContents.openDevTools();
}
vy.pham
  • 571
  • 6
  • 20
  • Welcome to asynchronous JavaScript. You need to learn about callbacks, Promises, and async/await – tehhowch Nov 01 '19 at 13:25
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – tehhowch Nov 01 '19 at 13:26

1 Answers1

1

Aparently, as I'm seeing in the docs, you can get it with a then function.

let mainWin;
function createMainWin(){
    mainWin = new BrowserWindow({
        width: 1920, 
        height:1080, 
        backgroundColor:'#ccc', 
        title:'Test',
        webPreferences: {
            nativeWindowOpen: true,
        }
    });

    mainWin.loadURL('https://example.com');
    mainWin.webContents.executeJavaScript(`
        document.querySelector('body').innerHTML; //value need to get
    `).then( (result) => {
        console.log(result);
    })
    mainWin.webContents.openDevTools();
}
Mario SG
  • 161
  • 7