@internetarchive/recaptcha-manager 2.0.0 → 2.0.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.
Files changed (58) hide show
  1. package/.claude/settings.local.json +7 -0
  2. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.editorconfig +29 -0
  3. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.github/workflows/ci.yml +27 -0
  4. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.github/workflows/gh-pages-main.yml +39 -0
  5. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.github/workflows/pr-preview.yml +63 -0
  6. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.husky/pre-commit +1 -0
  7. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.prettierignore +1 -0
  8. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/LICENSE +661 -0
  9. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/README.md +63 -0
  10. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/coverage/.gitignore +3 -0
  11. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/coverage/.placeholder +0 -0
  12. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/demo/app-root.ts +111 -0
  13. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/demo/index.html +21 -0
  14. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/eslint.config.mjs +53 -0
  15. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/index.ts +10 -0
  16. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/package-lock.json +11336 -0
  17. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/package.json +76 -0
  18. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/renovate.json +6 -0
  19. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/src/recaptcha-manager.ts +114 -0
  20. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/src/recaptcha-widget.ts +148 -0
  21. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/ssl/server.crt +22 -0
  22. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/ssl/server.key +28 -0
  23. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/test/mock-grecaptcha.ts +85 -0
  24. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/test/mock-lazy-loader.ts +36 -0
  25. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/test/recaptcha-manager.test.ts +201 -0
  26. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/tsconfig.json +31 -0
  27. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/vite.config.ts +25 -0
  28. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/web-dev-server.config.mjs +32 -0
  29. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/web-test-runner.config.mjs +41 -0
  30. package/.editorconfig +29 -29
  31. package/.github/workflows/ci.yml +27 -27
  32. package/.github/workflows/gh-pages-main.yml +39 -39
  33. package/.github/workflows/pr-preview.yml +63 -63
  34. package/.husky/pre-commit +1 -1
  35. package/.prettierignore +1 -1
  36. package/LICENSE +661 -661
  37. package/README.md +63 -63
  38. package/coverage/.gitignore +2 -2
  39. package/demo/app-root.ts +111 -111
  40. package/demo/index.html +21 -21
  41. package/dist/demo/app-root.js +31 -31
  42. package/dist/demo/app-root.js.map +1 -1
  43. package/dist/src/recaptcha-manager.d.ts +2 -1
  44. package/dist/src/recaptcha-manager.js +3 -2
  45. package/dist/src/recaptcha-manager.js.map +1 -1
  46. package/dist/test/mock-lazy-loader.js.map +1 -1
  47. package/dist/test/recaptcha-manager.test.js +1 -1
  48. package/dist/test/recaptcha-manager.test.js.map +1 -1
  49. package/eslint.config.mjs +53 -53
  50. package/package.json +76 -76
  51. package/renovate.json +6 -6
  52. package/src/recaptcha-manager.ts +114 -113
  53. package/ssl/server.key +28 -28
  54. package/test/mock-lazy-loader.ts +36 -36
  55. package/test/recaptcha-manager.test.ts +201 -199
  56. package/tsconfig.json +31 -31
  57. package/web-dev-server.config.mjs +32 -32
  58. package/web-test-runner.config.mjs +41 -41
