@internetarchive/recaptcha-manager 0.1.1 → 0.1.2-webdev-7354.0
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/.github/workflows/ci.yml +8 -7
- package/.github/workflows/gh-pages-main.yml +39 -0
- package/.github/workflows/pr-preview.yml +63 -0
- package/.husky/pre-commit +0 -3
- package/.prettierignore +1 -0
- package/README.md +6 -28
- package/coverage/.gitignore +3 -0
- package/demo/app-root.ts +1 -1
- package/dist/demo/app-root.js +1 -1
- package/dist/demo/app-root.js.map +1 -1
- package/dist/src/recaptcha-manager.d.ts +0 -1
- package/dist/src/recaptcha-manager.js +2 -0
- package/dist/src/recaptcha-manager.js.map +1 -1
- package/dist/src/recaptcha-widget.d.ts +1 -2
- package/dist/src/recaptcha-widget.js.map +1 -1
- package/dist/test/mock-grecaptcha.d.ts +1 -2
- package/dist/test/mock-grecaptcha.js +10 -12
- package/dist/test/mock-grecaptcha.js.map +1 -1
- package/dist/test/mock-lazy-loader.js +1 -0
- package/dist/test/mock-lazy-loader.js.map +1 -1
- package/dist/test/recaptcha-manager.test.js.map +1 -1
- package/dist/vite.config.d.ts +2 -0
- package/dist/vite.config.js +25 -0
- package/dist/vite.config.js.map +1 -0
- package/eslint.config.mjs +53 -0
- package/package.json +41 -62
- package/renovate.json +6 -0
- package/src/recaptcha-manager.ts +3 -1
- package/src/recaptcha-widget.ts +3 -3
- package/ssl/server.crt +22 -0
- package/ssl/server.key +28 -0
- package/test/mock-grecaptcha.ts +1 -3
- package/test/mock-lazy-loader.ts +2 -1
- package/test/recaptcha-manager.test.ts +2 -2
- package/tsconfig.json +11 -0
- package/vite.config.ts +25 -0
- package/web-dev-server.config.mjs +6 -2
- package/dist/src/recaptcha-manager copy.d.ts +0 -82
- package/dist/src/recaptcha-manager copy.js +0 -140
- package/dist/src/recaptcha-manager copy.js.map +0 -1
- package/dist/src/recaptcha-manager-bak.d.ts +0 -82
- package/dist/src/recaptcha-manager-bak.js +0 -140
- package/dist/src/recaptcha-manager-bak.js.map +0 -1
- package/dist/src/recaptcha.d.ts +0 -28
- package/dist/src/recaptcha.js +0 -87
- package/dist/src/recaptcha.js.map +0 -1
- package/dist/test/your-webcomponent.test.js +0 -4
- package/dist/test/your-webcomponent.test.js.map +0 -1
- /package/{dist/test/your-webcomponent.test.d.ts → coverage/.placeholder} +0 -0
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/// <reference types="grecaptcha" />
|
|
2
|
-
import { LazyLoaderServiceInterface } from '@internetarchive/lazy-loader-service';
|
|
3
|
-
export interface RecaptchaManagerInterface {
|
|
4
|
-
/**
|
|
5
|
-
* Executes the recaptcha and returns a token
|
|
6
|
-
*
|
|
7
|
-
* Example:
|
|
8
|
-
*
|
|
9
|
-
* try {
|
|
10
|
-
* const recaptchaResult = await recaptchaManager.execute();
|
|
11
|
-
* console.log('recaptcha token:', recaptchaResult);
|
|
12
|
-
* } catch {
|
|
13
|
-
* console.error('something happened')
|
|
14
|
-
* }
|
|
15
|
-
*/
|
|
16
|
-
execute(): Promise<string>;
|
|
17
|
-
/**
|
|
18
|
-
* Create the recaptcha widget.
|
|
19
|
-
*
|
|
20
|
-
* If the `container` is not passed in, one will be created.
|
|
21
|
-
*
|
|
22
|
-
* @param options
|
|
23
|
-
*/
|
|
24
|
-
setup(options?: {
|
|
25
|
-
container?: HTMLElement;
|
|
26
|
-
tabIndex?: number;
|
|
27
|
-
theme?: ReCaptchaV2.Theme;
|
|
28
|
-
type?: ReCaptchaV2.Type;
|
|
29
|
-
size?: ReCaptchaV2.Size;
|
|
30
|
-
}): void;
|
|
31
|
-
}
|
|
32
|
-
export declare class RecaptchaManager implements RecaptchaManagerInterface {
|
|
33
|
-
/**
|
|
34
|
-
* This is a convenience initializer that also lazily loads
|
|
35
|
-
* the recaptcha library from Google.
|
|
36
|
-
*
|
|
37
|
-
* @param options
|
|
38
|
-
* @returns
|
|
39
|
-
*/
|
|
40
|
-
static getRecaptchaManager(siteKey: string, options?: {
|
|
41
|
-
lazyLoader?: LazyLoaderServiceInterface;
|
|
42
|
-
tabIndex?: number;
|
|
43
|
-
theme?: ReCaptchaV2.Theme;
|
|
44
|
-
type?: ReCaptchaV2.Type;
|
|
45
|
-
size?: ReCaptchaV2.Size;
|
|
46
|
-
}): Promise<RecaptchaManagerInterface>;
|
|
47
|
-
/**
|
|
48
|
-
* Load the Recaptch library from Google.
|
|
49
|
-
*
|
|
50
|
-
* @param options
|
|
51
|
-
* @returns
|
|
52
|
-
*/
|
|
53
|
-
static loadRecaptchaLibrary(options?: {
|
|
54
|
-
lazyLoader?: LazyLoaderServiceInterface;
|
|
55
|
-
}): Promise<ReCaptchaV2.ReCaptcha>;
|
|
56
|
-
private grecaptchaLibrary;
|
|
57
|
-
private siteKey;
|
|
58
|
-
constructor(options: {
|
|
59
|
-
grecaptchaLibrary: ReCaptchaV2.ReCaptcha;
|
|
60
|
-
siteKey: string;
|
|
61
|
-
});
|
|
62
|
-
private executionSuccessBlock?;
|
|
63
|
-
private executionExpiredBlock?;
|
|
64
|
-
private executionErrorBlock?;
|
|
65
|
-
private isExecuting;
|
|
66
|
-
private containerElement;
|
|
67
|
-
/** @inheritdoc */
|
|
68
|
-
execute(): Promise<string>;
|
|
69
|
-
/** @inheritdoc */
|
|
70
|
-
setup(options?: {
|
|
71
|
-
container?: HTMLElement;
|
|
72
|
-
tabIndex?: number;
|
|
73
|
-
theme?: ReCaptchaV2.Theme;
|
|
74
|
-
type?: ReCaptchaV2.Type;
|
|
75
|
-
size?: ReCaptchaV2.Size;
|
|
76
|
-
}): void;
|
|
77
|
-
private createContainer;
|
|
78
|
-
private finishExecution;
|
|
79
|
-
private responseHandler;
|
|
80
|
-
private expiredHandler;
|
|
81
|
-
private errorHandler;
|
|
82
|
-
}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { LazyLoaderService, } from '@internetarchive/lazy-loader-service';
|
|
2
|
-
export class RecaptchaManager {
|
|
3
|
-
constructor(options) {
|
|
4
|
-
this.isExecuting = false;
|
|
5
|
-
this.containerElement = null;
|
|
6
|
-
this.grecaptchaLibrary = options.grecaptchaLibrary;
|
|
7
|
-
this.siteKey = options.siteKey;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* This is a convenience initializer that also lazily loads
|
|
11
|
-
* the recaptcha library from Google.
|
|
12
|
-
*
|
|
13
|
-
* @param options
|
|
14
|
-
* @returns
|
|
15
|
-
*/
|
|
16
|
-
static async getRecaptchaManager(siteKey, options) {
|
|
17
|
-
const grecaptchaLibrary = await RecaptchaManager.loadRecaptchaLibrary({
|
|
18
|
-
lazyLoader: options === null || options === void 0 ? void 0 : options.lazyLoader,
|
|
19
|
-
});
|
|
20
|
-
const manager = new RecaptchaManager({
|
|
21
|
-
grecaptchaLibrary,
|
|
22
|
-
siteKey,
|
|
23
|
-
});
|
|
24
|
-
manager.setup({
|
|
25
|
-
tabIndex: options === null || options === void 0 ? void 0 : options.tabIndex,
|
|
26
|
-
theme: options === null || options === void 0 ? void 0 : options.theme,
|
|
27
|
-
type: options === null || options === void 0 ? void 0 : options.type,
|
|
28
|
-
size: options === null || options === void 0 ? void 0 : options.size,
|
|
29
|
-
});
|
|
30
|
-
return manager;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Load the Recaptch library from Google.
|
|
34
|
-
*
|
|
35
|
-
* @param options
|
|
36
|
-
* @returns
|
|
37
|
-
*/
|
|
38
|
-
static async loadRecaptchaLibrary(options) {
|
|
39
|
-
var _a;
|
|
40
|
-
const service = (_a = options === null || options === void 0 ? void 0 : options.lazyLoader) !== null && _a !== void 0 ? _a : new LazyLoaderService();
|
|
41
|
-
return new Promise(resolve => {
|
|
42
|
-
window.grecaptchaLoadedCallback = () => {
|
|
43
|
-
setTimeout(() => {
|
|
44
|
-
// remove the callback when we're done with it
|
|
45
|
-
delete window.grecaptchaLoadedCallback;
|
|
46
|
-
}, 10);
|
|
47
|
-
resolve(window.grecaptcha);
|
|
48
|
-
};
|
|
49
|
-
service.loadScript({
|
|
50
|
-
src: 'https://www.google.com/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
/** @inheritdoc */
|
|
55
|
-
execute() {
|
|
56
|
-
if (this.isExecuting) {
|
|
57
|
-
throw new Error('Recaptcha is already executing');
|
|
58
|
-
}
|
|
59
|
-
this.isExecuting = true;
|
|
60
|
-
return new Promise((resolve, reject) => {
|
|
61
|
-
this.executionSuccessBlock = (token) => {
|
|
62
|
-
this.finishExecution();
|
|
63
|
-
resolve(token);
|
|
64
|
-
};
|
|
65
|
-
this.executionExpiredBlock = () => {
|
|
66
|
-
this.finishExecution();
|
|
67
|
-
reject(new Error('expired'));
|
|
68
|
-
};
|
|
69
|
-
this.executionErrorBlock = () => {
|
|
70
|
-
this.finishExecution();
|
|
71
|
-
reject(new Error('error'));
|
|
72
|
-
};
|
|
73
|
-
this.grecaptchaLibrary.execute();
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
/** @inheritdoc */
|
|
77
|
-
setup(options) {
|
|
78
|
-
var _a, _b;
|
|
79
|
-
const container = (_a = options === null || options === void 0 ? void 0 : options.container) !== null && _a !== void 0 ? _a : this.createContainer();
|
|
80
|
-
// we've already rendered this container, don't do it again
|
|
81
|
-
if (container === this.containerElement)
|
|
82
|
-
return;
|
|
83
|
-
this.containerElement = container;
|
|
84
|
-
this.grecaptchaLibrary.render(container, {
|
|
85
|
-
callback: this.responseHandler.bind(this),
|
|
86
|
-
'expired-callback': this.expiredHandler.bind(this),
|
|
87
|
-
'error-callback': this.errorHandler.bind(this),
|
|
88
|
-
sitekey: this.siteKey,
|
|
89
|
-
tabindex: options === null || options === void 0 ? void 0 : options.tabIndex,
|
|
90
|
-
theme: options === null || options === void 0 ? void 0 : options.theme,
|
|
91
|
-
type: options === null || options === void 0 ? void 0 : options.type,
|
|
92
|
-
size: (_b = options === null || options === void 0 ? void 0 : options.size) !== null && _b !== void 0 ? _b : 'invisible',
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
createContainer() {
|
|
96
|
-
const elementId = `recaptchaManager-${this.siteKey}`;
|
|
97
|
-
let element = document.getElementById(elementId);
|
|
98
|
-
if (!element) {
|
|
99
|
-
element = document.createElement('div');
|
|
100
|
-
element.id = elementId;
|
|
101
|
-
element.style.position = 'fixed';
|
|
102
|
-
element.style.top = '50%';
|
|
103
|
-
element.style.left = '50%';
|
|
104
|
-
element.style.outline = '1px solid purple';
|
|
105
|
-
element.style.width = '10px';
|
|
106
|
-
element.style.height = '10px';
|
|
107
|
-
// #recaptcha - TopCollections {
|
|
108
|
-
// position: fixed;
|
|
109
|
-
// top: 50 %;
|
|
110
|
-
// left: 50 %;
|
|
111
|
-
// }
|
|
112
|
-
document.body.insertBefore(element, document.body.firstChild);
|
|
113
|
-
}
|
|
114
|
-
return element;
|
|
115
|
-
}
|
|
116
|
-
finishExecution() {
|
|
117
|
-
this.isExecuting = false;
|
|
118
|
-
this.grecaptchaLibrary.reset();
|
|
119
|
-
}
|
|
120
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
121
|
-
responseHandler(response) {
|
|
122
|
-
if (this.executionSuccessBlock) {
|
|
123
|
-
this.executionSuccessBlock(response);
|
|
124
|
-
this.executionSuccessBlock = undefined;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
expiredHandler() {
|
|
128
|
-
if (this.executionExpiredBlock) {
|
|
129
|
-
this.executionExpiredBlock();
|
|
130
|
-
this.executionExpiredBlock = undefined;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
errorHandler() {
|
|
134
|
-
if (this.executionErrorBlock) {
|
|
135
|
-
this.executionErrorBlock();
|
|
136
|
-
this.executionErrorBlock = undefined;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
//# sourceMappingURL=recaptcha-manager-bak.js.map
|
|
@@ -1 +0,0 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AC3B,OAAO,CAAC,KAAK,CAAC,OAAO,GAAG,kBAAkB,CAAC;YAC3C,OAAO,CAAC,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC;YAC7B,OAAO,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;YAC9B,gCAAgC;YAChC,qBAAqB;YACrB,eAAe;YACf,gBAAgB;YAChB,IAAI;YAEJ,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;SAC/D;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAEO,eAAe;QACrB,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QACzB,IAAI,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC;IACjC,CAAC;IAED,8DAA8D;IACtD,eAAe,CAAC,QAAa;QACnC,IAAI,IAAI,CAAC,qBAAqB,EAAE;YAC9B,IAAI,CAAC,qBAAqB,CAAC,QAAQ,CAAC,CAAC;YACrC,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;SACxC;IACH,CAAC;IAEO,cAAc;QACpB,IAAI,IAAI,CAAC,qBAAqB,EAAE;YAC9B,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAC7B,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;SACxC;IACH,CAAC;IAEO,YAAY;QAClB,IAAI,IAAI,CAAC,mBAAmB,EAAE;YAC5B,IAAI,CAAC,mBAAmB,EAAE,CAAC;YAC3B,IAAI,CAAC,mBAAmB,GAAG,SAAS,CAAC;SACtC;IACH,CAAC;CACF","sourcesContent":["import {\n LazyLoaderService,\n LazyLoaderServiceInterface,\n} from '@internetarchive/lazy-loader-service';\n\nexport interface RecaptchaManagerInterface {\n /**\n * Executes the recaptcha and returns a token\n *\n * Example:\n *\n * try {\n * const recaptchaResult = await recaptchaManager.execute();\n * console.log('recaptcha token:', recaptchaResult);\n * } catch {\n * console.error('something happened')\n * }\n */\n execute(): Promise<string>;\n\n /**\n * Create the recaptcha widget.\n *\n * If the `container` is not passed in, one will be created.\n *\n * @param options\n */\n setup(options?: {\n container?: HTMLElement;\n tabIndex?: number;\n theme?: ReCaptchaV2.Theme;\n type?: ReCaptchaV2.Type;\n size?: ReCaptchaV2.Size;\n }): void;\n}\n\nexport class RecaptchaManager implements RecaptchaManagerInterface {\n /**\n * This is a convenience initializer that also lazily loads\n * the recaptcha library from Google.\n *\n * @param options\n * @returns\n */\n static async getRecaptchaManager(\n siteKey: string,\n options?: {\n lazyLoader?: LazyLoaderServiceInterface;\n tabIndex?: number;\n theme?: ReCaptchaV2.Theme;\n type?: ReCaptchaV2.Type;\n size?: ReCaptchaV2.Size;\n }\n ): Promise<RecaptchaManagerInterface> {\n const grecaptchaLibrary = await RecaptchaManager.loadRecaptchaLibrary({\n lazyLoader: options?.lazyLoader,\n });\n\n const manager = new RecaptchaManager({\n grecaptchaLibrary,\n siteKey,\n });\n\n manager.setup({\n tabIndex: options?.tabIndex,\n theme: options?.theme,\n type: options?.type,\n size: options?.size,\n });\n\n return manager;\n }\n\n /**\n * Load the Recaptch library from Google.\n *\n * @param options\n * @returns\n */\n static async loadRecaptchaLibrary(options?: {\n lazyLoader?: LazyLoaderServiceInterface;\n }): Promise<ReCaptchaV2.ReCaptcha> {\n const service = options?.lazyLoader ?? new LazyLoaderService();\n\n return new Promise(resolve => {\n (window as any).grecaptchaLoadedCallback = (): void => {\n setTimeout(() => {\n // remove the callback when we're done with it\n delete (window as any).grecaptchaLoadedCallback;\n }, 10);\n resolve(window.grecaptcha);\n };\n\n service.loadScript({\n src: 'https://www.google.com/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',\n });\n });\n }\n\n private grecaptchaLibrary: ReCaptchaV2.ReCaptcha;\n\n private siteKey: string;\n\n constructor(options: {\n grecaptchaLibrary: ReCaptchaV2.ReCaptcha;\n siteKey: string;\n }) {\n this.grecaptchaLibrary = options.grecaptchaLibrary;\n this.siteKey = options.siteKey;\n }\n\n private executionSuccessBlock?: (token: string) => void;\n\n private executionExpiredBlock?: () => void;\n\n private executionErrorBlock?: () => void;\n\n private isExecuting = false;\n\n private containerElement: HTMLElement | null = null;\n\n /** @inheritdoc */\n execute(): Promise<string> {\n if (this.isExecuting) {\n throw new Error('Recaptcha is already executing');\n }\n\n this.isExecuting = true;\n return new Promise((resolve, reject) => {\n this.executionSuccessBlock = (token: string): void => {\n this.finishExecution();\n resolve(token);\n };\n\n this.executionExpiredBlock = (): void => {\n this.finishExecution();\n reject(new Error('expired'));\n };\n\n this.executionErrorBlock = (): void => {\n this.finishExecution();\n reject(new Error('error'));\n };\n\n this.grecaptchaLibrary.execute();\n });\n }\n\n /** @inheritdoc */\n setup(options?: {\n container?: HTMLElement;\n tabIndex?: number;\n theme?: ReCaptchaV2.Theme;\n type?: ReCaptchaV2.Type;\n size?: ReCaptchaV2.Size;\n }): void {\n const container = options?.container ?? this.createContainer();\n\n // we've already rendered this container, don't do it again\n if (container === this.containerElement) return;\n\n this.containerElement = container;\n this.grecaptchaLibrary.render(container, {\n callback: this.responseHandler.bind(this),\n 'expired-callback': this.expiredHandler.bind(this),\n 'error-callback': this.errorHandler.bind(this),\n sitekey: this.siteKey,\n tabindex: options?.tabIndex,\n theme: options?.theme,\n type: options?.type,\n size: options?.size ?? 'invisible',\n });\n }\n\n private createContainer(): HTMLElement {\n const elementId = `recaptchaManager-${this.siteKey}`;\n let element: HTMLElement | null = document.getElementById(elementId);\n if (!element) {\n element = document.createElement('div');\n element.id = elementId;\n element.style.position = 'fixed';\n element.style.top = '50%';\n element.style.left = '50%';\n element.style.outline = '1px solid purple';\n element.style.width = '10px';\n element.style.height = '10px';\n // #recaptcha - TopCollections {\n // position: fixed;\n // top: 50 %;\n // left: 50 %;\n // }\n\n document.body.insertBefore(element, document.body.firstChild);\n }\n return element;\n }\n\n private finishExecution(): void {\n this.isExecuting = false;\n this.grecaptchaLibrary.reset();\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private responseHandler(response: any): void {\n if (this.executionSuccessBlock) {\n this.executionSuccessBlock(response);\n this.executionSuccessBlock = undefined;\n }\n }\n\n private expiredHandler(): void {\n if (this.executionExpiredBlock) {\n this.executionExpiredBlock();\n this.executionExpiredBlock = undefined;\n }\n }\n\n private errorHandler(): void {\n if (this.executionErrorBlock) {\n this.executionErrorBlock();\n this.executionErrorBlock = undefined;\n }\n }\n}\n"]}
|
package/dist/src/recaptcha.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/// <reference types="grecaptcha" />
|
|
2
|
-
export interface RecaptchaWidgetInterface {
|
|
3
|
-
/** execute the recaptcha request and returns a token */
|
|
4
|
-
execute(): Promise<string>;
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* This encompasses a widget for a given site key.
|
|
8
|
-
*/
|
|
9
|
-
export declare class RecaptchaWidget implements RecaptchaWidgetInterface {
|
|
10
|
-
private executionSuccessBlock?;
|
|
11
|
-
private executionExpiredBlock?;
|
|
12
|
-
private executionErrorBlock?;
|
|
13
|
-
private grecaptchaLibrary;
|
|
14
|
-
private siteKey;
|
|
15
|
-
private widgetId;
|
|
16
|
-
constructor(config: {
|
|
17
|
-
siteKey: string;
|
|
18
|
-
grecaptchaLibrary: ReCaptchaV2.ReCaptcha;
|
|
19
|
-
}, recaptchaParams?: ReCaptchaV2.Parameters);
|
|
20
|
-
/** @inheritdoc */
|
|
21
|
-
execute(): Promise<string>;
|
|
22
|
-
/** @inheritdoc */
|
|
23
|
-
private setup;
|
|
24
|
-
private createContainer;
|
|
25
|
-
private responseHandler;
|
|
26
|
-
private expiredHandler;
|
|
27
|
-
private errorHandler;
|
|
28
|
-
}
|
package/dist/src/recaptcha.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This encompasses a widget for a given site key.
|
|
3
|
-
*/
|
|
4
|
-
export class RecaptchaWidget {
|
|
5
|
-
constructor(config, recaptchaParams) {
|
|
6
|
-
this.widgetId = null;
|
|
7
|
-
this.siteKey = config.siteKey;
|
|
8
|
-
this.grecaptchaLibrary = config.grecaptchaLibrary;
|
|
9
|
-
const container = this.createContainer();
|
|
10
|
-
this.setup(container, recaptchaParams);
|
|
11
|
-
}
|
|
12
|
-
/** @inheritdoc */
|
|
13
|
-
async execute() {
|
|
14
|
-
const { widgetId } = this;
|
|
15
|
-
if (widgetId === null) {
|
|
16
|
-
throw new Error('Recaptcha is not setup');
|
|
17
|
-
}
|
|
18
|
-
// there is no way to know when users have dismissed a previous recaptcha
|
|
19
|
-
// so if we're still in the middle of execution when we call execute again,
|
|
20
|
-
// just reset it and execute it again
|
|
21
|
-
this.grecaptchaLibrary.reset(widgetId);
|
|
22
|
-
return new Promise((resolve, reject) => {
|
|
23
|
-
this.executionSuccessBlock = (token) => {
|
|
24
|
-
this.grecaptchaLibrary.reset(widgetId);
|
|
25
|
-
resolve(token);
|
|
26
|
-
};
|
|
27
|
-
this.executionExpiredBlock = () => {
|
|
28
|
-
this.grecaptchaLibrary.reset(widgetId);
|
|
29
|
-
reject(new Error('expired'));
|
|
30
|
-
};
|
|
31
|
-
this.executionErrorBlock = () => {
|
|
32
|
-
this.grecaptchaLibrary.reset(widgetId);
|
|
33
|
-
reject(new Error('error'));
|
|
34
|
-
};
|
|
35
|
-
this.grecaptchaLibrary.execute(widgetId);
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
/** @inheritdoc */
|
|
39
|
-
setup(container, recaptchaParams) {
|
|
40
|
-
var _a;
|
|
41
|
-
this.widgetId = this.grecaptchaLibrary.render(container, {
|
|
42
|
-
callback: this.responseHandler.bind(this),
|
|
43
|
-
'expired-callback': this.expiredHandler.bind(this),
|
|
44
|
-
'error-callback': this.errorHandler.bind(this),
|
|
45
|
-
sitekey: this.siteKey,
|
|
46
|
-
tabindex: recaptchaParams === null || recaptchaParams === void 0 ? void 0 : recaptchaParams.tabindex,
|
|
47
|
-
theme: recaptchaParams === null || recaptchaParams === void 0 ? void 0 : recaptchaParams.theme,
|
|
48
|
-
type: recaptchaParams === null || recaptchaParams === void 0 ? void 0 : recaptchaParams.type,
|
|
49
|
-
size: (_a = recaptchaParams === null || recaptchaParams === void 0 ? void 0 : recaptchaParams.size) !== null && _a !== void 0 ? _a : 'invisible',
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
createContainer() {
|
|
53
|
-
const elementId = `recaptchaManager-${this.siteKey}`;
|
|
54
|
-
let element = document.getElementById(elementId);
|
|
55
|
-
if (!element) {
|
|
56
|
-
element = document.createElement('div');
|
|
57
|
-
element.id = elementId;
|
|
58
|
-
element.style.position = 'fixed';
|
|
59
|
-
element.style.top = '50%';
|
|
60
|
-
element.style.left = '50%';
|
|
61
|
-
element.style.outline = '1px solid purple';
|
|
62
|
-
element.style.width = '10px';
|
|
63
|
-
element.style.height = '10px';
|
|
64
|
-
document.body.appendChild(element);
|
|
65
|
-
}
|
|
66
|
-
return element;
|
|
67
|
-
}
|
|
68
|
-
responseHandler(response) {
|
|
69
|
-
if (this.executionSuccessBlock) {
|
|
70
|
-
this.executionSuccessBlock(response);
|
|
71
|
-
this.executionSuccessBlock = undefined;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
expiredHandler() {
|
|
75
|
-
if (this.executionExpiredBlock) {
|
|
76
|
-
this.executionExpiredBlock();
|
|
77
|
-
this.executionExpiredBlock = undefined;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
errorHandler() {
|
|
81
|
-
if (this.executionErrorBlock) {
|
|
82
|
-
this.executionErrorBlock();
|
|
83
|
-
this.executionErrorBlock = undefined;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
//# sourceMappingURL=recaptcha.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"recaptcha.js","sourceRoot":"","sources":["../../src/recaptcha.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,MAAM,OAAO,eAAe;IAa1B,YACE,M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kBAAkB,CAAC;YAC3C,OAAO,CAAC,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC;YAC7B,OAAO,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;YAC9B,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;SACpC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAEO,eAAe,CAAC,QAAa;QACnC,IAAI,IAAI,CAAC,qBAAqB,EAAE;YAC9B,IAAI,CAAC,qBAAqB,CAAC,QAAQ,CAAC,CAAC;YACrC,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;SACxC;IACH,CAAC;IAEO,cAAc;QACpB,IAAI,IAAI,CAAC,qBAAqB,EAAE;YAC9B,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAC7B,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;SACxC;IACH,CAAC;IAEO,YAAY;QAClB,IAAI,IAAI,CAAC,mBAAmB,EAAE;YAC5B,IAAI,CAAC,mBAAmB,EAAE,CAAC;YAC3B,IAAI,CAAC,mBAAmB,GAAG,SAAS,CAAC;SACtC;IACH,CAAC;CACF","sourcesContent":["export interface RecaptchaWidgetInterface {\n /** execute the recaptcha request and returns a token */\n execute(): Promise<string>;\n}\n\n/**\n * This encompasses a widget for a given site key.\n */\nexport class RecaptchaWidget implements RecaptchaWidgetInterface {\n private executionSuccessBlock?: (token: string) => void;\n\n private executionExpiredBlock?: () => void;\n\n private executionErrorBlock?: () => void;\n\n private grecaptchaLibrary: ReCaptchaV2.ReCaptcha;\n\n private siteKey: string;\n\n private widgetId: number | null = null;\n\n constructor(\n config: {\n siteKey: string;\n grecaptchaLibrary: ReCaptchaV2.ReCaptcha;\n },\n recaptchaParams?: ReCaptchaV2.Parameters\n ) {\n this.siteKey = config.siteKey;\n this.grecaptchaLibrary = config.grecaptchaLibrary;\n\n const container = this.createContainer();\n this.setup(container, recaptchaParams);\n }\n\n /** @inheritdoc */\n async execute(): Promise<string> {\n const { widgetId } = this;\n\n if (widgetId === null) {\n throw new Error('Recaptcha is not setup');\n }\n\n // there is no way to know when users have dismissed a previous recaptcha\n // so if we're still in the middle of execution when we call execute again,\n // just reset it and execute it again\n this.grecaptchaLibrary.reset(widgetId);\n\n return new Promise((resolve, reject) => {\n this.executionSuccessBlock = (token: string): void => {\n this.grecaptchaLibrary.reset(widgetId);\n resolve(token);\n };\n\n this.executionExpiredBlock = (): void => {\n this.grecaptchaLibrary.reset(widgetId);\n reject(new Error('expired'));\n };\n\n this.executionErrorBlock = (): void => {\n this.grecaptchaLibrary.reset(widgetId);\n reject(new Error('error'));\n };\n\n this.grecaptchaLibrary.execute(widgetId);\n });\n }\n\n /** @inheritdoc */\n private setup(\n container: HTMLElement,\n recaptchaParams?: ReCaptchaV2.Parameters\n ): void {\n this.widgetId = this.grecaptchaLibrary.render(container, {\n callback: this.responseHandler.bind(this),\n 'expired-callback': this.expiredHandler.bind(this),\n 'error-callback': this.errorHandler.bind(this),\n sitekey: this.siteKey,\n tabindex: recaptchaParams?.tabindex,\n theme: recaptchaParams?.theme,\n type: recaptchaParams?.type,\n size: recaptchaParams?.size ?? 'invisible',\n });\n }\n\n private createContainer(): HTMLElement {\n const elementId = `recaptchaManager-${this.siteKey}`;\n let element: HTMLElement | null = document.getElementById(elementId);\n if (!element) {\n element = document.createElement('div');\n element.id = elementId;\n element.style.position = 'fixed';\n element.style.top = '50%';\n element.style.left = '50%';\n element.style.outline = '1px solid purple';\n element.style.width = '10px';\n element.style.height = '10px';\n document.body.appendChild(element);\n }\n return element;\n }\n\n private responseHandler(response: any): void {\n if (this.executionSuccessBlock) {\n this.executionSuccessBlock(response);\n this.executionSuccessBlock = undefined;\n }\n }\n\n private expiredHandler(): void {\n if (this.executionExpiredBlock) {\n this.executionExpiredBlock();\n this.executionExpiredBlock = undefined;\n }\n }\n\n private errorHandler(): void {\n if (this.executionErrorBlock) {\n this.executionErrorBlock();\n this.executionErrorBlock = undefined;\n }\n }\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"your-webcomponent.test.js","sourceRoot":"","sources":["../../test/your-webcomponent.test.ts"],"names":[],"mappings":";AAAA,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;AAElC,CAAC,CAAC,CAAC","sourcesContent":["describe('YourWebComponent', () => {\n\n});\n"]}
|
|
File without changes
|