@prosopo/procaptcha-icon-order 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$cjs.log +52 -0
- package/.turbo/turbo-build$colon$tsc.log +53 -0
- package/.turbo/turbo-build.log +54 -0
- package/CHANGELOG.md +46 -0
- package/LICENSE +201 -0
- package/dist/_virtual/_rolldown/runtime.js +3 -0
- package/dist/cjs/components/iconOrderCanvas.cjs +321 -0
- package/dist/cjs/components/procaptchaIconOrder.cjs +7 -0
- package/dist/cjs/components/procaptchaWidget.cjs +198 -0
- package/dist/cjs/index.cjs +8 -0
- package/dist/cjs/services/Manager.cjs +207 -0
- package/dist/components/iconOrderCanvas.d.ts +20 -0
- package/dist/components/iconOrderCanvas.d.ts.map +1 -0
- package/dist/components/iconOrderCanvas.js +321 -0
- package/dist/components/iconOrderCanvas.js.map +1 -0
- package/dist/components/procaptchaIconOrder.d.ts +7 -0
- package/dist/components/procaptchaIconOrder.d.ts.map +1 -0
- package/dist/components/procaptchaIconOrder.js +6 -0
- package/dist/components/procaptchaIconOrder.js.map +1 -0
- package/dist/components/procaptchaWidget.d.ts +7 -0
- package/dist/components/procaptchaWidget.d.ts.map +1 -0
- package/dist/components/procaptchaWidget.js +198 -0
- package/dist/components/procaptchaWidget.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/services/Manager.d.ts +10 -0
- package/dist/services/Manager.d.ts.map +1 -0
- package/dist/services/Manager.js +206 -0
- package/dist/services/Manager.js.map +1 -0
- package/dist/tests/domHarness.d.ts +20 -0
- package/dist/tests/domHarness.d.ts.map +1 -0
- package/dist/tests/domHarness.js +56 -0
- package/dist/tests/domHarness.js.map +1 -0
- package/dist/tests/iconOrderCanvas.unit.test.d.ts +2 -0
- package/dist/tests/iconOrderCanvas.unit.test.d.ts.map +1 -0
- package/dist/tests/iconOrderCanvas.unit.test.js +415 -0
- package/dist/tests/iconOrderCanvas.unit.test.js.map +1 -0
- package/dist/tests/invisibleCanvasVisibility.unit.test.d.ts +2 -0
- package/dist/tests/invisibleCanvasVisibility.unit.test.d.ts.map +1 -0
- package/dist/tests/invisibleCanvasVisibility.unit.test.js +101 -0
- package/dist/tests/invisibleCanvasVisibility.unit.test.js.map +1 -0
- package/dist/tests/manager.unit.test.d.ts +2 -0
- package/dist/tests/manager.unit.test.d.ts.map +1 -0
- package/dist/tests/manager.unit.test.js +566 -0
- package/dist/tests/manager.unit.test.js.map +1 -0
- package/dist/tests/managerHarness.d.ts +21 -0
- package/dist/tests/managerHarness.d.ts.map +1 -0
- package/dist/tests/managerHarness.js +72 -0
- package/dist/tests/managerHarness.js.map +1 -0
- package/dist/tests/procaptchaIconOrder.test-d.d.ts +2 -0
- package/dist/tests/procaptchaIconOrder.test-d.d.ts.map +1 -0
- package/dist/tests/procaptchaIconOrder.test-d.js +102 -0
- package/dist/tests/procaptchaIconOrder.test-d.js.map +1 -0
- package/dist/tests/procaptchaIconOrder.unit.test.d.ts +2 -0
- package/dist/tests/procaptchaIconOrder.unit.test.d.ts.map +1 -0
- package/dist/tests/procaptchaIconOrder.unit.test.js +92 -0
- package/dist/tests/procaptchaIconOrder.unit.test.js.map +1 -0
- package/dist/tests/procaptchaWidget.unit.test.d.ts +2 -0
- package/dist/tests/procaptchaWidget.unit.test.d.ts.map +1 -0
- package/dist/tests/procaptchaWidget.unit.test.js +607 -0
- package/dist/tests/procaptchaWidget.unit.test.js.map +1 -0
- package/package.json +70 -3
- package/vite.cjs.config.ts +22 -0
- package/vite.esm.config.ts +20 -0
- package/vite.test.config.ts +26 -0
- package/README.md +0 -3
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
let _prosopo_procaptcha_common = require("@prosopo/procaptcha-common");
|
|
2
|
+
//#region src/components/iconOrderCanvas.ts
|
|
3
|
+
/**
|
|
4
|
+
* Must match `DEFAULT_GEOMETRY` in @prosopo/icon-order-assets: the provider
|
|
5
|
+
* grades clicks in this coordinate space.
|
|
6
|
+
*/
|
|
7
|
+
var CONTAINER_WIDTH = 300;
|
|
8
|
+
var CONTAINER_HEIGHT = 200;
|
|
9
|
+
var MARKER_RADIUS = 13;
|
|
10
|
+
var SHAKE_MS = 500;
|
|
11
|
+
/** `MouseEvent.button` for a primary click, a touch or a pen contact. */
|
|
12
|
+
var MAIN_BUTTON = 0;
|
|
13
|
+
var SHAKE_KEYFRAMES = `
|
|
14
|
+
@keyframes prosopo-icon-order-shake {
|
|
15
|
+
0%, 100% { transform: translateX(0); }
|
|
16
|
+
10%, 30%, 50%, 70%, 90% { transform: translateX(-4px); }
|
|
17
|
+
20%, 40%, 60%, 80% { transform: translateX(4px); }
|
|
18
|
+
}
|
|
19
|
+
`;
|
|
20
|
+
var mountIconOrderCanvas = (initialProps) => {
|
|
21
|
+
const teardown = new _prosopo_procaptcha_common.Teardown();
|
|
22
|
+
let props = initialProps;
|
|
23
|
+
let clicks = [];
|
|
24
|
+
let events = [];
|
|
25
|
+
let startedAt = Date.now();
|
|
26
|
+
let visible = false;
|
|
27
|
+
let shaking = false;
|
|
28
|
+
let shakeTimer;
|
|
29
|
+
const markers = [];
|
|
30
|
+
const t = (key, options) => props.translator.t(key, options);
|
|
31
|
+
const style = (0, _prosopo_procaptcha_common.createElement)("style", { text: SHAKE_KEYFRAMES });
|
|
32
|
+
const instruction = (0, _prosopo_procaptcha_common.createElement)("span");
|
|
33
|
+
const legend = (0, _prosopo_procaptcha_common.createElement)("img", {
|
|
34
|
+
style: {
|
|
35
|
+
imageRendering: "auto",
|
|
36
|
+
userSelect: "none"
|
|
37
|
+
},
|
|
38
|
+
attributes: { draggable: "false" }
|
|
39
|
+
});
|
|
40
|
+
const header = (0, _prosopo_procaptcha_common.createElement)("div", {
|
|
41
|
+
style: {
|
|
42
|
+
borderRadius: "20px 20px 0 0",
|
|
43
|
+
padding: "10px 16px",
|
|
44
|
+
width: `${CONTAINER_WIDTH}px`,
|
|
45
|
+
boxSizing: "border-box",
|
|
46
|
+
display: "flex",
|
|
47
|
+
alignItems: "center",
|
|
48
|
+
justifyContent: "space-between",
|
|
49
|
+
gap: "10px",
|
|
50
|
+
fontSize: "14px",
|
|
51
|
+
fontWeight: 500,
|
|
52
|
+
transition: "color 0.3s ease, border-color 0.3s ease"
|
|
53
|
+
},
|
|
54
|
+
children: [instruction, legend]
|
|
55
|
+
});
|
|
56
|
+
const background = (0, _prosopo_procaptcha_common.createElement)("img", {
|
|
57
|
+
style: {
|
|
58
|
+
position: "absolute",
|
|
59
|
+
inset: 0,
|
|
60
|
+
width: `${CONTAINER_WIDTH}px`,
|
|
61
|
+
height: `${CONTAINER_HEIGHT}px`,
|
|
62
|
+
pointerEvents: "none",
|
|
63
|
+
userSelect: "none"
|
|
64
|
+
},
|
|
65
|
+
attributes: {
|
|
66
|
+
alt: "",
|
|
67
|
+
draggable: "false"
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
const frame = (0, _prosopo_procaptcha_common.createElement)("div", {
|
|
71
|
+
style: {
|
|
72
|
+
position: "relative",
|
|
73
|
+
width: `${CONTAINER_WIDTH}px`,
|
|
74
|
+
height: `${CONTAINER_HEIGHT}px`,
|
|
75
|
+
overflow: "hidden",
|
|
76
|
+
userSelect: "none",
|
|
77
|
+
touchAction: "none",
|
|
78
|
+
transition: "opacity 0.2s ease"
|
|
79
|
+
},
|
|
80
|
+
children: [background]
|
|
81
|
+
});
|
|
82
|
+
const resetButton = (0, _prosopo_procaptcha_common.createElement)("button", {
|
|
83
|
+
attributes: { type: "button" },
|
|
84
|
+
style: {
|
|
85
|
+
flex: "0 0 auto",
|
|
86
|
+
padding: "8px 12px",
|
|
87
|
+
borderRadius: "10px",
|
|
88
|
+
background: "transparent",
|
|
89
|
+
fontSize: "13px"
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
const submitButton = (0, _prosopo_procaptcha_common.createElement)("button", {
|
|
93
|
+
attributes: { type: "button" },
|
|
94
|
+
style: {
|
|
95
|
+
flex: "1 1 auto",
|
|
96
|
+
padding: "8px 12px",
|
|
97
|
+
borderRadius: "10px",
|
|
98
|
+
border: "none",
|
|
99
|
+
fontSize: "14px",
|
|
100
|
+
fontWeight: 600
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
if ("production" !== process.env.NODE_ENV) {
|
|
104
|
+
(0, _prosopo_procaptcha_common.applyAttributes)(frame, { "data-cy": "prosopo-icon-order-frame" });
|
|
105
|
+
(0, _prosopo_procaptcha_common.applyAttributes)(resetButton, { "data-cy": "prosopo-icon-order-reset" });
|
|
106
|
+
(0, _prosopo_procaptcha_common.applyAttributes)(submitButton, { "data-cy": "prosopo-icon-order-submit" });
|
|
107
|
+
}
|
|
108
|
+
const controls = (0, _prosopo_procaptcha_common.createElement)("div", {
|
|
109
|
+
style: {
|
|
110
|
+
borderRadius: "0 0 20px 20px",
|
|
111
|
+
padding: "10px 16px",
|
|
112
|
+
width: `${CONTAINER_WIDTH}px`,
|
|
113
|
+
boxSizing: "border-box",
|
|
114
|
+
display: "flex",
|
|
115
|
+
alignItems: "center",
|
|
116
|
+
gap: "10px"
|
|
117
|
+
},
|
|
118
|
+
children: [resetButton, submitButton]
|
|
119
|
+
});
|
|
120
|
+
const panel = (0, _prosopo_procaptcha_common.createElement)("div", {
|
|
121
|
+
style: {
|
|
122
|
+
display: "flex",
|
|
123
|
+
flexDirection: "column",
|
|
124
|
+
alignItems: "center",
|
|
125
|
+
transition: "opacity 0.3s ease, transform 0.3s ease"
|
|
126
|
+
},
|
|
127
|
+
children: [
|
|
128
|
+
header,
|
|
129
|
+
frame,
|
|
130
|
+
controls
|
|
131
|
+
]
|
|
132
|
+
});
|
|
133
|
+
const surfaceProps = () => ({
|
|
134
|
+
show: true,
|
|
135
|
+
placement: props.placement,
|
|
136
|
+
anchor: props.anchor,
|
|
137
|
+
onDismiss: props.onDismiss,
|
|
138
|
+
scrim: visible ? "dim" : "none",
|
|
139
|
+
dialogLabel: t("WIDGET.ICON_ORDER.DIALOG_LABEL", { defaultValue: "Icon order challenge" })
|
|
140
|
+
});
|
|
141
|
+
const surface = (0, _prosopo_procaptcha_common.mountChallengeSurface)(surfaceProps());
|
|
142
|
+
surface.content.append(style, panel);
|
|
143
|
+
const audioAlternativeFooterProps = () => ({
|
|
144
|
+
offer: props.audioAlternative,
|
|
145
|
+
theme: props.theme,
|
|
146
|
+
width: CONTAINER_WIDTH
|
|
147
|
+
});
|
|
148
|
+
const audioAlternativeFooter = (0, _prosopo_procaptcha_common.mountAudioAlternativeFooter)(panel, audioAlternativeFooterProps());
|
|
149
|
+
const createMarker = () => (0, _prosopo_procaptcha_common.createElement)("div", { style: {
|
|
150
|
+
position: "absolute",
|
|
151
|
+
width: `${MARKER_RADIUS * 2}px`,
|
|
152
|
+
height: `${MARKER_RADIUS * 2}px`,
|
|
153
|
+
borderRadius: "50%",
|
|
154
|
+
border: "2px solid rgba(255, 255, 255, 0.9)",
|
|
155
|
+
boxShadow: "0 2px 6px rgba(0, 0, 0, 0.45)",
|
|
156
|
+
display: "flex",
|
|
157
|
+
alignItems: "center",
|
|
158
|
+
justifyContent: "center",
|
|
159
|
+
fontSize: "12px",
|
|
160
|
+
fontWeight: 700,
|
|
161
|
+
pointerEvents: "none",
|
|
162
|
+
userSelect: "none"
|
|
163
|
+
} });
|
|
164
|
+
const renderMarkers = () => {
|
|
165
|
+
while (markers.length > clicks.length) markers.pop()?.remove();
|
|
166
|
+
while (markers.length < clicks.length) {
|
|
167
|
+
const marker = createMarker();
|
|
168
|
+
frame.appendChild(marker);
|
|
169
|
+
markers.push(marker);
|
|
170
|
+
}
|
|
171
|
+
const { theme } = props;
|
|
172
|
+
clicks.forEach((click, index) => {
|
|
173
|
+
const marker = markers[index];
|
|
174
|
+
if (!marker) return;
|
|
175
|
+
marker.textContent = String(index + 1);
|
|
176
|
+
(0, _prosopo_procaptcha_common.applyStyles)(marker, {
|
|
177
|
+
left: `${click.x - MARKER_RADIUS}px`,
|
|
178
|
+
top: `${click.y - MARKER_RADIUS}px`,
|
|
179
|
+
backgroundColor: theme.palette.primary.main,
|
|
180
|
+
color: theme.palette.background.default,
|
|
181
|
+
fontFamily: theme.font.fontFamily
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
const render = () => {
|
|
186
|
+
const { theme, showRetry, submitting } = props;
|
|
187
|
+
const empty = 0 === clicks.length;
|
|
188
|
+
const inactive = submitting || empty;
|
|
189
|
+
(0, _prosopo_procaptcha_common.applyStyles)(panel, {
|
|
190
|
+
opacity: visible ? 1 : 0,
|
|
191
|
+
transform: visible ? "scale(1)" : "scale(0.9)",
|
|
192
|
+
animation: shaking ? `prosopo-icon-order-shake ${SHAKE_MS}ms ease` : "none"
|
|
193
|
+
});
|
|
194
|
+
instruction.textContent = showRetry ? t("WIDGET.ICON_ORDER.RETRY", { defaultValue: "Not quite — try again" }) : t("WIDGET.ICON_ORDER.SELECT", { defaultValue: "Select in this order" });
|
|
195
|
+
(0, _prosopo_procaptcha_common.applyStyles)(header, {
|
|
196
|
+
backgroundColor: theme.palette.surface,
|
|
197
|
+
fontFamily: theme.font.fontFamily,
|
|
198
|
+
color: showRetry ? theme.palette.error.main : theme.palette.onSurface,
|
|
199
|
+
borderBottom: `2px solid ${showRetry ? theme.palette.error.main : "transparent"}`
|
|
200
|
+
});
|
|
201
|
+
legend.src = props.legend;
|
|
202
|
+
legend.alt = t("WIDGET.ICON_ORDER.LEGEND_ALT", { defaultValue: "Icons to select, in order" });
|
|
203
|
+
(0, _prosopo_procaptcha_common.applyStyles)(legend, { height: `${props.legendIconSize}px` });
|
|
204
|
+
background.src = props.background;
|
|
205
|
+
(0, _prosopo_procaptcha_common.applyStyles)(frame, {
|
|
206
|
+
background: `linear-gradient(135deg, ${theme.palette.surface} 0%, ${theme.palette.primaryContainer.main} 50%, ${theme.palette.surface} 100%)`,
|
|
207
|
+
cursor: submitting ? "default" : "pointer",
|
|
208
|
+
opacity: submitting ? .6 : 1,
|
|
209
|
+
pointerEvents: submitting ? "none" : "auto"
|
|
210
|
+
});
|
|
211
|
+
renderMarkers();
|
|
212
|
+
(0, _prosopo_procaptcha_common.applyStyles)(controls, {
|
|
213
|
+
backgroundColor: theme.palette.surface,
|
|
214
|
+
borderRadius: props.audioAlternative ? "0" : "0 0 20px 20px"
|
|
215
|
+
});
|
|
216
|
+
audioAlternativeFooter.update(audioAlternativeFooterProps());
|
|
217
|
+
resetButton.textContent = t("WIDGET.ICON_ORDER.RESET", { defaultValue: "Reset" });
|
|
218
|
+
resetButton.disabled = inactive;
|
|
219
|
+
(0, _prosopo_procaptcha_common.applyStyles)(resetButton, {
|
|
220
|
+
border: `1px solid ${theme.palette.border}`,
|
|
221
|
+
color: theme.palette.onSurface,
|
|
222
|
+
fontFamily: theme.font.fontFamily,
|
|
223
|
+
cursor: inactive ? "default" : "pointer",
|
|
224
|
+
opacity: inactive ? .5 : 1
|
|
225
|
+
});
|
|
226
|
+
submitButton.textContent = submitting ? t("WIDGET.ICON_ORDER.CHECKING", { defaultValue: "Checking…" }) : t("WIDGET.ICON_ORDER.SUBMIT", { defaultValue: "OK" });
|
|
227
|
+
submitButton.disabled = inactive;
|
|
228
|
+
(0, _prosopo_procaptcha_common.applyStyles)(submitButton, {
|
|
229
|
+
backgroundColor: theme.palette.primary.main,
|
|
230
|
+
color: theme.palette.background.default,
|
|
231
|
+
fontFamily: theme.font.fontFamily,
|
|
232
|
+
cursor: inactive ? "default" : "pointer",
|
|
233
|
+
opacity: inactive ? .5 : 1
|
|
234
|
+
});
|
|
235
|
+
surface.update(surfaceProps());
|
|
236
|
+
};
|
|
237
|
+
const clearAnswer = () => {
|
|
238
|
+
clicks = [];
|
|
239
|
+
events = [];
|
|
240
|
+
startedAt = Date.now();
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* Scales by the frame's measured box rather than the constants, so a host
|
|
244
|
+
* page that transforms the widget still reports clicks in the provider's
|
|
245
|
+
* coordinate space.
|
|
246
|
+
*/
|
|
247
|
+
const toFramePoint = (event) => {
|
|
248
|
+
if (props.submitting || !(event instanceof MouseEvent)) return null;
|
|
249
|
+
const rect = frame.getBoundingClientRect();
|
|
250
|
+
if (0 === rect.width || 0 === rect.height) return null;
|
|
251
|
+
return {
|
|
252
|
+
x: (event.clientX - rect.left) / rect.width * CONTAINER_WIDTH,
|
|
253
|
+
y: (event.clientY - rect.top) / rect.height * CONTAINER_HEIGHT
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
const recordEvent = (point) => {
|
|
257
|
+
events.push({
|
|
258
|
+
x: point.x,
|
|
259
|
+
y: point.y,
|
|
260
|
+
t: Date.now() - startedAt
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
teardown.addEventListener(frame, "pointermove", (event) => {
|
|
264
|
+
const point = toFramePoint(event);
|
|
265
|
+
if (point) recordEvent(point);
|
|
266
|
+
});
|
|
267
|
+
teardown.addEventListener(frame, "pointerup", (event) => {
|
|
268
|
+
if (event instanceof MouseEvent && MAIN_BUTTON !== event.button) return;
|
|
269
|
+
const point = toFramePoint(event);
|
|
270
|
+
if (!point) return;
|
|
271
|
+
recordEvent(point);
|
|
272
|
+
clicks = [...clicks, point];
|
|
273
|
+
render();
|
|
274
|
+
});
|
|
275
|
+
teardown.addEventListener(resetButton, "click", () => {
|
|
276
|
+
clearAnswer();
|
|
277
|
+
render();
|
|
278
|
+
});
|
|
279
|
+
teardown.addEventListener(submitButton, "click", () => {
|
|
280
|
+
if (props.submitting || 0 === clicks.length) return;
|
|
281
|
+
props.onComplete([...clicks], [...events]);
|
|
282
|
+
});
|
|
283
|
+
render();
|
|
284
|
+
const frameRequest = requestAnimationFrame(() => {
|
|
285
|
+
visible = true;
|
|
286
|
+
render();
|
|
287
|
+
});
|
|
288
|
+
teardown.add(() => cancelAnimationFrame(frameRequest));
|
|
289
|
+
const startShake = () => {
|
|
290
|
+
shaking = true;
|
|
291
|
+
render();
|
|
292
|
+
if (void 0 !== shakeTimer) clearTimeout(shakeTimer);
|
|
293
|
+
shakeTimer = setTimeout(() => {
|
|
294
|
+
shaking = false;
|
|
295
|
+
render();
|
|
296
|
+
}, SHAKE_MS);
|
|
297
|
+
};
|
|
298
|
+
teardown.add(() => {
|
|
299
|
+
if (void 0 !== shakeTimer) clearTimeout(shakeTimer);
|
|
300
|
+
});
|
|
301
|
+
if (props.showRetry) startShake();
|
|
302
|
+
return {
|
|
303
|
+
update: (nextProps) => {
|
|
304
|
+
const previous = props;
|
|
305
|
+
props = nextProps;
|
|
306
|
+
if (nextProps.background !== previous.background || nextProps.legend !== previous.legend) clearAnswer();
|
|
307
|
+
if (nextProps.showRetry && !previous.showRetry) {
|
|
308
|
+
startShake();
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
render();
|
|
312
|
+
},
|
|
313
|
+
destroy: () => {
|
|
314
|
+
teardown.run();
|
|
315
|
+
audioAlternativeFooter.destroy();
|
|
316
|
+
surface.destroy();
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
};
|
|
320
|
+
//#endregion
|
|
321
|
+
exports.mountIconOrderCanvas = mountIconOrderCanvas;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
let _prosopo_procaptcha_common = require("@prosopo/procaptcha-common");
|
|
2
|
+
//#region src/components/procaptchaIconOrder.ts
|
|
3
|
+
var loadProcaptchaIconOrder = async () => (await Promise.resolve().then(() => require("./procaptchaWidget.cjs"))).mountProcaptchaIconOrderWidget;
|
|
4
|
+
var mountProcaptchaIconOrder = (0, _prosopo_procaptcha_common.lazyMount)(loadProcaptchaIconOrder);
|
|
5
|
+
//#endregion
|
|
6
|
+
exports.loadProcaptchaIconOrder = loadProcaptchaIconOrder;
|
|
7
|
+
exports.mountProcaptchaIconOrder = mountProcaptchaIconOrder;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
const require_Manager = require("../services/Manager.cjs");
|
|
2
|
+
const require_iconOrderCanvas = require("./iconOrderCanvas.cjs");
|
|
3
|
+
let _prosopo_locale = require("@prosopo/locale");
|
|
4
|
+
let _prosopo_procaptcha_common = require("@prosopo/procaptcha-common");
|
|
5
|
+
let _prosopo_types = require("@prosopo/types");
|
|
6
|
+
let _prosopo_widget_skeleton = require("@prosopo/widget-skeleton");
|
|
7
|
+
//#region src/components/procaptchaWidget.ts
|
|
8
|
+
var mountProcaptchaIconOrderWidget = (container, props) => {
|
|
9
|
+
const teardown = new _prosopo_procaptcha_common.Teardown();
|
|
10
|
+
const config = props.config;
|
|
11
|
+
const i18n = props.i18n;
|
|
12
|
+
const frictionlessState = props.frictionlessState;
|
|
13
|
+
const callbacks = props.callbacks || {};
|
|
14
|
+
const translator = (0, _prosopo_locale.createTranslator)(i18n);
|
|
15
|
+
const isInvisible = _prosopo_types.ModeEnum.invisible === config.mode;
|
|
16
|
+
const theme = "light" === config.theme ? _prosopo_widget_skeleton.lightTheme : _prosopo_widget_skeleton.darkTheme;
|
|
17
|
+
const store = (0, _prosopo_procaptcha_common.createProcaptchaState)();
|
|
18
|
+
let loading = false;
|
|
19
|
+
let iconOrderPhase = "checkbox";
|
|
20
|
+
let challengeData = null;
|
|
21
|
+
let showRetry = true === props.startShowRetry;
|
|
22
|
+
let lastError = store.state.error;
|
|
23
|
+
let lastCoords = null;
|
|
24
|
+
let sessionInvalidatedFired = false;
|
|
25
|
+
let honeypot;
|
|
26
|
+
let checkbox;
|
|
27
|
+
let canvas;
|
|
28
|
+
const updateState = (0, _prosopo_procaptcha_common.buildUpdateState)(store.state, store.update);
|
|
29
|
+
const manager = require_Manager.Manager(config, store.state, updateState, callbacks, frictionlessState, () => honeypot?.getValue());
|
|
30
|
+
const root = (0, _prosopo_procaptcha_common.createElement)("div");
|
|
31
|
+
container.appendChild(root);
|
|
32
|
+
if (frictionlessState?.hp) honeypot = (0, _prosopo_procaptcha_common.mountHoneypot)(root, { encodedQuestion: frictionlessState.hp });
|
|
33
|
+
const reportError = (error) => {
|
|
34
|
+
callbacks.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
35
|
+
};
|
|
36
|
+
const returnToCheckbox = () => {
|
|
37
|
+
iconOrderPhase = "checkbox";
|
|
38
|
+
challengeData = null;
|
|
39
|
+
showRetry = false;
|
|
40
|
+
};
|
|
41
|
+
const showChallenge = (challenge) => {
|
|
42
|
+
if (challenge) {
|
|
43
|
+
challengeData = challenge;
|
|
44
|
+
iconOrderPhase = "selecting";
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const handleIconOrderComplete = async (clicks, iconOrderEvents) => {
|
|
48
|
+
iconOrderPhase = "submitting";
|
|
49
|
+
scheduler.schedule();
|
|
50
|
+
let verified = false;
|
|
51
|
+
try {
|
|
52
|
+
verified = await manager.submitSolution(clicks, iconOrderEvents);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
reportError(error);
|
|
55
|
+
}
|
|
56
|
+
if (verified) {
|
|
57
|
+
returnToCheckbox();
|
|
58
|
+
loading = false;
|
|
59
|
+
scheduler.schedule();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
showRetry = true;
|
|
63
|
+
iconOrderPhase = "selecting";
|
|
64
|
+
scheduler.schedule();
|
|
65
|
+
if (frictionlessState?.sessionId && props.onReload) {
|
|
66
|
+
iconOrderPhase = "submitting";
|
|
67
|
+
scheduler.schedule();
|
|
68
|
+
props.onReload(lastCoords?.x, lastCoords?.y, { showRetry: true });
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const newChallenge = await manager.start();
|
|
73
|
+
if (newChallenge) challengeData = newChallenge;
|
|
74
|
+
else returnToCheckbox();
|
|
75
|
+
} catch {
|
|
76
|
+
returnToCheckbox();
|
|
77
|
+
}
|
|
78
|
+
loading = false;
|
|
79
|
+
scheduler.schedule();
|
|
80
|
+
};
|
|
81
|
+
const handleDismiss = () => {
|
|
82
|
+
returnToCheckbox();
|
|
83
|
+
loading = false;
|
|
84
|
+
scheduler.schedule();
|
|
85
|
+
};
|
|
86
|
+
const canvasProps = (challenge) => ({
|
|
87
|
+
background: challenge.background,
|
|
88
|
+
legend: challenge.legend,
|
|
89
|
+
legendIconSize: challenge.legendIconSize,
|
|
90
|
+
onComplete: (clicks, events) => {
|
|
91
|
+
handleIconOrderComplete(clicks, events);
|
|
92
|
+
},
|
|
93
|
+
showRetry,
|
|
94
|
+
submitting: "submitting" === iconOrderPhase,
|
|
95
|
+
theme,
|
|
96
|
+
translator,
|
|
97
|
+
placement: config.placement,
|
|
98
|
+
anchor: props.container,
|
|
99
|
+
onDismiss: handleDismiss,
|
|
100
|
+
audioAlternative: (0, _prosopo_procaptcha_common.audioAlternativeOffer)(props, translator.isReady() ? translator.t("WIDGET.AUDIO_ALTERNATIVE") : "")
|
|
101
|
+
});
|
|
102
|
+
const runErrorEffect = () => {
|
|
103
|
+
if (store.state.error === lastError) return;
|
|
104
|
+
lastError = store.state.error;
|
|
105
|
+
if (!store.state.error) return;
|
|
106
|
+
loading = false;
|
|
107
|
+
returnToCheckbox();
|
|
108
|
+
if ("CAPTCHA.NO_SESSION_FOUND" !== store.state.error.key) return;
|
|
109
|
+
if (props.onSessionInvalidated && !sessionInvalidatedFired || void 0 !== frictionlessState) {
|
|
110
|
+
loading = true;
|
|
111
|
+
store.update({ error: void 0 });
|
|
112
|
+
}
|
|
113
|
+
if (props.onSessionInvalidated && !sessionInvalidatedFired) {
|
|
114
|
+
sessionInvalidatedFired = true;
|
|
115
|
+
props.onSessionInvalidated(lastCoords?.x, lastCoords?.y);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (frictionlessState) {
|
|
119
|
+
const timer = setTimeout(() => {
|
|
120
|
+
frictionlessState.restart();
|
|
121
|
+
}, 100);
|
|
122
|
+
teardown.add(() => clearTimeout(timer));
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const render = () => {
|
|
126
|
+
runErrorEffect();
|
|
127
|
+
if (("selecting" === iconOrderPhase || "submitting" === iconOrderPhase) && null !== challengeData) if (void 0 === canvas) canvas = require_iconOrderCanvas.mountIconOrderCanvas(canvasProps(challengeData));
|
|
128
|
+
else canvas.update(canvasProps(challengeData));
|
|
129
|
+
else {
|
|
130
|
+
canvas?.destroy();
|
|
131
|
+
canvas = void 0;
|
|
132
|
+
}
|
|
133
|
+
checkbox?.update(checkboxProps());
|
|
134
|
+
};
|
|
135
|
+
const scheduler = (0, _prosopo_procaptcha_common.createRenderScheduler)(render);
|
|
136
|
+
const beginChallenge = async (coords) => {
|
|
137
|
+
if (loading) return;
|
|
138
|
+
loading = true;
|
|
139
|
+
showRetry = false;
|
|
140
|
+
scheduler.schedule();
|
|
141
|
+
if (coords) lastCoords = coords;
|
|
142
|
+
try {
|
|
143
|
+
showChallenge(await manager.start(coords?.x, coords?.y));
|
|
144
|
+
} catch (error) {
|
|
145
|
+
reportError(error);
|
|
146
|
+
} finally {
|
|
147
|
+
loading = false;
|
|
148
|
+
scheduler.schedule();
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const checkboxProps = () => ({
|
|
152
|
+
checked: store.state.isHuman,
|
|
153
|
+
theme,
|
|
154
|
+
labelText: translator.isReady() ? translator.t("WIDGET.I_AM_HUMAN") : "",
|
|
155
|
+
error: store.state.error ? (0, _prosopo_locale.localiseErrorMessage)(translator.i18n, store.state.error) : void 0,
|
|
156
|
+
loadingText: translator.t("WIDGET.CHECKING", { defaultValue: "Checking that you are human" }),
|
|
157
|
+
loading: loading || "submitting" === iconOrderPhase,
|
|
158
|
+
onChange: (event) => beginChallenge((0, _prosopo_procaptcha_common.trustedClickCoords)(event))
|
|
159
|
+
});
|
|
160
|
+
if (!isInvisible) checkbox = (0, _prosopo_procaptcha_common.mountCheckbox)(root, checkboxProps());
|
|
161
|
+
teardown.add(scheduler.cancel);
|
|
162
|
+
teardown.add(store.subscribe(scheduler.schedule));
|
|
163
|
+
teardown.add(translator.subscribe(scheduler.schedule));
|
|
164
|
+
const handleExecute = () => {
|
|
165
|
+
beginChallenge();
|
|
166
|
+
};
|
|
167
|
+
if (props.container) teardown.addEventListener(props.container, _prosopo_procaptcha_common.PROCAPTCHA_EXECUTE_EVENT, handleExecute);
|
|
168
|
+
if (isInvisible) teardown.addEventListener(document, _prosopo_procaptcha_common.PROCAPTCHA_EXECUTE_EVENT, handleExecute);
|
|
169
|
+
if (config.language) if (i18n) {
|
|
170
|
+
if (i18n.language !== config.language) i18n.changeLanguage(config.language);
|
|
171
|
+
} else (0, _prosopo_locale.loadI18next)(false, config.language);
|
|
172
|
+
if (props.autoStart) {
|
|
173
|
+
loading = true;
|
|
174
|
+
const coords = props.startCoords;
|
|
175
|
+
lastCoords = coords ?? null;
|
|
176
|
+
scheduler.schedule();
|
|
177
|
+
manager.start(coords?.x ?? 0, coords?.y ?? 0).then((challenge) => {
|
|
178
|
+
showChallenge(challenge);
|
|
179
|
+
loading = false;
|
|
180
|
+
scheduler.schedule();
|
|
181
|
+
}, () => {
|
|
182
|
+
loading = false;
|
|
183
|
+
scheduler.schedule();
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
render();
|
|
187
|
+
return { destroy: () => {
|
|
188
|
+
manager.dispose();
|
|
189
|
+
teardown.run();
|
|
190
|
+
canvas?.destroy();
|
|
191
|
+
checkbox?.destroy();
|
|
192
|
+
honeypot?.destroy();
|
|
193
|
+
root.parentNode?.removeChild(root);
|
|
194
|
+
} };
|
|
195
|
+
};
|
|
196
|
+
//#endregion
|
|
197
|
+
exports.default = mountProcaptchaIconOrderWidget;
|
|
198
|
+
exports.mountProcaptchaIconOrderWidget = mountProcaptchaIconOrderWidget;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_iconOrderCanvas = require("./components/iconOrderCanvas.cjs");
|
|
3
|
+
const require_procaptchaWidget = require("./components/procaptchaWidget.cjs");
|
|
4
|
+
const require_procaptchaIconOrder = require("./components/procaptchaIconOrder.cjs");
|
|
5
|
+
exports.loadProcaptchaIconOrder = require_procaptchaIconOrder.loadProcaptchaIconOrder;
|
|
6
|
+
exports.mountIconOrderCanvas = require_iconOrderCanvas.mountIconOrderCanvas;
|
|
7
|
+
exports.mountProcaptchaIconOrder = require_procaptchaIconOrder.mountProcaptchaIconOrder;
|
|
8
|
+
exports.mountProcaptchaIconOrderWidget = require_procaptchaWidget.default;
|