@@ -293,6 +293,27 @@ async function dispatchFocusOrSubmit(settings, request, allFrames, allowForeign)
293
293
} catch ( e ) { }
294
294
}
295
295
296
+ /**
297
+ * Inject script
298
+ *
299
+ * @param object settings Settings object
300
+ * @param boolean allFrames Inject in all frames
301
+ * @return object Cancellable promise
302
+ */
303
+ async function injectScript ( settings , allFrames ) {
304
+ const MAX_WAIT = 1000 ;
305
+
306
+ return new Promise ( async ( resolve , reject ) => {
307
+ const waitTimeout = setTimeout ( reject , MAX_WAIT ) ;
308
+ await chrome . tabs . executeScript ( settings . tab . id , {
309
+ allFrames : allFrames ,
310
+ file : "js/inject.dist.js"
311
+ } ) ;
312
+ clearTimeout ( waitTimeout ) ;
313
+ resolve ( true ) ;
314
+ } ) ;
315
+ }
316
+
296
317
/**
297
318
* Fill form fields
298
319
*
@@ -303,10 +324,19 @@ async function dispatchFocusOrSubmit(settings, request, allFrames, allowForeign)
303
324
*/
304
325
async function fillFields ( settings , login , fields ) {
305
326
// inject script
306
- await chrome . tabs . executeScript ( settings . tab . id , {
307
- allFrames : true ,
308
- file : "js/inject.dist.js"
309
- } ) ;
327
+ try {
328
+ await injectScript ( settings , false ) ;
329
+ } catch {
330
+ throw new Error ( "Unable to inject script in the top frame" ) ;
331
+ }
332
+
333
+ let injectedAllFrames = false ;
334
+ try {
335
+ await injectScript ( settings , true ) ;
336
+ injectedAllFrames = true ;
337
+ } catch {
338
+ // we'll proceed with trying to fill only the top frame
339
+ }
310
340
311
341
// build fill request
312
342
var fillRequest = {
@@ -326,43 +356,68 @@ async function fillFields(settings, login, fields) {
326
356
await dispatchFill ( settings , fillRequest , allFrames , allowForeign , allowNoSecret )
327
357
) ;
328
358
329
- // try again using same-origin frames if we couldn't fill an "important" field
330
- if ( ! filledFields . includes ( importantFieldToFill ) ) {
331
- allFrames = true ;
332
- filledFields = filledFields . concat (
333
- await dispatchFill ( settings , fillRequest , allFrames , allowForeign , allowNoSecret )
334
- ) ;
335
- }
359
+ if ( injectedAllFrames ) {
360
+ // try again using same-origin frames if we couldn't fill an "important" field
361
+ if ( ! filledFields . includes ( importantFieldToFill ) ) {
362
+ allFrames = true ;
363
+ filledFields = filledFields . concat (
364
+ await dispatchFill ( settings , fillRequest , allFrames , allowForeign , allowNoSecret )
365
+ ) ;
366
+ }
336
367
337
- // try again using all available frames if we couldn't fill an "important" field
338
- if (
339
- ! filledFields . includes ( importantFieldToFill ) &&
340
- settings . foreignFills [ settings . host ] !== false
341
- ) {
342
- allowForeign = true ;
343
- filledFields = filledFields . concat (
344
- await dispatchFill ( settings , fillRequest , allFrames , allowForeign , allowNoSecret )
345
- ) ;
368
+ // try again using all available frames if we couldn't fill an "important" field
369
+ if (
370
+ ! filledFields . includes ( importantFieldToFill ) &&
371
+ settings . foreignFills [ settings . host ] !== false
372
+ ) {
373
+ allowForeign = true ;
374
+ filledFields = filledFields . concat (
375
+ await dispatchFill ( settings , fillRequest , allFrames , allowForeign , allowNoSecret )
376
+ ) ;
377
+ }
346
378
}
347
379
348
380
// try again, but don't require a password field (if it was required until now)
349
381
if ( ! allowNoSecret ) {
350
382
allowNoSecret = true ;
351
383
352
- // try again using same-origin frames
384
+ // try again using only the top frame
353
385
if ( ! filledFields . length ) {
386
+ allFrames = false ;
354
387
allowForeign = false ;
355
388
filledFields = filledFields . concat (
356
389
await dispatchFill ( settings , fillRequest , allFrames , allowForeign , allowNoSecret )
357
390
) ;
358
391
}
359
392
360
- // try again using all available frames
361
- if ( ! filledFields . length && settings . foreignFills [ settings . host ] !== false ) {
362
- allowForeign = true ;
363
- filledFields = filledFields . concat (
364
- await dispatchFill ( settings , fillRequest , allFrames , allowForeign , allowNoSecret )
365
- ) ;
393
+ if ( injectedAllFrames ) {
394
+ // try again using same-origin frames
395
+ if ( ! filledFields . length ) {
396
+ allFrames = true ;
397
+ filledFields = filledFields . concat (
398
+ await dispatchFill (
399
+ settings ,
400
+ fillRequest ,
401
+ allFrames ,
402
+ allowForeign ,
403
+ allowNoSecret
404
+ )
405
+ ) ;
406
+ }
407
+
408
+ // try again using all available frames
409
+ if ( ! filledFields . length && settings . foreignFills [ settings . host ] !== false ) {
410
+ allowForeign = true ;
411
+ filledFields = filledFields . concat (
412
+ await dispatchFill (
413
+ settings ,
414
+ fillRequest ,
415
+ allFrames ,
416
+ allowForeign ,
417
+ allowNoSecret
418
+ )
419
+ ) ;
420
+ }
366
421
}
367
422
}
368
423
0 commit comments