package/README.md CHANGED
@@ -1,63 +1,63 @@
1
- ![Build Status](https://github.com/internetarchive/iaux-recaptcha-manager/actions/workflows/ci.yml/badge.svg)
2
-
3
- # Internet Archive reCaptcha Manager
4
-
5
- A library to lazy load and interact with reCaptcha
6
-
7
- ## Installation
8
-
9
- ```shell
10
- > npm install @internetarchive/recaptcha-manager
11
- ```
12
-
13
- ## Usage
14
-
15
- ```ts
16
- const recaptchaManager = new RecaptchaManager({
17
- defaultSiteKey: 'your-site-key',
18
- });
19
-
20
- // will load recaptcha library if it's not loaded
21
- const recaptchaWidget = await recaptchaManager.getRecaptchaWidget({
22
- recaptchaParams: {
23
- tabindex: 0,
24
- theme: 'light',
25
- type: 'image',
26
- },
27
- });
28
-
29
- const token = await recaptchaWidget.execute();
30
-
31
- // submit token with your post and validate on the backend
32
- ```
33
-
34
- For more usage examples, see `demo/app-root.ts` and `test/recaptcha-manager.test.ts`.
35
-
36
- ## Local Demo with `web-dev-server`
37
- ```bash
38
- npm run start
39
- ```
40
- To run a local development server that serves the basic demo located in `demo/index.html`
41
-
42
- ## Testing with Web Test Runner
43
- To run the suite of Web Test Runner tests, run
44
- ```bash
45
- npm run test
46
- ```
47
-
48
- To run the tests in watch mode (for <abbr title="test driven development">TDD</abbr>, for example), run
49
-
50
- ```bash
51
- npm run test:watch
52
- ```
53
-
54
- ## Linting with ESLint, Prettier, and Types
55
- To scan the project for linting errors, run
56
- ```bash
57
- npm run lint
58
- ```
59
-
60
- To automatically fix many linting errors, run
61
- ```bash
62
- npm run format
63
- ```
1
+ ![Build Status](https://github.com/internetarchive/iaux-recaptcha-manager/actions/workflows/ci.yml/badge.svg)
2
+
3
+ # Internet Archive reCaptcha Manager
4
+
5
+ A library to lazy load and interact with reCaptcha
6
+
7
+ ## Installation
8
+
9
+ ```shell
10
+ > npm install @internetarchive/recaptcha-manager
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ const recaptchaManager = new RecaptchaManager({
17
+ defaultSiteKey: 'your-site-key',
18
+ });
19
+
20
+ // will load recaptcha library if it's not loaded
21
+ const recaptchaWidget = await recaptchaManager.getRecaptchaWidget({
22
+ recaptchaParams: {
23
+ tabindex: 0,
24
+ theme: 'light',
25
+ type: 'image',
26
+ },
27
+ });
28
+
29
+ const token = await recaptchaWidget.execute();
30
+
31
+ // submit token with your post and validate on the backend
32
+ ```
33
+
34
+ For more usage examples, see `demo/app-root.ts` and `test/recaptcha-manager.test.ts`.
35
+
36
+ ## Local Demo with `web-dev-server`
37
+ ```bash
38
+ npm run start
39
+ ```
40
+ To run a local development server that serves the basic demo located in `demo/index.html`
41
+
42
+ ## Testing with Web Test Runner
43
+ To run the suite of Web Test Runner tests, run
44
+ ```bash
45
+ npm run test
46
+ ```
47
+
48
+ To run the tests in watch mode (for <abbr title="test driven development">TDD</abbr>, for example), run
49
+
50
+ ```bash
51
+ npm run test:watch
52
+ ```
53
+
54
+ ## Linting with ESLint, Prettier, and Types
55
+ To scan the project for linting errors, run
56
+ ```bash
57
+ npm run lint
58
+ ```
59
+
60
+ To automatically fix many linting errors, run
61
+ ```bash
62
+ npm run format
63
+ ```
@@ -1,3 +1,3 @@
1
- *
2
- !.placeholder
1
+ *
2
+ !.placeholder
3
3
  !.gitignore
package/demo/app-root.ts CHANGED
@@ -1,111 +1,111 @@
1
- import { html, css, LitElement } from 'lit';
2
- import { customElement, state, query } from 'lit/decorators.js';
3
- import { RecaptchaWidgetInterface } from '../src/recaptcha-widget';
4
- import {
5
- RecaptchaManager,
6
- RecaptchaManagerInterface,
7
- } from '../src/recaptcha-manager';
8
-
9
- @customElement('app-root')
10
- export class AppRoot extends LitElement {
11
- @state() result?: string;
12
-
13
- @state() result2?: string;
14
-
15
- @query('#timeout') timeoutInput!: HTMLInputElement;
16
-
17
- private recaptchaManager: RecaptchaManagerInterface = new RecaptchaManager({
18
- defaultSiteKey: '6Ld64a8UAAAAAGbDwi1927ztGNw7YABQ-dqzvTN2',
19
- });
20
-
21
- private recaptcha1?: RecaptchaWidgetInterface;
22
-
23
- private recaptcha2?: RecaptchaWidgetInterface;
24
-
25
- render() {
26
- return html`
27
- <label for="timeout">
28
- Timeout (ms):
29
- <input
30
- type="number"
31
- id="timeout"
32
- value="10000"
33
- min="0"
34
- max="99999"
35
- step="100"
36
- @change=${this.updateTimeout}
37
- />
38
- </label>
39
- <p>
40
- <button @click="${this.loadRecaptcha}">Load Recaptcha</button
41
- ><button @click="${this.executeRecaptcha}">Execute Recaptcha</button>
42
- </p>
43
- ${this.result
44
- ? html`<p><strong>Token:</strong></p>
45
- <p>${this.result}</p>`
46
- : ''}
47
- <p>
48
- <button @click="${this.loadRecaptcha2}">Load Another Recaptcha</button
49
- ><button @click="${this.executeRecaptcha2}">
50
- Execute Another Recaptcha
51
- </button>
52
- </p>
53
- ${this.result2
54
- ? html`<p><strong>Token 2:</strong></p>
55
- <p>${this.result2}</p>`
56
- : ''}
57
- `;
58
- }
59
-
60
- private updateTimeout() {
61
- this.recaptchaManager.setTimeoutDelay(+this.timeoutInput.value);
62
- }
63
-
64
- private async loadRecaptcha() {
65
- this.recaptcha1 = await this.recaptchaManager?.getRecaptchaWidget({
66
- recaptchaParams: {
67
- tabindex: 0,
68
- theme: 'light',
69
- type: 'image',
70
- },
71
- });
72
- }
73
-
74
- private async executeRecaptcha() {
75
- if (!this.recaptcha1) {
76
- await this.loadRecaptcha();
77
- }
78
-
79
- if (!this.recaptcha1) {
80
- console.error('error loading recaptcha');
81
- return;
82
- }
83
-
84
- this.result = await this.recaptcha1.execute();
85
- }
86
-
87
- private async loadRecaptcha2() {
88
- this.recaptcha2 = await this.recaptchaManager?.getRecaptchaWidget({
89
- siteKey: '<your-site-key>',
90
- });
91
- }
92
-
93
- private async executeRecaptcha2() {
94
- if (!this.recaptcha2) {
95
- await this.loadRecaptcha();
96
- }
97
-
98
- if (!this.recaptcha2) {
99
- console.error('error loading recaptcha');
100
- return;
101
- }
102
-
103
- this.result2 = await this.recaptcha2.execute();
104
- }
105
-
106
- static styles = css`
107
- :host {
108
- display: block;
109
- }
110
- `;
111
- }
1
+ import { html, css, LitElement } from 'lit';
2
+ import { customElement, state, query } from 'lit/decorators.js';
3
+ import { RecaptchaWidgetInterface } from '../src/recaptcha-widget';
4
+ import {
5
+ RecaptchaManager,
6
+ RecaptchaManagerInterface,
7
+ } from '../src/recaptcha-manager';
8
+
9
+ @customElement('app-root')
10
+ export class AppRoot extends LitElement {
11
+ @state() result?: string;
12
+
13
+ @state() result2?: string;
14
+
15
+ @query('#timeout') timeoutInput!: HTMLInputElement;
16
+
17
+ private recaptchaManager: RecaptchaManagerInterface = new RecaptchaManager({
18
+ defaultSiteKey: '6Ld64a8UAAAAAGbDwi1927ztGNw7YABQ-dqzvTN2',
19
+ });
20
+
21
+ private recaptcha1?: RecaptchaWidgetInterface;
22
+
23
+ private recaptcha2?: RecaptchaWidgetInterface;
24
+
25
+ render() {
26
+ return html`
27
+ <label for="timeout">
28
+ Timeout (ms):
29
+ <input
30
+ type="number"
31
+ id="timeout"
32
+ value="10000"
33
+ min="0"
34
+ max="99999"
35
+ step="100"
36
+ @change=${this.updateTimeout}
37
+ />
38
+ </label>
39
+ <p>
40
+ <button @click="${this.loadRecaptcha}">Load Recaptcha</button
41
+ ><button @click="${this.executeRecaptcha}">Execute Recaptcha</button>
42
+ </p>
43
+ ${this.result
44
+ ? html`<p><strong>Token:</strong></p>
45
+ <p>${this.result}</p>`
46
+ : ''}
47
+ <p>
48
+ <button @click="${this.loadRecaptcha2}">Load Another Recaptcha</button
49
+ ><button @click="${this.executeRecaptcha2}">
50
+ Execute Another Recaptcha
51
+ </button>
52
+ </p>
53
+ ${this.result2
54
+ ? html`<p><strong>Token 2:</strong></p>
55
+ <p>${this.result2}</p>`
56
+ : ''}
57
+ `;
58
+ }
59
+
60
+ private updateTimeout() {
61
+ this.recaptchaManager.setTimeoutDelay(+this.timeoutInput.value);
62
+ }
63
+
64
+ private async loadRecaptcha() {
65
+ this.recaptcha1 = await this.recaptchaManager?.getRecaptchaWidget({
66
+ recaptchaParams: {
67
+ tabindex: 0,
68
+ theme: 'light',
69
+ type: 'image',
70
+ },
71
+ });
72
+ }
73
+
74
+ private async executeRecaptcha() {
75
+ if (!this.recaptcha1) {
76
+ await this.loadRecaptcha();
77
+ }
78
+
79
+ if (!this.recaptcha1) {
80
+ console.error('error loading recaptcha');
81
+ return;
82
+ }
83
+
84
+ this.result = await this.recaptcha1.execute();
85
+ }
86
+
87
+ private async loadRecaptcha2() {
88
+ this.recaptcha2 = await this.recaptchaManager?.getRecaptchaWidget({
89
+ siteKey: '<your-site-key>',
90
+ });
91
+ }
92
+
93
+ private async executeRecaptcha2() {
94
+ if (!this.recaptcha2) {
95
+ await this.loadRecaptcha();
96
+ }
97
+
98
+ if (!this.recaptcha2) {
99
+ console.error('error loading recaptcha');
100
+ return;
101
+ }
102
+
103
+ this.result2 = await this.recaptcha2.execute();
104
+ }
105
+
106
+ static styles = css`
107
+ :host {
108
+ display: block;
109
+ }
110
+ `;
111
+ }
package/demo/index.html CHANGED
@@ -1,21 +1,21 @@
1
- <!doctype html>
2
- <html lang="en-GB">
3
- <head>
4
- <meta charset="utf-8">
5
- <style>
6
- html {
7
- font-size: 10px; /* This is to match petabox's base font size */
8
- }
9
-
10
- body {
11
- background: #fafafa;
12
- }
13
- </style>
14
- </head>
15
- <body>
16
- <app-root></app-root>
17
-
18
- <script type="module" src="../dist/demo/app-root.js">
19
- </script>
20
- </body>
21
- </html>
1
+ <!doctype html>
2
+ <html lang="en-GB">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <style>
6
+ html {
7
+ font-size: 10px; /* This is to match petabox's base font size */
8
+ }
9
+
10
+ body {
11
+ background: #fafafa;
12
+ }
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <app-root></app-root>
17
+
18
+ <script type="module" src="../dist/demo/app-root.js">
19
+ </script>
20
+ </body>
21
+ </html>
@@ -10,37 +10,37 @@ let AppRoot = class AppRoot extends LitElement {
10
10
  });
11
11
  }
