简体   繁体   中英

How do you print messages to openDevTools's console in Electron js?

I just completed a hello world application for electron js. I can print to my terminal via console.log and I can open the openDevTools in the window of my application but I'd like for my console.log statements to print to the openDevTools in the window if possible. Does anyone know how to do this?

To print to dev tools in electron, your console.log needs to be inside the JavaScript running as your client, not the server part that you run from the console.

Say this is your electron app (snippet)

app.on('ready', () => {
    // load the local HTML file
    let url = require('url').format({
        protocol: 'file',
        slashes: true,
        pathname: require('path').join(__dirname, '/build/index.html')
    })
    mainWindow.loadURL(url);
    mainWindow.webContents.openDevTools();
    console.log('this will go to the terminal');
});

index.html:

<script>
    console.log('this will go to electron dev tools');
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM