@@ -228,27 +228,35 @@ class AlwaysOnTop extends EventEmitter {
228228 */
229229 _onMessageReceived ( event , { type, data = { } } ) {
230230 if ( type === 'event' && data . name === 'new-window' ) {
231- this . _alwaysOnTopBrowserWindow
232- = remote . BrowserWindow . fromId ( data . id ) ;
231+ this . _onNewAlwaysOnTopBrowserWindow ( data . id ) ;
233232 }
234233 }
235234
236235 /**
237- * Creates and opens the always on top window .
236+ * Handles 'new-window' always on top events .
238237 *
238+ * @param {number } windowId - The id of the BrowserWindow instance.
239239 * @returns {void }
240240 */
241- _openAlwaysOnTopWindow ( ) {
242- if ( this . _alwaysOnTopWindow ) {
243- return ;
241+ _onNewAlwaysOnTopBrowserWindow ( windowId ) {
242+ this . _alwaysOnTopBrowserWindow = remote . BrowserWindow . fromId ( windowId ) ;
243+ const { webContents } = this . _alwaysOnTopBrowserWindow ;
244+ // if the window is still loading we may end up loosing the injected content when load finishes. We need to wait
245+ // for the loading to be completed. We are using the browser windows events instead of the DOM window ones because
246+ // it appears they are unreliable (readyState is always completed, most of the events are not fired!!!)
247+ if ( webContents . isLoading ( ) ) {
248+ webContents . on ( 'did-stop-loading' , ( ) => this . _setupAlwaysOnTopWindow ( ) ) ;
249+ } else {
250+ this . _setupAlwaysOnTopWindow ( ) ;
244251 }
245- ipcRenderer . on ( 'jitsi-always-on-top' , this . _onMessageReceived ) ;
246- this . _api . on ( 'largeVideoChanged' , this . _updateLargeVideoSrc ) ;
252+ }
247253
248- // Intentionally open about:blank. Otherwise if an origin is set, a
249- // cross-origin redirect can cause any set global variables to be blown
250- // away.
251- this . _alwaysOnTopWindow = window . open ( '' , 'AlwaysOnTop' ) ;
254+ /**
255+ * Sets all necessary content (HTML, CSS, JS) to the always on top window.
256+ *
257+ * @returns {void }
258+ */
259+ _setupAlwaysOnTopWindow ( ) {
252260 if ( ! this . _alwaysOnTopWindow ) {
253261 return ;
254262 }
@@ -304,19 +312,13 @@ class AlwaysOnTop extends EventEmitter {
304312 }
305313 } ;
306314
307- // Change the dom of 'about:blank' to display the always on top content.
308- this . _alwaysOnTopWindow . onload = ( ) => {
309- if ( ! this . _alwaysOnTopWindow ) {
310- return ;
311- }
312-
313315 const cssPath = path . join ( __dirname , './alwaysontop.css' ) ;
314316 const jsPath = path . join ( __dirname , './alwaysontop.js' ) ;
315317
316318 // Add the markup for the JS to manipulate and load the CSS.
317319 this . _alwaysOnTopWindow . document . body . innerHTML = `
318320 <div id="react"></div>
319- <video autoplay="" id="video" style="transform: none;"></video>
321+ <video autoplay="" id="video" style="transform: none;" muted ></video>
320322 <link rel="stylesheet" href="file://${ cssPath } ">
321323 ` ;
322324
@@ -327,7 +329,24 @@ class AlwaysOnTop extends EventEmitter {
327329
328330 scriptTag . setAttribute ( 'src' , `file://${ jsPath } ` ) ;
329331 this . _alwaysOnTopWindow . document . head . appendChild ( scriptTag ) ;
330- } ;
332+ }
333+
334+ /**
335+ * Creates and opens the always on top window.
336+ *
337+ * @returns {void }
338+ */
339+ _openAlwaysOnTopWindow ( ) {
340+ if ( this . _alwaysOnTopWindow ) {
341+ return ;
342+ }
343+ ipcRenderer . on ( 'jitsi-always-on-top' , this . _onMessageReceived ) ;
344+ this . _api . on ( 'largeVideoChanged' , this . _updateLargeVideoSrc ) ;
345+
346+ // Intentionally open about:blank. Otherwise if an origin is set, a
347+ // cross-origin redirect can cause any set global variables to be blown
348+ // away.
349+ this . _alwaysOnTopWindow = window . open ( '' , 'AlwaysOnTop' ) ;
331350 }
332351
333352 /**
@@ -353,8 +372,7 @@ class AlwaysOnTop extends EventEmitter {
353372 this . _alwaysOnTopWindow . close ( ) ;
354373 }
355374
356- ipcRenderer . removeListener ( 'jitsi-always-on-top' ,
357- this . _onMessageReceived ) ;
375+ ipcRenderer . removeListener ( 'jitsi-always-on-top' , this . _onMessageReceived ) ;
358376 }
359377
360378 //we need to tell the main process to close the BrowserWindow because when
0 commit comments