@@ -112,6 +112,7 @@ Electron.app.on('ready', function() {
112
112
cookieResponse = null ,
113
113
screenshotResponse = null ,
114
114
windowWillUnload = false ,
115
+ /** @type {Object.<string, string> } */
115
116
windowIdNameMap = { } ,
116
117
captureResponse = false ,
117
118
bindServerOnce ;
@@ -125,6 +126,7 @@ Electron.app.on('ready', function() {
125
126
*/
126
127
global . setExecutionError = function ( error ) {
127
128
Logger . error ( 'Script evaluation failed internally: %s' , errorToString ( error ) ) ;
129
+
128
130
executeResponse = { 'error' : errorToString ( error ) } ;
129
131
} ;
130
132
@@ -147,26 +149,36 @@ Electron.app.on('ready', function() {
147
149
148
150
/**
149
151
*
150
- * @param {integer } id
151
- * @param {string } name
152
- * @param {string } url
152
+ * @param {Number } id
153
+ * @param {String } name
154
+ * @param {String } url
153
155
*/
154
156
global . setWindowIdName = function ( id , name , url ) {
155
157
const sId = id === null ? "" : id . toString ( ) ;
156
158
157
159
if ( name === '' ) {
158
- name = 'frame- ' + sId ;
160
+ name = 'electron_window_ ' + sId ;
159
161
}
160
162
161
163
if ( name === null ) {
162
- Logger . info ( 'Unlinked window named "%s" from id "%s" for %s .' , name , sId , url ) ;
164
+ Logger . info ( 'Unlinked window named %j from id %j for %j .' , name , sId , url ) ;
163
165
if ( windowIdNameMap [ sId ] ) delete windowIdNameMap [ sId ] ;
164
166
} else {
165
- Logger . info ( 'Linked window named "%s" with id "%s" for %s .' , name , sId , url ) ;
167
+ Logger . info ( 'Linked window named %j with id %j for %j .' , name , sId , url ) ;
166
168
windowIdNameMap [ sId ] = name ;
167
169
}
168
170
} ;
169
171
172
+ /**
173
+ *
174
+ * @param {Number } id
175
+ */
176
+ global . getWindowNameFromId = function ( id ) {
177
+ const sId = id === null ? "" : id . toString ( ) ;
178
+
179
+ return windowIdNameMap [ sId ] ;
180
+ } ;
181
+
170
182
/**
171
183
* Finds window by its window name. Note that this depends on the windows successfully registering it's id and name
172
184
* when created. Since we keep these details in a hash map, we need to be careful about keeping it up to date.
@@ -181,7 +193,7 @@ Electron.app.on('ready', function() {
181
193
}
182
194
183
195
for ( let id in windowIdNameMap ) {
184
- if ( windowIdNameMap [ id ] === name ) {
196
+ if ( windowIdNameMap . hasOwnProperty ( id ) && windowIdNameMap [ id ] === name ) {
185
197
const wnd = BrowserWindow . fromId ( parseInt ( id ) ) ;
186
198
if ( wnd ) result . push ( wnd ) ;
187
199
}
@@ -339,6 +351,25 @@ Electron.app.on('ready', function() {
339
351
* @param {Electron.BrowserWindow } window
340
352
*/
341
353
function ( event , window ) {
354
+ const windowId = window . id ;
355
+
356
+ Logger . debug ( 'Browser window created with id %j.' , windowId ) ;
357
+
358
+ window
359
+ . on ( 'closed' , function ( ) { // important: we can't use window anymore in here!
360
+ if ( windowId ) {
361
+ Logger . info ( 'Window "%s" (id %d) has been closed.' , windowIdNameMap [ windowId . toString ( ) ] || '' , windowId ) ;
362
+
363
+ pageVisited = true ;
364
+ captureResponse = false ;
365
+ delete windowIdNameMap [ windowId . toString ( ) ] ;
366
+ ResponseManager . remove ( windowId ) ;
367
+ } else {
368
+ Logger . warn ( 'Browser window with id %j was closed.' , windowId ) ;
369
+ }
370
+ } )
371
+ ;
372
+
342
373
window . webContents
343
374
. on ( 'login' , function ( event , request , authInfo , callback ) {
344
375
if ( auth . user !== false ) {
@@ -352,10 +383,9 @@ Electron.app.on('ready', function() {
352
383
global . newWindowName = frameName ;
353
384
setupWindowOptions ( options ) ;
354
385
} )
355
- . on ( 'closed' , function ( ) {
356
- Logger . info ( 'Window "%s" (id %d) has been closed.' , windowIdNameMap [ window . id ] || '' , window . id ) ;
357
- delete windowIdNameMap [ window . id ] ;
358
- ResponseManager . remove ( window . id ) ;
386
+ . on ( 'will-navigate' , function ( event , url ) {
387
+ Logger . debug ( 'Event "will-navigate" triggered for url %j.' , url ) ;
388
+ global . setWindowUnloading ( true ) ;
359
389
} )
360
390
. on ( 'did-finish-load' , function ( ) {
361
391
if ( bindServerOnce ) {
0 commit comments