12
12
  render() {
13
- return html `
14
- <label for="timeout">
15
- Timeout (ms):
16
- <input
17
- type="number"
18
- id="timeout"
19
- value="10000"
20
- min="0"
21
- max="99999"
22
- step="100"
23
- @change=${this.updateTimeout}
24
- />
25
- </label>
26
- <p>
27
- <button @click="${this.loadRecaptcha}">Load Recaptcha</button
28
- ><button @click="${this.executeRecaptcha}">Execute Recaptcha</button>
29
- </p>
13
+ return html `
14
+ <label for="timeout">
15
+ Timeout (ms):
16
+ <input
17
+ type="number"
18
+ id="timeout"
19
+ value="10000"
20
+ min="0"
21
+ max="99999"
22
+ step="100"
23
+ @change=${this.updateTimeout}
24
+ />
25
+ </label>
26
+ <p>
27
+ <button @click="${this.loadRecaptcha}">Load Recaptcha</button
28
+ ><button @click="${this.executeRecaptcha}">Execute Recaptcha</button>
29
+ </p>
30
30
  ${this.result
31
- ? html `<p><strong>Token:</strong></p>
31
+ ? html `<p><strong>Token:</strong></p>
32
32
  <p>${this.result}</p>`
33
- : ''}
34
- <p>
35
- <button @click="${this.loadRecaptcha2}">Load Another Recaptcha</button
36
- ><button @click="${this.executeRecaptcha2}">
37
- Execute Another Recaptcha
38
- </button>
39
- </p>
33
+ : ''}
34
+ <p>
35
+ <button @click="${this.loadRecaptcha2}">Load Another Recaptcha</button
36
+ ><button @click="${this.executeRecaptcha2}">
37
+ Execute Another Recaptcha
38
+ </button>
39
+ </p>
40
40
  ${this.result2
41
- ? html `<p><strong>Token 2:</strong></p>
41
+ ? html `<p><strong>Token 2:</strong></p>
42
42
  <p>${this.result2}</p>`
43
- : ''}
43
+ : ''}
44
44
  `;
45
45
  }
46
46
  updateTimeout() {
@@ -83,10 +83,10 @@ let AppRoot = class AppRoot extends LitElement {
83
83
  this.result2 = await this.recaptcha2.execute();
84
84
  }
85
85
  };
86
- AppRoot.styles = css `
87
- :host {
88
- display: block;
89
- }
86
+ AppRoot.styles = css `
87
+ :host {
88
+ display: block;
89
+ }
90
90
  `;
