Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron - How to know when renderer window is ready

Tags:

electron

In my main process I create a renderer window:

var mainWindow = new BrowserWindow({     height: 600,     width: 800,     x: 0,     y: 0,     frame: false,     resizable: true }); mainWindow.openDevTools(); mainWindow.loadURL('file://' + __dirname + '/renderer/index.html'); 

Then I want to communicate with it in some way:

mainWindow.webContents.send('message', 'hello world'); 

However the main window doesn't receive this message because it isn't fully done being created at the time I attempt to send it.

I have temporarily solved this by wrapping the latter code in a setTimeout() but that is most definitely not the right way to resolve a race condition.

Is there a callback for when the main window is ready? I tried the 'ready-to-show' event mentioned in the docs but it did not work.

like image 266
Joey Avatar asked Feb 16 '17 21:02

Joey


People also ask

How do you close the Electron window?

getElementById("close-btn"). addEventListener("click", function (e) { var window = remote. getCurrentWindow(); window. close(); });

How do you destroy a BrowserWindow in Electron?

For electron's BrowserWindow you can use isDestroyed() method as well, which potentially makes the use of 'closed' unnecessary but invalidating objects is a general technique while destroy queries are always up to the API.

What is Electron kiosk mode?

Kiosk mode is a common way to lock down a Windows device when that device is used for a specific task or used in a public setting. So in electron kiosk mode, we'd have the ability to lock down our application to a point that users are restricted to the actions that we want them to perform.


2 Answers

A listener on "mainWindow" doesn't worked for me. I used instead "mainWindow.webContents".

mainWindow.webContents.once('dom-ready', () => {}); 
like image 125
Besa Avatar answered Sep 28 '22 03:09

Besa


Have a look at the did-finish-load event mentioned in the Electron browser-window documentation.

mainWindow.once('did-finish-load', () => {    // Send Message }) 

There seems to be a dom-ready event too.

like image 38
Michael Avatar answered Sep 28 '22 02:09

Michael