vy.pham
vy.pham

Reputation: 611

how to get a return value from executeJavaScript

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

Upvotes: 3

Views: 3475

Answers (1)

Mario SG
Mario SG

Reputation: 161

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

Upvotes: 1

Related Questions