Skip to content

Instantly share code, notes, and snippets.

@CharlieHess
Created Oct 30, 2019
Embed
What would you like to do?
web-contents-created fired too early?
const {app, BrowserWindow, webContents: WebContents} = require('electron')
const assert = require('assert')
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.loadFile('index.html')
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('web-contents-created', (e, webContents) => {
const theId = webContents.id
const existingWebContents = WebContents.fromId(theId)
assert(!!existingWebContents, 'webContents missing?!')
})
app.on('ready', createWindow)
app.on('window-all-closed', function () {
app.quit()
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment