Skip to content

Commit f030e4c

Browse files
authored
Handle inability to inject script to all frames (#126)
1 parent 3d487e5 commit f030e4c

File tree

1 file changed

+82
-27
lines changed

1 file changed

+82
-27
lines changed

src/background.js

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,27 @@ async function dispatchFocusOrSubmit(settings, request, allFrames, allowForeign)
293293
} catch (e) {}
294294
}
295295

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+
296317
/**
297318
* Fill form fields
298319
*
@@ -303,10 +324,19 @@ async function dispatchFocusOrSubmit(settings, request, allFrames, allowForeign)
303324
*/
304325
async function fillFields(settings, login, fields) {
305326
// 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+
}
310340

311341
// build fill request
312342
var fillRequest = {
@@ -326,43 +356,68 @@ async function fillFields(settings, login, fields) {
326356
await dispatchFill(settings, fillRequest, allFrames, allowForeign, allowNoSecret)
327357
);
328358

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+
}
336367

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+
}
346378
}
347379

348380
// try again, but don't require a password field (if it was required until now)
349381
if (!allowNoSecret) {
350382
allowNoSecret = true;
351383

352-
// try again using same-origin frames
384+
// try again using only the top frame
353385
if (!filledFields.length) {
386+
allFrames = false;
354387
allowForeign = false;
355388
filledFields = filledFields.concat(
356389
await dispatchFill(settings, fillRequest, allFrames, allowForeign, allowNoSecret)
357390
);
358391
}
359392

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+
}
366421
}
367422
}
368423

0 commit comments

Comments
 (0)