91
91
  __decorate([
92
92
  state()
@@ -1 +1 @@
1
- {"version":3,"file":"app-root.js","sourceRoot":"","sources":["../../demo/app-root.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAEhE,OAAO,EACL,gBAAgB,GAEjB,MAAM,0BAA0B,CAAC;AAG3B,IAAM,OAAO,GAAb,MAAM,OAAQ,SAAQ,UAAU;IAAhC;;QAOG,qBAAgB,GAA8B,IAAI,gBAAgB,CAAC;YACzE,cAAc,EAAE,0CAA0C;SAC3D,CAAC,CAAC;IA4FL,CAAC;IAtFC,MAAM;QACJ,OAAO,IAAI,CAAA;;;;;;;;;;oBAUK,IAAI,CAAC,aAAa;;;;0BAIZ,IAAI,CAAC,aAAa;2BACjB,IAAI,CAAC,gBAAgB;;QAExC,IAAI,CAAC,MAAM;YACX,CAAC,CAAC,IAAI,CAAA;iBACG,IAAI,CAAC,MAAM,MAAM;YAC1B,CAAC,CAAC,EAAE;;0BAEc,IAAI,CAAC,cAAc;2BAClB,IAAI,CAAC,iBAAiB;;;;QAIzC,IAAI,CAAC,OAAO;YACZ,CAAC,CAAC,IAAI,CAAA;iBACG,IAAI,CAAC,OAAO,MAAM;YAC3B,CAAC,CAAC,EAAE;KACP,CAAC;IACJ,CAAC;IAEO,aAAa;QACnB,IAAI,CAAC,gBAAgB,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAClE,CAAC;IAEO,KAAK,CAAC,aAAa;;QACzB,IAAI,CAAC,UAAU,GAAG,MAAM,CAAA,MAAA,IAAI,CAAC,gBAAgB,0CAAE,kBAAkB,CAAC;YAChE,eAAe,EAAE;gBACf,QAAQ,EAAE,CAAC;gBACX,KAAK,EAAE,OAAO;gBACd,IAAI,EAAE,OAAO;aACd;SACF,CAAC,CAAA,CAAC;IACL,CAAC;IAEO,KAAK,CAAC,gBAAgB;QAC5B,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YACrB,MAAM,IAAI,CAAC,aAAa,EAAE,CAAC;QAC7B,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YACrB,OAAO,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC;YACzC,OAAO;QACT,CAAC;QAED,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;IAChD,CAAC;IAEO,KAAK,CAAC,cAAc;;QAC1B,IAAI,CAAC,UAAU,GAAG,MAAM,CAAA,MAAA,IAAI,CAAC,gBAAgB,0CAAE,kBAAkB,CAAC;YAChE,OAAO,EAAE,iBAAiB;SAC3B,CAAC,CAAA,CAAC;IACL,CAAC;IAEO,KAAK,CAAC,iBAAiB;QAC7B,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YACrB,MAAM,IAAI,CAAC,aAAa,EAAE,CAAC;QAC7B,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YACrB,OAAO,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC;YACzC,OAAO;QACT,CAAC;QAED,IAAI,CAAC,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;IACjD,CAAC;;AAEM,cAAM,GAAG,GAAG,CAAA;;;;GAIlB,AAJY,CAIX;AAnGO;IAAR,KAAK,EAAE;uCAAiB;AAEhB;IAAR,KAAK,EAAE;wCAAkB;AAEP;IAAlB,KAAK,CAAC,UAAU,CAAC;6CAAiC;AALxC,OAAO;IADnB,aAAa,CAAC,UAAU,CAAC;GACb,OAAO,CAqGnB","sourcesContent":["import { html, css, LitElement } from 'lit';\r\nimport { customElement, state, query } from 'lit/decorators.js';\r\nimport { RecaptchaWidgetInterface } from '../src/recaptcha-widget';\r\nimport {\r\n RecaptchaManager,\r\n RecaptchaManagerInterface,\r\n} from '../src/recaptcha-manager';\r\n\r\n@customElement('app-root')\r\nexport class AppRoot extends LitElement {\r\n @state() result?: string;\r\n\r\n @state() result2?: string;\r\n\r\n @query('#timeout') timeoutInput!: HTMLInputElement;\r\n\r\n private recaptchaManager: RecaptchaManagerInterface = new RecaptchaManager({\r\n defaultSiteKey: '6Ld64a8UAAAAAGbDwi1927ztGNw7YABQ-dqzvTN2',\r\n });\r\n\r\n private recaptcha1?: RecaptchaWidgetInterface;\r\n\r\n private recaptcha2?: RecaptchaWidgetInterface;\r\n\r\n render() {\r\n return html`\r\n <label for=\"timeout\">\r\n Timeout (ms):\r\n <input\r\n type=\"number\"\r\n id=\"timeout\"\r\n value=\"10000\"\r\n min=\"0\"\r\n max=\"99999\"\r\n step=\"100\"\r\n @change=${this.updateTimeout}\r\n />\r\n </label>\r\n <p>\r\n <button @click=\"${this.loadRecaptcha}\">Load Recaptcha</button\r\n ><button @click=\"${this.executeRecaptcha}\">Execute Recaptcha</button>\r\n </p>\r\n ${this.result\r\n ? html`<p><strong>Token:</strong></p>\r\n <p>${this.result}</p>`\r\n : ''}\r\n <p>\r\n <button @click=\"${this.loadRecaptcha2}\">Load Another Recaptcha</button\r\n ><button @click=\"${this.executeRecaptcha2}\">\r\n Execute Another Recaptcha\r\n </button>\r\n </p>\r\n ${this.result2\r\n ? html`<p><strong>Token 2:</strong></p>\r\n <p>${this.result2}</p>`\r\n : ''}\r\n `;\r\n }\r\n\r\n private updateTimeout() {\r\n this.recaptchaManager.setTimeoutDelay(+this.timeoutInput.value);\r\n }\r\n\r\n private async loadRecaptcha() {\r\n this.recaptcha1 = await this.recaptchaManager?.getRecaptchaWidget({\r\n recaptchaParams: {\r\n tabindex: 0,\r\n theme: 'light',\r\n type: 'image',\r\n },\r\n });\r\n }\r\n\r\n private async executeRecaptcha() {\r\n if (!this.recaptcha1) {\r\n await this.loadRecaptcha();\r\n }\r\n\r\n if (!this.recaptcha1) {\r\n console.error('error loading recaptcha');\r\n return;\r\n }\r\n\r\n this.result = await this.recaptcha1.execute();\r\n }\r\n\r\n private async loadRecaptcha2() {\r\n this.recaptcha2 = await this.recaptchaManager?.getRecaptchaWidget({\r\n siteKey: '<your-site-key>',\r\n });\r\n }\r\n\r\n private async executeRecaptcha2() {\r\n if (!this.recaptcha2) {\r\n await this.loadRecaptcha();\r\n }\r\n\r\n if (!this.recaptcha2) {\r\n console.error('error loading recaptcha');\r\n return;\r\n }\r\n\r\n this.result2 = await this.recaptcha2.execute();\r\n }\r\n\r\n static styles = css`\r\n :host {\r\n display: block;\r\n }\r\n `;\r\n}\r\n"]}
1
+ {"version":3,"file":"app-root.js","sourceRoot":"","sources":["../../demo/app-root.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAEhE,OAAO,EACL,gBAAgB,GAEjB,MAAM,0BAA0B,CAAC;AAG3B,IAAM,OAAO,GAAb,MAAM,OAAQ,SAAQ,UAAU;IAAhC;;QAOG,qBAAgB,GAA8B,IAAI,gBAAgB,CAAC;YACzE,cAAc,EAAE,0CAA0C;SAC3D,CAAC,CAAC;IA4FL,CAAC;IAtFC,MAAM;QACJ,OAAO,IAAI,CAAA;;;;;;;;;;oBAUK,IAAI,CAAC,aAAa;;;;0BAIZ,IAAI,CAAC,aAAa;2BACjB,IAAI,CAAC,gBAAgB;;QAExC,IAAI,CAAC,MAAM;YACX,CAAC,CAAC,IAAI,CAAA;iBACG,IAAI,CAAC,MAAM,MAAM;YAC1B,CAAC,CAAC,EAAE;;0BAEc,IAAI,CAAC,cAAc;2BAClB,IAAI,CAAC,iBAAiB;;;;QAIzC,IAAI,CAAC,OAAO;YACZ,CAAC,CAAC,IAA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AAC,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;IACjD,CAAC;;AAEM,cAAM,GAAG,GAAG,CAAA;;;;GAIlB,AAJY,CAIX;AAnGO;IAAR,KAAK,EAAE;uCAAiB;AAEhB;IAAR,KAAK,EAAE;wCAAkB;AAEP;IAAlB,KAAK,CAAC,UAAU,CAAC;6CAAiC;AALxC,OAAO;IADnB,aAAa,CAAC,UAAU,CAAC;GACb,OAAO,CAqGnB","sourcesContent":["import { html, css, LitElement } from 'lit';\nimport { customElement, state, query } from 'lit/decorators.js';\nimport { RecaptchaWidgetInterface } from '../src/recaptcha-widget';\nimport {\n RecaptchaManager,\n RecaptchaManagerInterface,\n} from '../src/recaptcha-manager';\n\n@customElement('app-root')\nexport class AppRoot extends LitElement {\n @state() result?: string;\n\n @state() result2?: string;\n\n @query('#timeout') timeoutInput!: HTMLInputElement;\n\n private recaptchaManager: RecaptchaManagerInterface = new RecaptchaManager({\n defaultSiteKey: '6Ld64a8UAAAAAGbDwi1927ztGNw7YABQ-dqzvTN2',\n });\n\n private recaptcha1?: RecaptchaWidgetInterface;\n\n private recaptcha2?: RecaptchaWidgetInterface;\n\n render() {\n return html`\n <label for=\"timeout\">\n Timeout (ms):\n <input\n type=\"number\"\n id=\"timeout\"\n value=\"10000\"\n min=\"0\"\n max=\"99999\"\n step=\"100\"\n @change=${this.updateTimeout}\n />\n </label>\n <p>\n <button @click=\"${this.loadRecaptcha}\">Load Recaptcha</button\n ><button @click=\"${this.executeRecaptcha}\">Execute Recaptcha</button>\n </p>\n ${this.result\n ? html`<p><strong>Token:</strong></p>\n <p>${this.result}</p>`\n : ''}\n <p>\n <button @click=\"${this.loadRecaptcha2}\">Load Another Recaptcha</button\n ><button @click=\"${this.executeRecaptcha2}\">\n Execute Another Recaptcha\n </button>\n </p>\n ${this.result2\n ? html`<p><strong>Token 2:</strong></p>\n <p>${this.result2}</p>`\n : ''}\n `;\n }\n\n private updateTimeout() {\n this.recaptchaManager.setTimeoutDelay(+this.timeoutInput.value);\n }\n\n private async loadRecaptcha() {\n this.recaptcha1 = await this.recaptchaManager?.getRecaptchaWidget({\n recaptchaParams: {\n tabindex: 0,\n theme: 'light',\n type: 'image',\n },\n });\n }\n\n private async executeRecaptcha() {\n if (!this.recaptcha1) {\n await this.loadRecaptcha();\n }\n\n if (!this.recaptcha1) {\n console.error('error loading recaptcha');\n return;\n }\n\n this.result = await this.recaptcha1.execute();\n }\n\n private async loadRecaptcha2() {\n this.recaptcha2 = await this.recaptchaManager?.getRecaptchaWidget({\n siteKey: '<your-site-key>',\n });\n }\n\n private async executeRecaptcha2() {\n if (!this.recaptcha2) {\n await this.loadRecaptcha();\n }\n\n if (!this.recaptcha2) {\n console.error('error loading recaptcha');\n return;\n }\n\n this.result2 = await this.recaptcha2.execute();\n }\n\n static styles = css`\n :host {\n display: block;\n }\n `;\n}\n"]}
@@ -36,7 +36,8 @@ export declare class RecaptchaManager implements RecaptchaManagerInterface {
36
36
  recaptchaParams?: ReCaptchaV2.Parameters;
37
37
  }): Promise<RecaptchaWidgetInterface>;
38
38
  /**
39
- * Load the Recaptcha library from Google.
39
+ * Load the Recaptcha library from Google's www.recaptcha.net mirror, which
40
+ * is reachable in regions where www.google.com is blocked.
40
41
  *
41
42
  * @returns Promise<ReCaptchaV2.ReCaptcha>
42
43
  */
@@ -36,7 +36,8 @@ export class RecaptchaManager {
36
36
  return recaptcha;
37
37
  }
38
38
  /**
39
- * Load the Recaptcha library from Google.
39
+ * Load the Recaptcha library from Google's www.recaptcha.net mirror, which
40
+ * is reachable in regions where www.google.com is blocked.
40
41
  *
41
42
  * @returns Promise<ReCaptchaV2.ReCaptcha>
42
43
  */
@@ -57,7 +58,7 @@ export class RecaptchaManager {
57
58
  };
58
59
  setTimeout(() => reject(new Error('grecaptcha failed to execute callback')), this.timeout);
59
60
  this.lazyLoader.loadScript({
60
- src: 'https://www.google.com/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',
61
+ src: 'https://www.recaptcha.net/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',
61
62
  });
62
63
  });
63
64
  }
