@thetypefounders/continue-with-google 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -2
- package/dist/index.js +19 -12
- package/package.json +1 -1
- package/src/index.ts +21 -18
package/dist/index.d.ts
CHANGED
|
@@ -6,10 +6,9 @@ export interface Logger {
|
|
|
6
6
|
export type Options = {
|
|
7
7
|
challengeCount?: number;
|
|
8
8
|
challengeTimeoutSeconds?: number;
|
|
9
|
-
challengeScreenshot?: boolean;
|
|
10
9
|
trialCount?: number;
|
|
11
10
|
trialTimeoutSeconds?: number;
|
|
12
|
-
|
|
11
|
+
screenshot?: boolean;
|
|
13
12
|
waitForSelector?: WaitForSelectorOptions;
|
|
14
13
|
};
|
|
15
14
|
export declare function authenticate(page: Page, email: string, password: string, secret: string, selector: string, options?: Options, logger?: Logger): Promise<ElementHandle | null>;
|
package/dist/index.js
CHANGED
|
@@ -3,31 +3,36 @@ import { setTimeout } from 'node:timers/promises';
|
|
|
3
3
|
const DEFAULTS = {
|
|
4
4
|
challengeCount: 3,
|
|
5
5
|
challengeTimeoutSeconds: 30,
|
|
6
|
-
challengeScreenshot: false,
|
|
7
6
|
trialCount: 10,
|
|
8
7
|
trialTimeoutSeconds: 2,
|
|
9
|
-
trialScreenshot: false,
|
|
10
8
|
};
|
|
11
9
|
export async function authenticate(page, email, password, secret, selector, options = DEFAULTS, logger = console) {
|
|
10
|
+
const mergedOptions = { ...DEFAULTS, ...options };
|
|
12
11
|
logger.info('Waiting to enter the email...');
|
|
12
|
+
if (mergedOptions.screenshot)
|
|
13
|
+
await takeScreenshotToDisplay(page);
|
|
13
14
|
await page.waitForSelector('input[type=email]', { visible: true });
|
|
14
15
|
logger.info('Entering the email...');
|
|
16
|
+
if (mergedOptions.screenshot)
|
|
17
|
+
await takeScreenshotToDisplay(page);
|
|
15
18
|
await page.type('input[type=email]', email);
|
|
16
19
|
await page.keyboard.press('Enter');
|
|
17
20
|
logger.info('Waiting to enter the password...');
|
|
21
|
+
if (mergedOptions.screenshot)
|
|
22
|
+
await takeScreenshotToDisplay(page);
|
|
18
23
|
await page.waitForSelector('input[type=password]', { visible: true });
|
|
19
24
|
logger.info('Entering the password...');
|
|
25
|
+
if (mergedOptions.screenshot)
|
|
26
|
+
await takeScreenshotToDisplay(page);
|
|
20
27
|
await page.type('input[type=password]', password);
|
|
21
28
|
await page.keyboard.press('Enter');
|
|
22
|
-
for (let attempt = 0, found = false; attempt <
|
|
29
|
+
for (let attempt = 0, found = false; attempt < mergedOptions.challengeCount && !found; attempt++) {
|
|
23
30
|
if (attempt > 0) {
|
|
24
31
|
logger.warn(`Challenged on attempt ${attempt}. Entering the code...`);
|
|
25
|
-
if (
|
|
32
|
+
if (mergedOptions.screenshot)
|
|
26
33
|
await takeScreenshotToDisplay(page);
|
|
27
34
|
if (attempt > 1) {
|
|
28
|
-
await setTimeout(1000 *
|
|
29
|
-
(options.challengeTimeoutSeconds ||
|
|
30
|
-
DEFAULTS.challengeTimeoutSeconds));
|
|
35
|
+
await setTimeout(1000 * mergedOptions.challengeTimeoutSeconds);
|
|
31
36
|
}
|
|
32
37
|
const code = generateToken(secret);
|
|
33
38
|
await page.evaluate(() => {
|
|
@@ -36,10 +41,12 @@ export async function authenticate(page, email, password, secret, selector, opti
|
|
|
36
41
|
});
|
|
37
42
|
await page.type('input[type=tel]', code);
|
|
38
43
|
await page.keyboard.press('Enter');
|
|
39
|
-
await waitForTrial(page,
|
|
44
|
+
await waitForTrial(page, mergedOptions.trialCount, mergedOptions.trialTimeoutSeconds, mergedOptions.screenshot, logger);
|
|
40
45
|
}
|
|
41
46
|
found = await Promise.any([
|
|
42
|
-
page
|
|
47
|
+
page
|
|
48
|
+
.waitForSelector(selector, mergedOptions.waitForSelector)
|
|
49
|
+
.then(() => true),
|
|
43
50
|
page
|
|
44
51
|
.waitForSelector('input[type=tel]', { visible: true })
|
|
45
52
|
.then(() => false),
|
|
@@ -47,11 +54,11 @@ export async function authenticate(page, email, password, secret, selector, opti
|
|
|
47
54
|
}
|
|
48
55
|
return await page.$(selector);
|
|
49
56
|
}
|
|
50
|
-
async function waitForTrial(page, attemptCount, attemptTimeoutSeconds,
|
|
57
|
+
async function waitForTrial(page, attemptCount, attemptTimeoutSeconds, screenshot, logger) {
|
|
51
58
|
for (let attempt = -1, previous = undefined, current = undefined; attempt < attemptCount && (current === undefined || previous !== current); attempt++) {
|
|
52
59
|
if (attempt > 0) {
|
|
53
60
|
logger.warn(`Tried on attempt ${attempt}. Waiting to finish...`);
|
|
54
|
-
if (
|
|
61
|
+
if (screenshot)
|
|
55
62
|
await takeScreenshotToDisplay(page);
|
|
56
63
|
}
|
|
57
64
|
if (attempt > -1) {
|
|
@@ -76,6 +83,6 @@ async function takeScreenshotToCompare(page) {
|
|
|
76
83
|
}
|
|
77
84
|
async function takeScreenshotToDisplay(page) {
|
|
78
85
|
await page.screenshot({
|
|
79
|
-
path: `continue-with-google-${
|
|
86
|
+
path: `continue-with-google-${Date.now()}.png`,
|
|
80
87
|
});
|
|
81
88
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -10,20 +10,17 @@ export interface Logger {
|
|
|
10
10
|
export type Options = {
|
|
11
11
|
challengeCount?: number;
|
|
12
12
|
challengeTimeoutSeconds?: number;
|
|
13
|
-
challengeScreenshot?: boolean;
|
|
14
13
|
trialCount?: number;
|
|
15
14
|
trialTimeoutSeconds?: number;
|
|
16
|
-
|
|
15
|
+
screenshot?: boolean;
|
|
17
16
|
waitForSelector?: WaitForSelectorOptions;
|
|
18
17
|
};
|
|
19
18
|
|
|
20
19
|
const DEFAULTS: Options = {
|
|
21
20
|
challengeCount: 3,
|
|
22
21
|
challengeTimeoutSeconds: 30,
|
|
23
|
-
challengeScreenshot: false,
|
|
24
22
|
trialCount: 10,
|
|
25
23
|
trialTimeoutSeconds: 2,
|
|
26
|
-
trialScreenshot: false,
|
|
27
24
|
};
|
|
28
25
|
|
|
29
26
|
export async function authenticate(
|
|
@@ -35,32 +32,36 @@ export async function authenticate(
|
|
|
35
32
|
options: Options = DEFAULTS,
|
|
36
33
|
logger: Logger = console
|
|
37
34
|
): Promise<ElementHandle | null> {
|
|
35
|
+
const mergedOptions = { ...DEFAULTS, ...options };
|
|
36
|
+
|
|
38
37
|
logger.info('Waiting to enter the email...');
|
|
38
|
+
if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
|
|
39
39
|
await page.waitForSelector('input[type=email]', { visible: true });
|
|
40
|
+
|
|
40
41
|
logger.info('Entering the email...');
|
|
42
|
+
if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
|
|
41
43
|
await page.type('input[type=email]', email);
|
|
42
44
|
await page.keyboard.press('Enter');
|
|
43
45
|
|
|
44
46
|
logger.info('Waiting to enter the password...');
|
|
47
|
+
if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
|
|
45
48
|
await page.waitForSelector('input[type=password]', { visible: true });
|
|
49
|
+
|
|
46
50
|
logger.info('Entering the password...');
|
|
51
|
+
if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
|
|
47
52
|
await page.type('input[type=password]', password);
|
|
48
53
|
await page.keyboard.press('Enter');
|
|
49
54
|
|
|
50
55
|
for (
|
|
51
56
|
let attempt = 0, found = false;
|
|
52
|
-
attempt <
|
|
57
|
+
attempt < mergedOptions.challengeCount! && !found;
|
|
53
58
|
attempt++
|
|
54
59
|
) {
|
|
55
60
|
if (attempt > 0) {
|
|
56
61
|
logger.warn(`Challenged on attempt ${attempt}. Entering the code...`);
|
|
57
|
-
if (
|
|
62
|
+
if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
|
|
58
63
|
if (attempt > 1) {
|
|
59
|
-
await setTimeout(
|
|
60
|
-
1000 *
|
|
61
|
-
(options.challengeTimeoutSeconds ||
|
|
62
|
-
DEFAULTS.challengeTimeoutSeconds!)
|
|
63
|
-
);
|
|
64
|
+
await setTimeout(1000 * mergedOptions.challengeTimeoutSeconds!);
|
|
64
65
|
}
|
|
65
66
|
const code = generateToken(secret);
|
|
66
67
|
await page.evaluate(() => {
|
|
@@ -71,14 +72,16 @@ export async function authenticate(
|
|
|
71
72
|
await page.keyboard.press('Enter');
|
|
72
73
|
await waitForTrial(
|
|
73
74
|
page,
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
mergedOptions.trialCount!,
|
|
76
|
+
mergedOptions.trialTimeoutSeconds!,
|
|
77
|
+
mergedOptions.screenshot,
|
|
77
78
|
logger
|
|
78
79
|
);
|
|
79
80
|
}
|
|
80
81
|
found = await Promise.any([
|
|
81
|
-
page
|
|
82
|
+
page
|
|
83
|
+
.waitForSelector(selector, mergedOptions.waitForSelector)
|
|
84
|
+
.then(() => true),
|
|
82
85
|
page
|
|
83
86
|
.waitForSelector('input[type=tel]', { visible: true })
|
|
84
87
|
.then(() => false),
|
|
@@ -92,7 +95,7 @@ async function waitForTrial(
|
|
|
92
95
|
page: Page,
|
|
93
96
|
attemptCount: number,
|
|
94
97
|
attemptTimeoutSeconds: number,
|
|
95
|
-
|
|
98
|
+
screenshot: boolean | undefined,
|
|
96
99
|
logger: Logger
|
|
97
100
|
): Promise<void> {
|
|
98
101
|
for (
|
|
@@ -102,7 +105,7 @@ async function waitForTrial(
|
|
|
102
105
|
) {
|
|
103
106
|
if (attempt > 0) {
|
|
104
107
|
logger.warn(`Tried on attempt ${attempt}. Waiting to finish...`);
|
|
105
|
-
if (
|
|
108
|
+
if (screenshot) await takeScreenshotToDisplay(page);
|
|
106
109
|
}
|
|
107
110
|
if (attempt > -1) {
|
|
108
111
|
await setTimeout(1000 * attemptTimeoutSeconds);
|
|
@@ -129,6 +132,6 @@ async function takeScreenshotToCompare(
|
|
|
129
132
|
|
|
130
133
|
async function takeScreenshotToDisplay(page: Page): Promise<void> {
|
|
131
134
|
await page.screenshot({
|
|
132
|
-
path: `continue-with-google-${
|
|
135
|
+
path: `continue-with-google-${Date.now()}.png`,
|
|
133
136
|
});
|
|
134
137
|
}
|