@prosopo/procaptcha-bundle 3.3.0 → 4.1.7
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/.turbo/turbo-build$colon$cjs.log +28 -35
- package/.turbo/turbo-build$colon$tsc.log +33 -36
- package/.turbo/turbo-build.log +29 -36
- package/CHANGELOG.md +481 -0
- package/dist/cjs/index.cjs +4 -9
- package/dist/cjs/util/captcha/captchaRenderer.cjs +10 -9
- package/dist/cjs/util/captcha/components/{frictionlessCaptcha.cjs → bundleCaptcha.cjs} +2 -2
- package/dist/cjs/util/config.cjs +14 -4
- package/dist/cjs/util/widgetFactory.cjs +4 -6
- package/dist/components.d.ts +1 -4
- package/dist/components.d.ts.map +1 -1
- package/dist/components.js +1 -4
- package/dist/components.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -9
- package/dist/index.js.map +1 -1
- package/dist/tests/util/config.test.js +6 -6
- package/dist/tests/util/config.test.js.map +1 -1
- package/dist/tests/util/renderLogic.test.js +22 -14
- package/dist/tests/util/renderLogic.test.js.map +1 -1
- package/dist/util/captcha/captchaRenderer.d.ts +1 -6
- package/dist/util/captcha/captchaRenderer.d.ts.map +1 -1
- package/dist/util/captcha/captchaRenderer.js +10 -9
- package/dist/util/captcha/captchaRenderer.js.map +1 -1
- package/dist/util/captcha/{captchaProps.d.ts → components/bundleCaptcha.d.ts} +4 -3
- package/dist/util/captcha/components/bundleCaptcha.d.ts.map +1 -0
- package/dist/util/captcha/components/{frictionlessCaptcha.js → bundleCaptcha.js} +2 -2
- package/dist/util/captcha/components/bundleCaptcha.js.map +1 -0
- package/dist/util/config.d.ts +2 -2
- package/dist/util/config.d.ts.map +1 -1
- package/dist/util/config.js +14 -4
- package/dist/util/config.js.map +1 -1
- package/dist/util/widgetFactory.d.ts +1 -1
- package/dist/util/widgetFactory.d.ts.map +1 -1
- package/dist/util/widgetFactory.js +4 -6
- package/dist/util/widgetFactory.js.map +1 -1
- package/package.json +12 -12
- package/src/components.ts +15 -0
- package/src/index.html +11 -0
- package/src/index.ts +259 -0
- package/src/tests/procaptchaReady.unit.test.ts +58 -0
- package/src/tests/util/config.test.ts +97 -0
- package/src/tests/util/renderLogic.test.tsx +221 -0
- package/src/util/captcha/captchaRenderer.tsx +126 -0
- package/src/util/captcha/components/bundleCaptcha.tsx +57 -0
- package/src/util/config.ts +40 -0
- package/src/util/configCreator.ts +51 -0
- package/src/util/language.ts +53 -0
- package/src/util/timeout.ts +43 -0
- package/src/util/widgetFactory.ts +145 -0
- package/src/util/widgetThemeResolver.ts +40 -0
- package/tsconfig.cjs.json +35 -0
- package/tsconfig.json +42 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsconfig.types.json +9 -0
- package/vite.config.ts +18 -1
- package/vite.iife.config.ts +119 -0
- package/vite.serve.config.ts +17 -0
- package/dist/cjs/util/captcha/captchaComponentProvider.cjs +0 -20
- package/dist/cjs/util/captcha/captchaComponentsList.cjs +0 -12
- package/dist/cjs/util/captcha/captchaType.cjs +0 -12
- package/dist/cjs/util/captcha/components/imageCaptcha.cjs +0 -17
- package/dist/cjs/util/captcha/components/powCaptcha.cjs +0 -17
- package/dist/tests/util/captchaType.test.d.ts +0 -2
- package/dist/tests/util/captchaType.test.d.ts.map +0 -1
- package/dist/tests/util/captchaType.test.js +0 -36
- package/dist/tests/util/captchaType.test.js.map +0 -1
- package/dist/util/captcha/captchaComponentProvider.d.ts +0 -8
- package/dist/util/captcha/captchaComponentProvider.d.ts.map +0 -1
- package/dist/util/captcha/captchaComponentProvider.js +0 -20
- package/dist/util/captcha/captchaComponentProvider.js.map +0 -1
- package/dist/util/captcha/captchaComponentsList.d.ts +0 -6
- package/dist/util/captcha/captchaComponentsList.d.ts.map +0 -1
- package/dist/util/captcha/captchaComponentsList.js +0 -12
- package/dist/util/captcha/captchaComponentsList.js.map +0 -1
- package/dist/util/captcha/captchaProps.d.ts.map +0 -1
- package/dist/util/captcha/captchaProps.js +0 -2
- package/dist/util/captcha/captchaProps.js.map +0 -1
- package/dist/util/captcha/captchaType.d.ts +0 -3
- package/dist/util/captcha/captchaType.d.ts.map +0 -1
- package/dist/util/captcha/captchaType.js +0 -12
- package/dist/util/captcha/captchaType.js.map +0 -1
- package/dist/util/captcha/components/frictionlessCaptcha.d.ts +0 -4
- package/dist/util/captcha/components/frictionlessCaptcha.d.ts.map +0 -1
- package/dist/util/captcha/components/frictionlessCaptcha.js.map +0 -1
- package/dist/util/captcha/components/imageCaptcha.d.ts +0 -4
- package/dist/util/captcha/components/imageCaptcha.d.ts.map +0 -1
- package/dist/util/captcha/components/imageCaptcha.js +0 -17
- package/dist/util/captcha/components/imageCaptcha.js.map +0 -1
- package/dist/util/captcha/components/powCaptcha.d.ts +0 -4
- package/dist/util/captcha/components/powCaptcha.d.ts.map +0 -1
- package/dist/util/captcha/components/powCaptcha.js +0 -17
- package/dist/util/captcha/components/powCaptcha.js.map +0 -1
package/src/index.ts
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import { getWindowCallback } from "@prosopo/procaptcha-common";
|
|
16
|
+
import type { ProcaptchaRenderOptions } from "@prosopo/types";
|
|
17
|
+
import { at } from "@prosopo/util";
|
|
18
|
+
import type { Root } from "react-dom/client";
|
|
19
|
+
import { extractParams, getProcaptchaScript } from "./util/config.js";
|
|
20
|
+
import { WidgetFactory } from "./util/widgetFactory.js";
|
|
21
|
+
import { WidgetThemeResolver } from "./util/widgetThemeResolver.js";
|
|
22
|
+
|
|
23
|
+
const BUNDLE_NAMES = ["procaptcha.bundle.iife.js", "procaptcha.bundle.js"];
|
|
24
|
+
let procaptchaRoots: Root[] = [];
|
|
25
|
+
|
|
26
|
+
const widgetFactory = new WidgetFactory(new WidgetThemeResolver());
|
|
27
|
+
|
|
28
|
+
// Define a custom event name for procaptcha execution
|
|
29
|
+
const PROCAPTCHA_EXECUTE_EVENT = "procaptcha:execute";
|
|
30
|
+
|
|
31
|
+
// Define a custom event name for procaptcha ready
|
|
32
|
+
const PROCAPTCHA_READY_EVENT = "procaptcha:ready";
|
|
33
|
+
|
|
34
|
+
// Implicit render for targeting all elements with class 'procaptcha'
|
|
35
|
+
const implicitRender = async () => {
|
|
36
|
+
// Get elements with class 'procaptcha', not including buttons
|
|
37
|
+
const elements: Element[] = Array.from(
|
|
38
|
+
document.getElementsByClassName("procaptcha"),
|
|
39
|
+
).filter((element) => element.tagName.toLowerCase() !== "button");
|
|
40
|
+
|
|
41
|
+
// Set siteKey from renderOptions or from the first element's data-sitekey attribute
|
|
42
|
+
if (elements.length) {
|
|
43
|
+
const siteKey = at(elements, 0).getAttribute("data-sitekey");
|
|
44
|
+
const web3 = at(elements, 0).getAttribute("data-web3");
|
|
45
|
+
if (!siteKey) {
|
|
46
|
+
console.error("No site key found");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const root = await widgetFactory.createWidgets(
|
|
51
|
+
elements,
|
|
52
|
+
{
|
|
53
|
+
siteKey: siteKey,
|
|
54
|
+
},
|
|
55
|
+
!(web3 === "true"),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
procaptchaRoots.push(...root);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Check for invisible mode indicators (procaptcha class on buttons)
|
|
62
|
+
const invisibleButtons = Array.from(
|
|
63
|
+
document.getElementsByClassName("procaptcha"),
|
|
64
|
+
).filter((button) => button.tagName.toLowerCase() === "button");
|
|
65
|
+
|
|
66
|
+
if (invisibleButtons.length) {
|
|
67
|
+
for (const button of invisibleButtons) {
|
|
68
|
+
const siteKey = button.getAttribute("data-sitekey") || "";
|
|
69
|
+
const callback = button.getAttribute("data-callback") || "";
|
|
70
|
+
|
|
71
|
+
const root = await widgetFactory.createWidgets(
|
|
72
|
+
[button],
|
|
73
|
+
{
|
|
74
|
+
siteKey: siteKey,
|
|
75
|
+
callback: callback,
|
|
76
|
+
},
|
|
77
|
+
true,
|
|
78
|
+
true,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
procaptchaRoots.push(...root);
|
|
82
|
+
|
|
83
|
+
// Add click event listener to the button
|
|
84
|
+
button.addEventListener("click", async (event) => {
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
execute();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// Explicit render for targeting specific elements
|
|
93
|
+
export const render = async (
|
|
94
|
+
element: Element,
|
|
95
|
+
renderOptions: ProcaptchaRenderOptions,
|
|
96
|
+
) => {
|
|
97
|
+
const hasInvisibleSize =
|
|
98
|
+
Object.prototype.hasOwnProperty.call(renderOptions, "size") &&
|
|
99
|
+
renderOptions.size === "invisible";
|
|
100
|
+
|
|
101
|
+
const isWeb2 = !renderOptions.web3;
|
|
102
|
+
|
|
103
|
+
if (hasInvisibleSize || element.tagName.toLowerCase() === "button") {
|
|
104
|
+
const roots = await widgetFactory.createWidgets(
|
|
105
|
+
[element],
|
|
106
|
+
renderOptions,
|
|
107
|
+
isWeb2,
|
|
108
|
+
true,
|
|
109
|
+
);
|
|
110
|
+
procaptchaRoots.push(...roots);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const roots = await widgetFactory.createWidgets(
|
|
115
|
+
[element],
|
|
116
|
+
renderOptions,
|
|
117
|
+
isWeb2,
|
|
118
|
+
false,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
procaptchaRoots.push(...roots);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export default function ready(fn: () => void) {
|
|
125
|
+
if (document && document.readyState !== "loading") {
|
|
126
|
+
console.log("document.readyState ready!");
|
|
127
|
+
fn();
|
|
128
|
+
} else {
|
|
129
|
+
console.log("DOMContentLoaded listener!");
|
|
130
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
131
|
+
console.log("DOMContentLoaded fired");
|
|
132
|
+
console.log(window);
|
|
133
|
+
fn();
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export const execute = () => {
|
|
139
|
+
const containers = findProcaptchaContainers();
|
|
140
|
+
|
|
141
|
+
if (containers.length === 0) {
|
|
142
|
+
console.error("No Procaptcha containers found for execution");
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Dispatch a custom event to notify React components to show the modal or perform silent verification
|
|
147
|
+
const executeEvent = new CustomEvent(PROCAPTCHA_EXECUTE_EVENT, {
|
|
148
|
+
detail: {
|
|
149
|
+
containerId: containers[0]?.id || "procaptcha-container",
|
|
150
|
+
containerCount: containers.length,
|
|
151
|
+
timestamp: Date.now(),
|
|
152
|
+
},
|
|
153
|
+
bubbles: true,
|
|
154
|
+
cancelable: true,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Dispatch the event on the document
|
|
158
|
+
document.dispatchEvent(executeEvent);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
function findProcaptchaContainers(): Element[] {
|
|
162
|
+
const containers: Element[] = [];
|
|
163
|
+
|
|
164
|
+
// Strategy 1: Look for elements with data-size="invisible"
|
|
165
|
+
const invisibleContainers = Array.from(
|
|
166
|
+
document.querySelectorAll('[data-size="invisible"]'),
|
|
167
|
+
);
|
|
168
|
+
containers.push(...invisibleContainers);
|
|
169
|
+
|
|
170
|
+
// Strategy 2: Look for elements with specific IDs
|
|
171
|
+
const idContainers = Array.from(
|
|
172
|
+
document.querySelectorAll(
|
|
173
|
+
'#procaptcha-container, [id$="-procaptcha-container"]',
|
|
174
|
+
),
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
// Strategy 3: Look for elements with class 'p-procaptcha'
|
|
178
|
+
const classContainers = Array.from(
|
|
179
|
+
document.getElementsByClassName("p-procaptcha"),
|
|
180
|
+
);
|
|
181
|
+
containers.push(...classContainers);
|
|
182
|
+
|
|
183
|
+
for (const container of idContainers) {
|
|
184
|
+
if (!containers.includes(container)) {
|
|
185
|
+
containers.push(container);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return containers;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// extend the global Window interface to include the procaptcha object
|
|
193
|
+
declare global {
|
|
194
|
+
interface Window {
|
|
195
|
+
procaptcha: {
|
|
196
|
+
ready: typeof ready;
|
|
197
|
+
render: typeof render;
|
|
198
|
+
reset: typeof reset;
|
|
199
|
+
execute: typeof execute;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const start = () => {
|
|
205
|
+
// onLoadUrlCallback defines the name of the callback function to be called when the script is loaded
|
|
206
|
+
// onRenderExplicit takes values of either explicit or implicit
|
|
207
|
+
const { onloadUrlCallback, renderExplicit } = extractParams(BUNDLE_NAMES);
|
|
208
|
+
let readyCalled = false;
|
|
209
|
+
|
|
210
|
+
// Render the Procaptcha component implicitly if renderExplicit is not set to explicit
|
|
211
|
+
if (renderExplicit !== "explicit") {
|
|
212
|
+
getProcaptchaScript(BUNDLE_NAMES)?.addEventListener("load", () => {
|
|
213
|
+
ready(implicitRender);
|
|
214
|
+
readyCalled = true;
|
|
215
|
+
});
|
|
216
|
+
// or if the document has already loaded, call the implicit render function
|
|
217
|
+
if (document.readyState === "complete" && !readyCalled) {
|
|
218
|
+
ready(implicitRender);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (onloadUrlCallback) {
|
|
223
|
+
// Add event listener to the script tag to call the callback function when the script is loaded
|
|
224
|
+
getProcaptchaScript(BUNDLE_NAMES)?.addEventListener("load", () => {
|
|
225
|
+
const onloadCallback = getWindowCallback(onloadUrlCallback);
|
|
226
|
+
ready(onloadCallback);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// or if the document has already loaded, call the callback function
|
|
230
|
+
if (document.readyState === "complete" && !readyCalled) {
|
|
231
|
+
const onloadCallback = getWindowCallback(onloadUrlCallback);
|
|
232
|
+
ready(onloadCallback);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
export const reset = () => {
|
|
238
|
+
for (const root of procaptchaRoots) {
|
|
239
|
+
root.unmount();
|
|
240
|
+
}
|
|
241
|
+
procaptchaRoots = [];
|
|
242
|
+
|
|
243
|
+
start();
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// set the procaptcha attribute on the window
|
|
247
|
+
window.procaptcha = { ready, render, reset, execute };
|
|
248
|
+
|
|
249
|
+
// Dispatch a custom event to notify that window.procaptcha is ready
|
|
250
|
+
const procaptchaReadyEvent = new CustomEvent(PROCAPTCHA_READY_EVENT, {
|
|
251
|
+
detail: {
|
|
252
|
+
timestamp: Date.now(),
|
|
253
|
+
},
|
|
254
|
+
bubbles: true,
|
|
255
|
+
cancelable: false,
|
|
256
|
+
});
|
|
257
|
+
document.dispatchEvent(procaptchaReadyEvent);
|
|
258
|
+
|
|
259
|
+
start();
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import { JSDOM } from "jsdom";
|
|
16
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
17
|
+
|
|
18
|
+
describe("procaptcha:ready event", () => {
|
|
19
|
+
let dom: JSDOM;
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
dom = new JSDOM("<!DOCTYPE html><html><body></body></html>", {
|
|
23
|
+
url: "https://example.com",
|
|
24
|
+
});
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
global.document = dom.window.document;
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
global.window = dom.window as unknown as Window & typeof globalThis;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
// biome-ignore lint/suspicious/noExplicitAny: TODO fix any
|
|
33
|
+
(global as any).document = undefined;
|
|
34
|
+
// biome-ignore lint/suspicious/noExplicitAny: TODO fix any
|
|
35
|
+
(global as any).window = undefined;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should dispatch procaptcha:ready event when window.procaptcha is set", (done) => {
|
|
39
|
+
// Set up event listener before importing the module
|
|
40
|
+
document.addEventListener("procaptcha:ready", (event: Event) => {
|
|
41
|
+
const customEvent = event as CustomEvent;
|
|
42
|
+
expect(customEvent.detail).toBeDefined();
|
|
43
|
+
expect(typeof customEvent.detail.timestamp).toBe("number");
|
|
44
|
+
expect(customEvent.bubbles).toBe(true);
|
|
45
|
+
expect(customEvent.cancelable).toBe(false);
|
|
46
|
+
|
|
47
|
+
// Check that window.procaptcha is available when the event fires
|
|
48
|
+
expect(window.procaptcha).toBeDefined();
|
|
49
|
+
expect(window.procaptcha?.render).toBeDefined();
|
|
50
|
+
expect(window.procaptcha?.ready).toBeDefined();
|
|
51
|
+
expect(window.procaptcha?.reset).toBeDefined();
|
|
52
|
+
expect(window.procaptcha?.execute).toBeDefined();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Import the module which should set window.procaptcha and dispatch the event
|
|
56
|
+
import("../index.js");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import { JSDOM } from "jsdom";
|
|
16
|
+
|
|
17
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
18
|
+
import { extractParams, getProcaptchaScript } from "../../util/config.js";
|
|
19
|
+
|
|
20
|
+
describe("Config utility functions", () => {
|
|
21
|
+
let dom: JSDOM;
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
dom = new JSDOM("<!DOCTYPE html><html><body></body></html>", {
|
|
25
|
+
url: "https://example.com",
|
|
26
|
+
});
|
|
27
|
+
global.document = dom.window.document;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
// biome-ignore lint/suspicious/noExplicitAny: TODO fix any
|
|
32
|
+
(global as any).document = undefined;
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("getProcaptchaScript", () => {
|
|
36
|
+
it("should return null when no matching script is found", () => {
|
|
37
|
+
const result = getProcaptchaScript(["nonexistent"]);
|
|
38
|
+
expect(result).toBeNull();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should return the script element when a matching script is found", () => {
|
|
42
|
+
const script = document.createElement("script");
|
|
43
|
+
script.src = "https://example.com/procaptcha.js";
|
|
44
|
+
document.body.appendChild(script);
|
|
45
|
+
|
|
46
|
+
const result = getProcaptchaScript(["procaptcha"]);
|
|
47
|
+
expect(result).toBe(script);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("extractParams", () => {
|
|
52
|
+
it("should return undefined values when no matching script is found", () => {
|
|
53
|
+
const result = extractParams(["nonexistent"]);
|
|
54
|
+
expect(result).toEqual({
|
|
55
|
+
onloadUrlCallback: undefined,
|
|
56
|
+
renderExplicit: undefined,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should extract parameters from the script URL", () => {
|
|
61
|
+
const script = document.createElement("script");
|
|
62
|
+
script.src =
|
|
63
|
+
"https://example.com/procaptcha.js?onload=onloadCallback&render=explicit";
|
|
64
|
+
document.body.appendChild(script);
|
|
65
|
+
|
|
66
|
+
const result = extractParams(["procaptcha"]);
|
|
67
|
+
expect(result).toEqual({
|
|
68
|
+
onloadUrlCallback: "onloadCallback",
|
|
69
|
+
renderExplicit: "explicit",
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should return undefined for missing parameters", () => {
|
|
74
|
+
const script = document.createElement("script");
|
|
75
|
+
script.src = "https://example.com/procaptcha.js?onload=onloadCallback";
|
|
76
|
+
document.body.appendChild(script);
|
|
77
|
+
|
|
78
|
+
const result = extractParams(["procaptcha"]);
|
|
79
|
+
expect(result).toEqual({
|
|
80
|
+
onloadUrlCallback: "onloadCallback",
|
|
81
|
+
renderExplicit: undefined,
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("should handle scripts without query parameters", () => {
|
|
86
|
+
const script = document.createElement("script");
|
|
87
|
+
script.src = "https://example.com/procaptcha.js";
|
|
88
|
+
document.body.appendChild(script);
|
|
89
|
+
|
|
90
|
+
const result = extractParams(["procaptcha"]);
|
|
91
|
+
expect(result).toEqual({
|
|
92
|
+
onloadUrlCallback: undefined,
|
|
93
|
+
renderExplicit: undefined,
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import { ProcaptchaFrictionless } from "@prosopo/procaptcha-frictionless";
|
|
16
|
+
import type { Callbacks, ProcaptchaProps } from "@prosopo/types";
|
|
17
|
+
import { type DOMWindow, JSDOM } from "jsdom";
|
|
18
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
19
|
+
import { WidgetFactory } from "../../util/widgetFactory.js";
|
|
20
|
+
import { WidgetThemeResolver } from "../../util/widgetThemeResolver.js";
|
|
21
|
+
|
|
22
|
+
const widgetFactory = new WidgetFactory(new WidgetThemeResolver());
|
|
23
|
+
|
|
24
|
+
interface TestContext {
|
|
25
|
+
document?: Document;
|
|
26
|
+
window?: DOMWindow;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const callbacks: Callbacks = {
|
|
30
|
+
onHuman: () => console.log("onHuman"),
|
|
31
|
+
onChallengeExpired: () => console.log("onChallengeExpired"),
|
|
32
|
+
onExpired: () => console.log("onExpired"),
|
|
33
|
+
onError: () => console.log("onError"),
|
|
34
|
+
onClose: () => console.log("onClose"),
|
|
35
|
+
onOpen: () => console.log("onOpen"),
|
|
36
|
+
onFailed: () => console.log("onFailed"),
|
|
37
|
+
onReset: () => console.log("onReset"),
|
|
38
|
+
onExtensionNotFound: () => console.log("onExtensionNotFound"),
|
|
39
|
+
onReload: () => console.log("onReload"),
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
vi.mock("@prosopo/detector", async () => {
|
|
43
|
+
return {
|
|
44
|
+
isBot: async () => {
|
|
45
|
+
return { isBot: true };
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
vi.mock("@polkadot/extension-dapp", async () => {
|
|
51
|
+
return {
|
|
52
|
+
web3Enable: async () => {
|
|
53
|
+
return [];
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
vi.mock("@prosopo/procaptcha-frictionless", async () => {
|
|
59
|
+
return {
|
|
60
|
+
ProcaptchaFrictionless: vi.fn((props: ProcaptchaProps) => {
|
|
61
|
+
return (
|
|
62
|
+
// @ts-ignore
|
|
63
|
+
<div config={props.config} callbacks={props.callbacks}>
|
|
64
|
+
ProcaptchaFrictionless
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
}),
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("Config utility functions", () => {
|
|
72
|
+
let dom: JSDOM;
|
|
73
|
+
|
|
74
|
+
beforeEach<TestContext>((context) => {
|
|
75
|
+
dom = new JSDOM("<!DOCTYPE html><html><body></body></html>", {
|
|
76
|
+
url: "https://example.com",
|
|
77
|
+
});
|
|
78
|
+
context.document = dom.window.document;
|
|
79
|
+
context.window = dom.window;
|
|
80
|
+
// @ts-ignore
|
|
81
|
+
global.window = dom.window;
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
global.document = dom.window.document;
|
|
84
|
+
// @ts-ignore
|
|
85
|
+
global.HTMLElement = dom.window.HTMLElement;
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
global.Element = dom.window.Element;
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
global.Node = dom.window.Node;
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
global.navigator = dom.window.navigator;
|
|
92
|
+
vi.clearAllMocks();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
afterEach<TestContext>((context) => {
|
|
96
|
+
context.document = undefined;
|
|
97
|
+
context.window = undefined;
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
global.HTMLElement = undefined;
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
global.Element = undefined;
|
|
102
|
+
// @ts-ignore
|
|
103
|
+
global.Node = undefined;
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
global.navigator = undefined;
|
|
106
|
+
vi.clearAllMocks();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it<TestContext>("always mounts the unified frictionless dispatcher", async (ctx) => {
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
const script = ctx.document.createElement("script");
|
|
112
|
+
script.src = "https://example.com/procaptcha.js";
|
|
113
|
+
document.body.appendChild(script);
|
|
114
|
+
|
|
115
|
+
expect(ctx.window).toBeDefined();
|
|
116
|
+
|
|
117
|
+
if (!ctx.window) {
|
|
118
|
+
expect(true).toBe(false);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
await widgetFactory.createWidgets([script], {
|
|
123
|
+
siteKey: "1234",
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
await vi.waitFor(() => {
|
|
127
|
+
expect(ProcaptchaFrictionless).toHaveBeenCalledTimes(1);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it<TestContext>("ignores any legacy data-captcha-type attribute on the host element", async (ctx) => {
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
const script = ctx.document.createElement("script");
|
|
134
|
+
script.src = "https://example.com/procaptcha.js";
|
|
135
|
+
// Customers may still have this attribute on their HTML from before
|
|
136
|
+
// the server-driven captchaType refactor. The bundle must silently
|
|
137
|
+
// ignore it and always mount the unified dispatcher.
|
|
138
|
+
script.setAttribute("data-captcha-type", "image");
|
|
139
|
+
document.body.appendChild(script);
|
|
140
|
+
|
|
141
|
+
expect(ctx.window).toBeDefined();
|
|
142
|
+
|
|
143
|
+
if (!ctx.window) {
|
|
144
|
+
expect(true).toBe(false);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
await widgetFactory.createWidgets([script], {
|
|
149
|
+
siteKey: "1234",
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
await vi.waitFor(() => {
|
|
153
|
+
expect(ProcaptchaFrictionless).toHaveBeenCalledTimes(1);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it<TestContext>("user callbacks should be set when defined on the window and pulled in via data attributes", async (ctx) => {
|
|
158
|
+
// @ts-ignore
|
|
159
|
+
const script = ctx.document.createElement("script");
|
|
160
|
+
script.src = "https://example.com/procaptcha.js";
|
|
161
|
+
// @ts-ignore
|
|
162
|
+
script["data-callback"] = "onHuman";
|
|
163
|
+
// @ts-ignore
|
|
164
|
+
script["data-chalexpired-callback"] = "onChallengeExpired";
|
|
165
|
+
// @ts-ignore
|
|
166
|
+
script["data-expired-callback"] = "onExpired";
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
script["data-error-callback"] = "onError";
|
|
169
|
+
// @ts-ignore
|
|
170
|
+
script["data-close-callback"] = "onClose";
|
|
171
|
+
// @ts-ignore
|
|
172
|
+
script["data-open-callback"] = "onOpen";
|
|
173
|
+
// @ts-ignore
|
|
174
|
+
script["data-failed-callback"] = "onFailed";
|
|
175
|
+
// @ts-ignore
|
|
176
|
+
script["data-reset-callback"] = "onReset";
|
|
177
|
+
document.body.appendChild(script);
|
|
178
|
+
|
|
179
|
+
expect(ctx.window).toBeDefined();
|
|
180
|
+
|
|
181
|
+
if (!ctx.window) {
|
|
182
|
+
expect(true).toBe(false);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
ctx.window.onHuman = callbacks.onHuman;
|
|
187
|
+
ctx.window.onChallengeExpired = callbacks.onChallengeExpired;
|
|
188
|
+
ctx.window.onExpired = callbacks.onExpired;
|
|
189
|
+
ctx.window.onError = callbacks.onError;
|
|
190
|
+
ctx.window.onClose = callbacks.onClose;
|
|
191
|
+
ctx.window.onOpen = callbacks.onOpen;
|
|
192
|
+
ctx.window.onFailed = callbacks.onFailed;
|
|
193
|
+
ctx.window.onReset = callbacks.onReset;
|
|
194
|
+
ctx.window.onExtensionNotFound = callbacks.onExtensionNotFound;
|
|
195
|
+
ctx.window.onReload = callbacks.onReload;
|
|
196
|
+
|
|
197
|
+
await widgetFactory.createWidgets([script], {
|
|
198
|
+
siteKey: "1234",
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
await vi.waitFor(() => {
|
|
202
|
+
expect(ProcaptchaFrictionless).toHaveBeenCalledTimes(1);
|
|
203
|
+
// hack to get mock calls
|
|
204
|
+
// @ts-ignore
|
|
205
|
+
const call = ProcaptchaFrictionless.mock.calls[0];
|
|
206
|
+
const props = call[0];
|
|
207
|
+
|
|
208
|
+
// Check that the core properties we care about are present
|
|
209
|
+
expect(props.config.account.address).toBe("1234");
|
|
210
|
+
expect(props.config.web2).toBe(true);
|
|
211
|
+
expect(props.config.solutionThreshold).toBe(80);
|
|
212
|
+
expect(props.config.dappName).toBe("ProsopoClientDapp");
|
|
213
|
+
expect(props.config.theme).toBe("light");
|
|
214
|
+
expect(props.config.mode).toBe("visible");
|
|
215
|
+
|
|
216
|
+
// Check that callbacks object exists (it will be empty since we're not testing callback extraction here)
|
|
217
|
+
expect(props.callbacks).toBeDefined();
|
|
218
|
+
expect(typeof props.callbacks).toBe("object");
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
});
|