@thetypefounders/continue-with-google 1.3.1 → 1.3.2

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 CHANGED
@@ -8,7 +8,7 @@ export type Options = {
8
8
  challengeTimeoutSeconds?: number;
9
9
  trialCount?: number;
10
10
  trialTimeoutSeconds?: number;
11
- screenshot?: boolean;
11
+ screenshot?: boolean | string;
12
12
  waitForSelector?: WaitForSelectorOptions;
13
13
  };
14
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
@@ -9,28 +9,23 @@ const DEFAULTS = {
9
9
  export async function authenticate(page, email, password, secret, selector, options = DEFAULTS, logger = console) {
10
10
  const mergedOptions = { ...DEFAULTS, ...options };
11
11
  logger.info('Waiting to enter the email...');
12
- if (mergedOptions.screenshot)
13
- await takeScreenshotToDisplay(page);
12
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
14
13
  await page.waitForSelector('input[type=email]', { visible: true });
15
14
  logger.info('Entering the email...');
16
- if (mergedOptions.screenshot)
17
- await takeScreenshotToDisplay(page);
15
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
18
16
  await page.type('input[type=email]', email);
19
17
  await page.keyboard.press('Enter');
20
18
  logger.info('Waiting to enter the password...');
21
- if (mergedOptions.screenshot)
22
- await takeScreenshotToDisplay(page);
19
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
23
20
  await page.waitForSelector('input[type=password]', { visible: true });
24
21
  logger.info('Entering the password...');
25
- if (mergedOptions.screenshot)
26
- await takeScreenshotToDisplay(page);
22
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
27
23
  await page.type('input[type=password]', password);
28
24
  await page.keyboard.press('Enter');
29
25
  for (let attempt = 0, found = false; attempt < mergedOptions.challengeCount && !found; attempt++) {
30
26
  if (attempt > 0) {
31
27
  logger.warn(`Challenged on attempt ${attempt}. Entering the code...`);
32
- if (mergedOptions.screenshot)
33
- await takeScreenshotToDisplay(page);
28
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
34
29
  if (attempt > 1) {
35
30
  await setTimeout(1000 * mergedOptions.challengeTimeoutSeconds);
36
31
  }
@@ -58,8 +53,7 @@ async function waitForTrial(page, attemptCount, attemptTimeoutSeconds, screensho
58
53
  for (let attempt = -1, previous = undefined, current = undefined; attempt < attemptCount && (current === undefined || previous !== current); attempt++) {
59
54
  if (attempt > 0) {
60
55
  logger.warn(`Tried on attempt ${attempt}. Waiting to finish...`);
61
- if (screenshot)
62
- await takeScreenshotToDisplay(page);
56
+ await takeScreenshotToDisplay(page, screenshot, logger);
63
57
  }
64
58
  if (attempt > -1) {
65
59
  await setTimeout(1000 * attemptTimeoutSeconds);
@@ -81,8 +75,15 @@ async function takeScreenshotToCompare(page) {
81
75
  return undefined;
82
76
  }
83
77
  }
84
- async function takeScreenshotToDisplay(page) {
85
- await page.screenshot({
86
- path: `continue-with-google-${Date.now()}.png`,
87
- });
78
+ async function takeScreenshotToDisplay(page, mode, logger) {
79
+ const timestamp = new Date(Date.now()).toISOString();
80
+ if (mode === 'log') {
81
+ const content = await page.evaluate(() => document.body.innerText);
82
+ logger.info(`${timestamp}:\n${content}\n`);
83
+ }
84
+ else if (mode) {
85
+ const path = `continue-with-google-${timestamp.replace(':', '-')}.png`;
86
+ // @ts-ignore
87
+ await page.screenshot({ path });
88
+ }
88
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thetypefounders/continue-with-google",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "license": "Apache-2.0",
5
5
  "author": "Ivan Ukhov <ivan.ukhov@gmail.com>",
6
6
  "description": "Two-factor authentication with Google via Puppeteer",
package/src/index.ts CHANGED
@@ -12,7 +12,7 @@ export type Options = {
12
12
  challengeTimeoutSeconds?: number;
13
13
  trialCount?: number;
14
14
  trialTimeoutSeconds?: number;
15
- screenshot?: boolean;
15
+ screenshot?: boolean | string;
16
16
  waitForSelector?: WaitForSelectorOptions;
17
17
  };
18
18
 
@@ -35,20 +35,20 @@ export async function authenticate(
35
35
  const mergedOptions = { ...DEFAULTS, ...options };
36
36
 
37
37
  logger.info('Waiting to enter the email...');
38
- if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
38
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
39
39
  await page.waitForSelector('input[type=email]', { visible: true });
40
40
 
41
41
  logger.info('Entering the email...');
42
- if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
42
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
43
43
  await page.type('input[type=email]', email);
44
44
  await page.keyboard.press('Enter');
45
45
 
46
46
  logger.info('Waiting to enter the password...');
47
- if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
47
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
48
48
  await page.waitForSelector('input[type=password]', { visible: true });
49
49
 
50
50
  logger.info('Entering the password...');
51
- if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
51
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
52
52
  await page.type('input[type=password]', password);
53
53
  await page.keyboard.press('Enter');
54
54
 
@@ -59,7 +59,7 @@ export async function authenticate(
59
59
  ) {
60
60
  if (attempt > 0) {
61
61
  logger.warn(`Challenged on attempt ${attempt}. Entering the code...`);
62
- if (mergedOptions.screenshot) await takeScreenshotToDisplay(page);
62
+ await takeScreenshotToDisplay(page, mergedOptions.screenshot, logger);
63
63
  if (attempt > 1) {
64
64
  await setTimeout(1000 * mergedOptions.challengeTimeoutSeconds!);
65
65
  }
@@ -95,7 +95,7 @@ async function waitForTrial(
95
95
  page: Page,
96
96
  attemptCount: number,
97
97
  attemptTimeoutSeconds: number,
98
- screenshot: boolean | undefined,
98
+ screenshot: boolean | string | undefined,
99
99
  logger: Logger
100
100
  ): Promise<void> {
101
101
  for (
@@ -105,7 +105,7 @@ async function waitForTrial(
105
105
  ) {
106
106
  if (attempt > 0) {
107
107
  logger.warn(`Tried on attempt ${attempt}. Waiting to finish...`);
108
- if (screenshot) await takeScreenshotToDisplay(page);
108
+ await takeScreenshotToDisplay(page, screenshot, logger);
109
109
  }
110
110
  if (attempt > -1) {
111
111
  await setTimeout(1000 * attemptTimeoutSeconds);
@@ -130,8 +130,18 @@ async function takeScreenshotToCompare(
130
130
  }
131
131
  }
132
132
 
133
- async function takeScreenshotToDisplay(page: Page): Promise<void> {
134
- await page.screenshot({
135
- path: `continue-with-google-${Date.now()}.png`,
136
- });
133
+ async function takeScreenshotToDisplay(
134
+ page: Page,
135
+ mode: boolean | string | undefined,
136
+ logger: Logger
137
+ ): Promise<void> {
138
+ const timestamp = new Date(Date.now()).toISOString();
139
+ if (mode === 'log') {
140
+ const content = await page.evaluate(() => document.body.innerText);
141
+ logger.info(`${timestamp}:\n${content}\n`);
142
+ } else if (mode) {
143
+ const path = `continue-with-google-${timestamp.replace(':', '-')}.png`;
144
+ // @ts-ignore
145
+ await page.screenshot({ path });
146
+ }
137
147
  }