@@ -1 +1 @@
1
- {"version":3,"file":"recaptcha-manager.js","sourceRoot":"","sources":["../../src/recaptcha-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,GAElB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,eAAe,EAA4B,MAAM,oBAAoB,CAAC;AAiB/E,MAAM,OAAO,gBAAgB;IAc3B,YAAY,OAKX;;QAZO,mBAAc,GAAoC,EAAE,CAAC;QAE7D;;WAEG;QACK,YAAO,GAAG,gBAAgB,CAAC,eAAe,CAAC;QAQjD,IAAI,CAAC,cAAc,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,cAAc,CAAC;QAC9C,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,mCAAI,IAAI,iBAAiB,EAAE,CAAC;QACjE,IAAI,CAAC,sBAAsB,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,iBAAiB,CAAC;QACzD,IAAI,CAAC,OAAO,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,mCAAI,gBAAgB,CAAC,eAAe,CAAC;IACtE,CAAC;IAED,kBAAkB;IAClB,eAAe,CAAC,KAAa;QAC3B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;IACvB,CAAC;IAED,kBAAkB;IAClB,KAAK,CAAC,kBAAkB,CAAC,OAGxB;;QACC,MAAM,GAAG,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,mCAAI,IAAI,CAAC,cAAc,CAAC;QACpD,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAC9D,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,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'@internetarchive/lazy-loader-service';\r\nimport { RecaptchaWidget, RecaptchaWidgetInterface } from './recaptcha-widget';\r\n\r\nexport interface RecaptchaManagerInterface {\r\n /**\r\n * Changes the manager's timeout delay to the given number of milliseconds.\r\n */\r\n setTimeoutDelay(delay: number): void;\r\n\r\n /**\r\n * Load a recaptcha widget for a given site key or the default site key.\r\n */\r\n getRecaptchaWidget(options?: {\r\n siteKey?: string;\r\n recaptchaParams?: ReCaptchaV2.Parameters;\r\n }): Promise<RecaptchaWidgetInterface>;\r\n}\r\n\r\nexport class RecaptchaManager implements RecaptchaManagerInterface {\r\n private static readonly DEFAULT_TIMEOUT = 10000;\r\n\r\n private lazyLoader: LazyLoaderServiceInterface;\r\n\r\n private defaultSiteKey?: string;\r\n\r\n private recaptchaCache: Record<string, RecaptchaWidget> = {};\r\n\r\n /**\r\n * How long in milliseconds to wait for the recaptcha library to load before timing out.\r\n */\r\n private timeout = RecaptchaManager.DEFAULT_TIMEOUT;\r\n\r\n constructor(options?: {\r\n defaultSiteKey?: string;\r\n lazyLoader?: LazyLoaderServiceInterface;\r\n grecaptchaLibrary?: ReCaptchaV2.ReCaptcha; // allows dependency injection or will be lazy loaded\r\n timeout?: number;\r\n }) {\r\n this.defaultSiteKey = options?.defaultSiteKey;\r\n this.lazyLoader = options?.lazyLoader ?? new LazyLoaderService();\r\n this.grecaptchaLibraryCache = options?.grecaptchaLibrary;\r\n this.timeout = options?.timeout ?? RecaptchaManager.DEFAULT_TIMEOUT;\r\n }\r\n\r\n /** @inheritdoc */\r\n setTimeoutDelay(delay: number): void {\r\n this.timeout = delay;\r\n }\r\n\r\n /** @inheritdoc */\r\n async getRecaptchaWidget(options?: {\r\n siteKey?: string;\r\n recaptchaParams?: ReCaptchaV2.Parameters;\r\n }): Promise<RecaptchaWidgetInterface> {\r\n const key = options?.siteKey ?? this.defaultSiteKey;\r\n if (!key) {\r\n throw new Error('The reCaptcha widget requires a site key');\r\n }\r\n\r\n const cached = this.recaptchaCache[key];\r\n if (cached) return cached;\r\n\r\n const grecaptchaLibrary = await this.getRecaptchaLibrary();\r\n const recaptcha = new RecaptchaWidget(\r\n {\r\n siteKey: key,\r\n grecaptchaLibrary,\r\n },\r\n options?.recaptchaParams,\r\n );\r\n\r\n this.recaptchaCache[key] = recaptcha;\r\n return recaptcha;\r\n }\r\n\r\n /**\r\n * Load the Recaptcha library from Google.\r\n *\r\n * @returns Promise<ReCaptchaV2.ReCaptcha>\r\n */\r\n private async getRecaptchaLibrary(): Promise<ReCaptchaV2.ReCaptcha> {\r\n if (this.grecaptchaLibraryCache) {\r\n return this.grecaptchaLibraryCache;\r\n }\r\n return new Promise((resolve, reject) => {\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n (window as any).grecaptchaLoadedCallback = (): void => {\r\n setTimeout(() => {\r\n // remove the callback when we're done with it\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n delete (window as any).grecaptchaLoadedCallback;\r\n }, 10);\r\n this.grecaptchaLibraryCache = window.grecaptcha;\r\n resolve(window.grecaptcha);\r\n };\r\n\r\n setTimeout(\r\n () => reject(new Error('grecaptcha failed to execute callback')),\r\n this.timeout,\r\n );\r\n\r\n this.lazyLoader.loadScript({\r\n src: 'https://www.google.com/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',\r\n });\r\n });\r\n }\r\n\r\n /** don't use directly, use `getRecaptchaLibrary()` */\r\n private grecaptchaLibraryCache?: ReCaptchaV2.ReCaptcha;\r\n}\r\n"]}
1
+ {"version":3,"file":"recaptcha-manager.js","sourceRoot":"","sources":["../../src/recaptcha-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,GAElB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,eAAe,EAA4B,MAAM,oBAAoB,CAAC;AAiB/E,MAAM,OAAO,gBAAgB;IAc3B,YAAY,OAKX;;QAZO,mBAAc,GAAoC,EAAE,CAAC;QAE7D;;WAEG;QACK,YAAO,GAAG,gBAA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QAC/B,IAAI,IAAI,CAAC,sBAAsB,EAAE,CAAC;YAChC,OAAO,IAAI,CAAC,sBAAsB,CAAC;QACrC,CAAC;QACD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,8DAA8D;YAC7D,MAAc,CAAC,wBAAwB,GAAG,GAAS,EAAE;gBACpD,UAAU,CAAC,GAAG,EAAE;oBACd,8CAA8C;oBAC9C,8DAA8D;oBAC9D,OAAQ,MAAc,CAAC,wBAAwB,CAAC;gBAClD,CAAC,EAAE,EAAE,CAAC,CAAC;gBACP,IAAI,CAAC,sBAAsB,GAAG,MAAM,CAAC,UAAU,CAAC;gBAChD,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YAC7B,CAAC,CAAC;YAEF,UAAU,CACR,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC,EAChE,IAAI,CAAC,OAAO,CACb,CAAC;YAEF,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC;gBACzB,GAAG,EAAE,4FAA4F;aAClG,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;;AAvFuB,gCAAe,GAAG,KAAK,AAAR,CAAS","sourcesContent":["import {\n LazyLoaderService,\n LazyLoaderServiceInterface,\n} from '@internetarchive/lazy-loader-service';\nimport { RecaptchaWidget, RecaptchaWidgetInterface } from './recaptcha-widget';\n\nexport interface RecaptchaManagerInterface {\n /**\n * Changes the manager's timeout delay to the given number of milliseconds.\n */\n setTimeoutDelay(delay: number): void;\n\n /**\n * Load a recaptcha widget for a given site key or the default site key.\n */\n getRecaptchaWidget(options?: {\n siteKey?: string;\n recaptchaParams?: ReCaptchaV2.Parameters;\n }): Promise<RecaptchaWidgetInterface>;\n}\n\nexport class RecaptchaManager implements RecaptchaManagerInterface {\n private static readonly DEFAULT_TIMEOUT = 10000;\n\n private lazyLoader: LazyLoaderServiceInterface;\n\n private defaultSiteKey?: string;\n\n private recaptchaCache: Record<string, RecaptchaWidget> = {};\n\n /**\n * How long in milliseconds to wait for the recaptcha library to load before timing out.\n */\n private timeout = RecaptchaManager.DEFAULT_TIMEOUT;\n\n constructor(options?: {\n defaultSiteKey?: string;\n lazyLoader?: LazyLoaderServiceInterface;\n grecaptchaLibrary?: ReCaptchaV2.ReCaptcha; // allows dependency injection or will be lazy loaded\n timeout?: number;\n }) {\n this.defaultSiteKey = options?.defaultSiteKey;\n this.lazyLoader = options?.lazyLoader ?? new LazyLoaderService();\n this.grecaptchaLibraryCache = options?.grecaptchaLibrary;\n this.timeout = options?.timeout ?? RecaptchaManager.DEFAULT_TIMEOUT;\n }\n\n /** @inheritdoc */\n setTimeoutDelay(delay: number): void {\n this.timeout = delay;\n }\n\n /** @inheritdoc */\n async getRecaptchaWidget(options?: {\n siteKey?: string;\n recaptchaParams?: ReCaptchaV2.Parameters;\n }): Promise<RecaptchaWidgetInterface> {\n const key = options?.siteKey ?? this.defaultSiteKey;\n if (!key) {\n throw new Error('The reCaptcha widget requires a site key');\n }\n\n const cached = this.recaptchaCache[key];\n if (cached) return cached;\n\n const grecaptchaLibrary = await this.getRecaptchaLibrary();\n const recaptcha = new RecaptchaWidget(\n {\n siteKey: key,\n grecaptchaLibrary,\n },\n options?.recaptchaParams,\n );\n\n this.recaptchaCache[key] = recaptcha;\n return recaptcha;\n }\n\n /**\n * Load the Recaptcha library from Google's www.recaptcha.net mirror, which\n * is reachable in regions where www.google.com is blocked.\n *\n * @returns Promise<ReCaptchaV2.ReCaptcha>\n */\n private async getRecaptchaLibrary(): Promise<ReCaptchaV2.ReCaptcha> {\n if (this.grecaptchaLibraryCache) {\n return this.grecaptchaLibraryCache;\n }\n return new Promise((resolve, reject) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (window as any).grecaptchaLoadedCallback = (): void => {\n setTimeout(() => {\n // remove the callback when we're done with it\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n delete (window as any).grecaptchaLoadedCallback;\n }, 10);\n this.grecaptchaLibraryCache = window.grecaptcha;\n resolve(window.grecaptcha);\n };\n\n setTimeout(\n () => reject(new Error('grecaptcha failed to execute callback')),\n this.timeout,\n );\n\n this.lazyLoader.loadScript({\n src: 'https://www.recaptcha.net/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',\n });\n });\n }\n\n /** don't use directly, use `getRecaptchaLibrary()` */\n private grecaptchaLibraryCache?: ReCaptchaV2.ReCaptcha;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"mock-lazy-loader.js","sourceRoot":"","sources":["../../test/mock-lazy-loader.ts"],"names":[],"mappings":"AAQA,MAAM,OAAO,qBAAqB;IAGhC,YAAoB,WAA0B,GAAG,EAAE,GAAE,CAAC;QAAlC,aAAQ,GAAR,QAAQ,CAA0B;IAAG,CAAC;IAE1D,EAAE,CACA,KAAQ,EACR,QAAoC;QAEpC,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,MAGhB;QACC,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,OAIhB;QACC,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC;QACjC,IAAI,CAAC,QAAQ,EAAE,CAAC;IAClB,CAAC;CACF","sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\r\nimport {\r\n BundleType,\r\n LazyLoaderServiceEvents,\r\n LazyLoaderServiceInterface,\r\n} from '@internetarchive/lazy-loader-service';\r\nimport { Unsubscribe } from 'nanoevents';\r\n\r\nexport class MockLazyLoaderService implements LazyLoaderServiceInterface {\r\n loadScriptSrc?: string;\r\n\r\n constructor(private onloadFn: () => unknown = () => {}) {}\r\n\r\n on<E extends keyof LazyLoaderServiceEvents>(\r\n event: E,\r\n callback: LazyLoaderServiceEvents[E],\r\n ): Unsubscribe {\r\n throw new Error('Method not implemented.');\r\n }\r\n\r\n async loadBundle(bundle: {\r\n module?: string;\r\n nomodule?: string;\r\n }): Promise<void> {\r\n console.debug('loadBundle', bundle);\r\n }\r\n\r\n async loadScript(options: {\r\n src: string;\r\n bundleType?: BundleType;\r\n attributes?: Record<string, string>;\r\n }): Promise<void> {\r\n this.loadScriptSrc = options.src;\r\n this.onloadFn();\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"mock-lazy-loader.js","sourceRoot":"","sources":["../../test/mock-lazy-loader.ts"],"names":[],"mappings":"AAQA,MAAM,OAAO,qBAAqB;IAGhC,YAAoB,WAA0B,GAAG,EAAE,GAAE,CAAC;QAAlC,aAAQ,GAAR,QAAQ,CAA0B;IAAG,CAAC;IAE1D,EAAE,CACA,KAAQ,EACR,QAAoC;QAEpC,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,MAGhB;QACC,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,OAIhB;QACC,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC;QACjC,IAAI,CAAC,QAAQ,EAAE,CAAC;IAClB,CAAC;CACF","sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\nimport {\n BundleType,\n LazyLoaderServiceEvents,\n LazyLoaderServiceInterface,\n} from '@internetarchive/lazy-loader-service';\nimport { Unsubscribe } from 'nanoevents';\n\nexport class MockLazyLoaderService implements LazyLoaderServiceInterface {\n loadScriptSrc?: string;\n\n constructor(private onloadFn: () => unknown = () => {}) {}\n\n on<E extends keyof LazyLoaderServiceEvents>(\n event: E,\n callback: LazyLoaderServiceEvents[E],\n ): Unsubscribe {\n throw new Error('Method not implemented.');\n }\n\n async loadBundle(bundle: {\n module?: string;\n nomodule?: string;\n }): Promise<void> {\n console.debug('loadBundle', bundle);\n }\n\n async loadScript(options: {\n src: string;\n bundleType?: BundleType;\n attributes?: Record<string, string>;\n }): Promise<void> {\n this.loadScriptSrc = options.src;\n this.onloadFn();\n }\n}\n"]}
@@ -66,7 +66,7 @@ describe('ReCaptcha Management', () => {
66
66
  });
67
67
  await recaptchaManager.getRecaptchaWidget();
68
68
  const loadUrl = mockLazyLoaderWithCallback.loadScriptSrc;
69
- expect(loadUrl === null || loadUrl === void 0 ? void 0 : loadUrl.includes('recaptcha/api.js?onload=grecaptchaLoadedCallback')).to.be.true;
69
+ expect(loadUrl === null || loadUrl === void 0 ? void 0 : loadUrl.includes('https://www.recaptcha.net/recaptcha/api.js?onload=grecaptchaLoadedCallback')).to.be.true;
70
70
  });
71
71
  it('rejects after specified timeout if grecaptcha fails to execute callback', async () => {
72
72
  const recaptchaManager = new RecaptchaManager({