Skip to content

Commit

Permalink
Merge pull request #14 from withuno/smithki/visible-login-targets
Browse files Browse the repository at this point in the history
add `getVisibleLoginTarget` function
  • Loading branch information
smithki authored Mar 21, 2023
2 parents 895f2ae + e184435 commit a767e5c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import { getSharedObserver } from './unload-observer';
getSharedObserver();

export { LoginTarget, LoginTargetType } from './login/login-target';
export { getLoginTarget, getLoginTargets } from './login';
export { getVisibleLoginTarget, getLoginTarget, getLoginTargets } from './login';
23 changes: 23 additions & 0 deletions src/login/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
/* eslint-disable prefer-destructuring */

import isVisible from 'is-visible';

import { findFormsWithInputs } from './login-inputs';
import { LoginTarget } from './login-target';
import { revealShySubmitButtons } from './reveal-shy-submit-buttons';

/**
* Get the best login (visible) target on the current page.
*
* @param queryEl The element to query within
* @returns A login target or `null` if none found
* @see getLoginTarget
*/
export function getVisibleLoginTarget(queryEl: Document | HTMLElement = document) {
const bestTarget = getLoginTarget(queryEl);
const isTargetVisible = [
bestTarget?.form,
bestTarget?.usernameField,
bestTarget?.passwordField,
bestTarget?.submitButton,
].some((el) => {
console.log('isVisible', !!el && isVisible(el), el);
return !!el && isVisible(el);
});
return isTargetVisible ? bestTarget : null;
}

/**
* Get the best login target on the current page.
*
Expand Down
4 changes: 2 additions & 2 deletions test/integration/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ async function executeOnDemandTest(url: string, page: Page) {
// If no login targets are found after 15s, we give up.
setTimeout(() => {
clearInterval(interval);
reject(new Error('No login targets found after 15s'));
}, 15000);
reject(new Error('No login targets found after 30s'));
}, 30_000);
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/integration/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function waitForPageLoad(page: Page) {
}

// Image hasn’t loaded yet, add an event listener to know when it does...
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
img.addEventListener('load', resolve);
// We don't mind if an image fails to load...
img.addEventListener('error', resolve);
Expand Down

0 comments on commit a767e5c

Please sign in to comment.