xq-slide-captcha 0.0.0 → 1.1.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/LICENSE +661 -0
- package/README.md +362 -28
- package/dist/css/xq-slide-captcha.css +162 -0
- package/dist/css/xq-slide-captcha.css.map +1 -0
- package/dist/css/xq-slide-captcha.min.css +7 -0
- package/dist/index.cjs +843 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.mjs +833 -0
- package/dist/js/xq-slide-captcha.js +3208 -0
- package/dist/js/xq-slide-captcha.min.js +7 -0
- package/package.json +59 -6
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,843 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* xq-slide-captcha v1.1.0 (https://xqkeji.cn/demo/xq-slide-captcha)
|
|
3
|
+
* Author xqkeji.cn
|
|
4
|
+
* LICENSE AGPL-3.0-only
|
|
5
|
+
* Copyright 2026 xqkeji.cn
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
10
|
+
|
|
11
|
+
const xqCaptchaCore = require('xq-captcha-core');
|
|
12
|
+
|
|
13
|
+
const DEFAULT_OPTIONS = {
|
|
14
|
+
...xqCaptchaCore.BASE_OPTIONS,
|
|
15
|
+
slideText: "\u62D6\u52A8\u5DE6\u8FB9\u6ED1\u5757\u5B8C\u6210\u4E0A\u65B9\u62FC\u56FE"
|
|
16
|
+
};
|
|
17
|
+
function resolveOptions(overrides = {}) {
|
|
18
|
+
return xqCaptchaCore.resolveOptions(DEFAULT_OPTIONS, overrides);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const SEL = {
|
|
22
|
+
/** 唯一公开的触发选择器,宿主页面按此放置触发元素。 */
|
|
23
|
+
trigger: ".xq-captcha",
|
|
24
|
+
modal: "#xq-slide-captcha-modal",
|
|
25
|
+
dialog: ".xq-captcha-dialog",
|
|
26
|
+
content: ".xq-captcha-content",
|
|
27
|
+
loading: ".xq-captcha-loading",
|
|
28
|
+
canvasWrap: ".xq-captcha-canvas-wrap",
|
|
29
|
+
bgCanvas: ".xq-captcha-bg-canvas",
|
|
30
|
+
markCanvas: ".xq-captcha-mark-canvas",
|
|
31
|
+
msgError: ".xq-captcha-alert-error",
|
|
32
|
+
msgOk: ".xq-captcha-alert-success",
|
|
33
|
+
slideArea: ".xq-captcha-slide-area",
|
|
34
|
+
slideBlock: ".xq-captcha-slide-block",
|
|
35
|
+
slideText: ".xq-captcha-slide-hint",
|
|
36
|
+
close: ".xq-captcha-close",
|
|
37
|
+
refresh: ".xq-captcha-refresh",
|
|
38
|
+
copyright: ".xq-captcha-copyright",
|
|
39
|
+
progressBar: ".xq-captcha-progress-bar"
|
|
40
|
+
};
|
|
41
|
+
const CLS = {
|
|
42
|
+
ripple: "xq-captcha-ripple",
|
|
43
|
+
verified: "xq-captcha-verified",
|
|
44
|
+
dragging: "xq-captcha-dragging",
|
|
45
|
+
shake: "xq-captcha-shake",
|
|
46
|
+
loadingError: "xq-captcha-loading-error",
|
|
47
|
+
hidden: "d-none"
|
|
48
|
+
};
|
|
49
|
+
const BOUND_ATTR = "xqCaptchaBound";
|
|
50
|
+
|
|
51
|
+
function context2d(canvas, willReadFrequently = false) {
|
|
52
|
+
return canvas.getContext("2d", { willReadFrequently });
|
|
53
|
+
}
|
|
54
|
+
class CanvasPair {
|
|
55
|
+
constructor(dom, state) {
|
|
56
|
+
this.dom = dom;
|
|
57
|
+
this.state = state;
|
|
58
|
+
}
|
|
59
|
+
get bg() {
|
|
60
|
+
return this.dom.query(SEL.bgCanvas);
|
|
61
|
+
}
|
|
62
|
+
get mark() {
|
|
63
|
+
return this.dom.query(SEL.markCanvas);
|
|
64
|
+
}
|
|
65
|
+
/** 画布像素尺寸随后端下发的 bg_width / bg_height 走,坐标换算才不失真。 */
|
|
66
|
+
resize(width, height) {
|
|
67
|
+
for (const canvas of [this.bg, this.mark]) {
|
|
68
|
+
if (canvas) {
|
|
69
|
+
canvas.width = width;
|
|
70
|
+
canvas.height = height;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
clear() {
|
|
75
|
+
for (const canvas of [this.bg, this.mark]) {
|
|
76
|
+
const ctx = canvas && context2d(canvas);
|
|
77
|
+
if (canvas && ctx) {
|
|
78
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
clearMark() {
|
|
83
|
+
const canvas = this.mark;
|
|
84
|
+
const ctx = canvas && context2d(canvas);
|
|
85
|
+
if (canvas && ctx) {
|
|
86
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
setVisible(visible) {
|
|
90
|
+
for (const canvas of [this.bg, this.mark]) {
|
|
91
|
+
if (canvas) {
|
|
92
|
+
canvas.style.display = visible ? "block" : "none";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** 必须在画布可见后测量:display:none 时 getBoundingClientRect() 恒为 0。 */
|
|
97
|
+
measure() {
|
|
98
|
+
const canvas = this.bg;
|
|
99
|
+
if (!canvas || canvas.width <= 0) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const wrap = this.dom.query(SEL.canvasWrap);
|
|
103
|
+
const width = wrap?.getBoundingClientRect().width || canvas.getBoundingClientRect().width || canvas.width;
|
|
104
|
+
this.state.scale = width / canvas.width;
|
|
105
|
+
this.state.invScale = canvas.width / width;
|
|
106
|
+
}
|
|
107
|
+
/** 滑动图画布里只画合成图第 0 段(带缺口背景),点击图整张即背景。 */
|
|
108
|
+
drawBackground(image) {
|
|
109
|
+
const canvas = this.bg;
|
|
110
|
+
const ctx = canvas && context2d(canvas);
|
|
111
|
+
if (!canvas || !ctx) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const { imgWidth, imgHeight } = this.state;
|
|
115
|
+
ctx.drawImage(image, 0, 0, imgWidth, imgHeight, 0, 0, imgWidth, imgHeight);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function loadImage(src) {
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
const image = new Image();
|
|
122
|
+
image.onload = () => resolve(image);
|
|
123
|
+
image.onerror = () => reject(new Error("\u9A8C\u8BC1\u7801\u56FE\u7247\u52A0\u8F7D\u5931\u8D25"));
|
|
124
|
+
image.src = src;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const LUMA_RED = 77;
|
|
129
|
+
const LUMA_GREEN = 150;
|
|
130
|
+
const LUMA_BLUE = 29;
|
|
131
|
+
const WINDOW_RADIUS = 1;
|
|
132
|
+
const REFERENCE_SAMPLES = 5;
|
|
133
|
+
const ALPHA_BASE = 5;
|
|
134
|
+
const ALPHA_RAMP = 15;
|
|
135
|
+
class SlideLayer {
|
|
136
|
+
constructor(canvas, state) {
|
|
137
|
+
this.canvas = canvas;
|
|
138
|
+
this.state = state;
|
|
139
|
+
this.image = null;
|
|
140
|
+
this.prepared = null;
|
|
141
|
+
}
|
|
142
|
+
reset() {
|
|
143
|
+
this.image = null;
|
|
144
|
+
this.prepared = null;
|
|
145
|
+
}
|
|
146
|
+
setImage(image) {
|
|
147
|
+
this.image = image;
|
|
148
|
+
this.prepared = null;
|
|
149
|
+
if (this.state.type === "slide") {
|
|
150
|
+
this.prepare(image);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
draw() {
|
|
154
|
+
if (this.state.type !== "slide") {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const canvas = this.canvas.mark;
|
|
158
|
+
const ctx = canvas && context2d(canvas, true);
|
|
159
|
+
if (!canvas || !ctx) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
163
|
+
const drawOffset = this.state.markOffset * this.state.invScale;
|
|
164
|
+
const drawWidth = Math.min(this.state.markWidth, canvas.width - drawOffset);
|
|
165
|
+
if (drawWidth <= 0) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const height = this.state.imgHeight;
|
|
169
|
+
if (this.prepared) {
|
|
170
|
+
ctx.drawImage(this.prepared, 0, 0, drawWidth, height, drawOffset, 0, drawWidth, height);
|
|
171
|
+
} else if (this.image) {
|
|
172
|
+
ctx.drawImage(this.image, 0, height, drawWidth, height, drawOffset, 0, drawWidth, height);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
prepare(image) {
|
|
176
|
+
const width = this.state.markWidth;
|
|
177
|
+
const height = this.state.imgHeight;
|
|
178
|
+
const layer = document.createElement("canvas");
|
|
179
|
+
layer.width = width;
|
|
180
|
+
layer.height = height;
|
|
181
|
+
const ctx = context2d(layer, true);
|
|
182
|
+
if (!ctx) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
ctx.drawImage(image, 0, height, width, height, 0, 0, width, height);
|
|
186
|
+
try {
|
|
187
|
+
const pixels = ctx.getImageData(0, 0, width, height);
|
|
188
|
+
cutOutBackdrop(pixels.data, width, height);
|
|
189
|
+
ctx.putImageData(pixels, 0, 0);
|
|
190
|
+
} catch (error) {
|
|
191
|
+
console.warn("xq-slide-captcha: \u6ED1\u5757\u56FE\u5C42\u62A0\u56FE\u5931\u8D25\uFF0C\u56DE\u9000\u5230\u539F\u56FE", error);
|
|
192
|
+
}
|
|
193
|
+
this.prepared = layer;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function cutOutBackdrop(data, width, height) {
|
|
197
|
+
for (let y = 0; y < height; y++) {
|
|
198
|
+
const row = windowAverages(data, width, y);
|
|
199
|
+
const reference = estimateBackdrop(row);
|
|
200
|
+
for (let x = 0; x < width; x++) {
|
|
201
|
+
const delta = Math.max(0, row[x] - reference);
|
|
202
|
+
const alpha = Math.max(0, Math.min(255, Math.round((delta - ALPHA_BASE) / ALPHA_RAMP * 255)));
|
|
203
|
+
const i = (y * width + x) * 4;
|
|
204
|
+
if (alpha < data[i + 3]) {
|
|
205
|
+
data[i + 3] = alpha;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function windowAverages(data, width, y) {
|
|
211
|
+
const result = new Float32Array(width);
|
|
212
|
+
for (let x = 0; x < width; x++) {
|
|
213
|
+
let total = 0;
|
|
214
|
+
let count = 0;
|
|
215
|
+
for (let dx = -WINDOW_RADIUS; dx <= WINDOW_RADIUS; dx++) {
|
|
216
|
+
const sx = x + dx;
|
|
217
|
+
if (sx < 0 || sx >= width) {
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const i = (y * width + sx) * 4;
|
|
221
|
+
total += (data[i] * LUMA_RED + data[i + 1] * LUMA_GREEN + data[i + 2] * LUMA_BLUE) / 256;
|
|
222
|
+
count++;
|
|
223
|
+
}
|
|
224
|
+
result[x] = count > 0 ? total / count : 0;
|
|
225
|
+
}
|
|
226
|
+
return result;
|
|
227
|
+
}
|
|
228
|
+
function estimateBackdrop(row) {
|
|
229
|
+
let lowest = Number.POSITIVE_INFINITY;
|
|
230
|
+
const last = row.length - 1;
|
|
231
|
+
for (let s = 0; s < REFERENCE_SAMPLES; s++) {
|
|
232
|
+
const x = Math.min(last, Math.round(s * last / (REFERENCE_SAMPLES - 1)));
|
|
233
|
+
lowest = Math.min(lowest, row[x] ?? 0);
|
|
234
|
+
}
|
|
235
|
+
return Number.isFinite(lowest) ? lowest : 0;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
class DragHandler {
|
|
239
|
+
constructor(target, enabled, callbacks) {
|
|
240
|
+
this.target = target;
|
|
241
|
+
this.enabled = enabled;
|
|
242
|
+
this.callbacks = callbacks;
|
|
243
|
+
this.dragging = false;
|
|
244
|
+
this.onDown = (event) => {
|
|
245
|
+
if (this.dragging || !this.enabled()) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (event.cancelable) {
|
|
249
|
+
event.preventDefault();
|
|
250
|
+
}
|
|
251
|
+
this.dragging = true;
|
|
252
|
+
this.bindDocument();
|
|
253
|
+
this.callbacks.onStart(event.clientX, event.clientY);
|
|
254
|
+
};
|
|
255
|
+
this.onMove = (event) => {
|
|
256
|
+
if (!this.dragging) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (event.cancelable) {
|
|
260
|
+
event.preventDefault();
|
|
261
|
+
}
|
|
262
|
+
this.callbacks.onMove(event.clientX, event.clientY);
|
|
263
|
+
};
|
|
264
|
+
this.onUp = (event) => {
|
|
265
|
+
if (!this.dragging) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (event.cancelable) {
|
|
269
|
+
event.preventDefault();
|
|
270
|
+
}
|
|
271
|
+
this.stop();
|
|
272
|
+
this.callbacks.onEnd();
|
|
273
|
+
};
|
|
274
|
+
this.onCancel = () => {
|
|
275
|
+
if (!this.dragging) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
this.stop();
|
|
279
|
+
this.callbacks.onCancel();
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
attach() {
|
|
283
|
+
this.target.addEventListener("pointerdown", this.onDown);
|
|
284
|
+
}
|
|
285
|
+
detach() {
|
|
286
|
+
this.target.removeEventListener("pointerdown", this.onDown);
|
|
287
|
+
this.stop();
|
|
288
|
+
}
|
|
289
|
+
/** 中止进行中的拖动,不触发任何回调(弹窗隐藏 / 重置时用)。 */
|
|
290
|
+
cancel() {
|
|
291
|
+
this.stop();
|
|
292
|
+
}
|
|
293
|
+
stop() {
|
|
294
|
+
this.dragging = false;
|
|
295
|
+
this.unbindDocument();
|
|
296
|
+
}
|
|
297
|
+
bindDocument() {
|
|
298
|
+
document.addEventListener("pointermove", this.onMove, { passive: false });
|
|
299
|
+
document.addEventListener("pointerup", this.onUp, { passive: false });
|
|
300
|
+
document.addEventListener("pointercancel", this.onCancel);
|
|
301
|
+
}
|
|
302
|
+
unbindDocument() {
|
|
303
|
+
document.removeEventListener("pointermove", this.onMove);
|
|
304
|
+
document.removeEventListener("pointerup", this.onUp);
|
|
305
|
+
document.removeEventListener("pointercancel", this.onCancel);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const STEP = 4;
|
|
310
|
+
const STEP_SHIFT = 12;
|
|
311
|
+
const DIRECTIONS = {
|
|
312
|
+
ArrowLeft: -1,
|
|
313
|
+
Left: -1,
|
|
314
|
+
ArrowRight: 1,
|
|
315
|
+
Right: 1
|
|
316
|
+
};
|
|
317
|
+
const JUMP_KEYS = {
|
|
318
|
+
Home: "start",
|
|
319
|
+
End: "end"
|
|
320
|
+
};
|
|
321
|
+
class KeyboardHandler {
|
|
322
|
+
constructor(target, state, callbacks) {
|
|
323
|
+
this.target = target;
|
|
324
|
+
this.state = state;
|
|
325
|
+
this.callbacks = callbacks;
|
|
326
|
+
this.keyUp = null;
|
|
327
|
+
this.onKeyDown = (event) => {
|
|
328
|
+
if (!this.enabled()) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const step = event.shiftKey ? STEP_SHIFT : STEP;
|
|
332
|
+
const direction = DIRECTIONS[event.key];
|
|
333
|
+
let next = null;
|
|
334
|
+
if (direction !== void 0) {
|
|
335
|
+
next = this.state.markOffset + direction * step;
|
|
336
|
+
} else if (Object.prototype.hasOwnProperty.call(JUMP_KEYS, event.key)) {
|
|
337
|
+
next = JUMP_KEYS[event.key] === "start" ? 0 : this.state.maxOffset;
|
|
338
|
+
}
|
|
339
|
+
if (next === null) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
event.preventDefault();
|
|
343
|
+
this.callbacks.onOffset(Math.max(0, Math.min(next, this.state.maxOffset)));
|
|
344
|
+
this.listenKeyUp();
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
attach() {
|
|
348
|
+
this.target.addEventListener("keydown", this.onKeyDown);
|
|
349
|
+
}
|
|
350
|
+
detach() {
|
|
351
|
+
this.target.removeEventListener("keydown", this.onKeyDown);
|
|
352
|
+
this.clearKeyUp();
|
|
353
|
+
}
|
|
354
|
+
/** 取消尚未触发的 keyup 提交,保留 keydown 绑定。 */
|
|
355
|
+
cancelCommit() {
|
|
356
|
+
this.clearKeyUp();
|
|
357
|
+
}
|
|
358
|
+
/** 自移除监听,避免隐藏/重置后残留全局 keyup 导致重复提交。 */
|
|
359
|
+
listenKeyUp() {
|
|
360
|
+
if (this.keyUp) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const handler = () => {
|
|
364
|
+
this.clearKeyUp();
|
|
365
|
+
if (this.enabled()) {
|
|
366
|
+
this.callbacks.onCommit();
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
this.keyUp = handler;
|
|
370
|
+
document.addEventListener("keyup", handler);
|
|
371
|
+
}
|
|
372
|
+
clearKeyUp() {
|
|
373
|
+
if (this.keyUp) {
|
|
374
|
+
document.removeEventListener("keyup", this.keyUp);
|
|
375
|
+
this.keyUp = null;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
enabled() {
|
|
379
|
+
return this.state.type === "slide" && this.state.imgLoaded && !this.state.busy;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
class TrackRecorder {
|
|
384
|
+
constructor() {
|
|
385
|
+
this.points = [];
|
|
386
|
+
this.start = 0;
|
|
387
|
+
}
|
|
388
|
+
get samples() {
|
|
389
|
+
return this.points;
|
|
390
|
+
}
|
|
391
|
+
get started() {
|
|
392
|
+
return this.start > 0;
|
|
393
|
+
}
|
|
394
|
+
begin() {
|
|
395
|
+
this.points = [];
|
|
396
|
+
this.start = Date.now();
|
|
397
|
+
}
|
|
398
|
+
record(x, y) {
|
|
399
|
+
this.points.push({
|
|
400
|
+
x: Math.round(x),
|
|
401
|
+
y: Math.round(y),
|
|
402
|
+
t: Date.now() - this.start
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
reset() {
|
|
406
|
+
this.points = [];
|
|
407
|
+
this.start = 0;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
class Slider {
|
|
412
|
+
constructor(dom, state, layer, onCommit) {
|
|
413
|
+
this.dom = dom;
|
|
414
|
+
this.state = state;
|
|
415
|
+
this.layer = layer;
|
|
416
|
+
this.onCommit = onCommit;
|
|
417
|
+
this.recorder = new TrackRecorder();
|
|
418
|
+
this.drag = null;
|
|
419
|
+
this.keyboard = null;
|
|
420
|
+
this.startClientX = 0;
|
|
421
|
+
this.startClientY = 0;
|
|
422
|
+
}
|
|
423
|
+
get track() {
|
|
424
|
+
return this.recorder.samples;
|
|
425
|
+
}
|
|
426
|
+
attach() {
|
|
427
|
+
const block = this.block;
|
|
428
|
+
if (!block) {
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
this.detach();
|
|
432
|
+
this.drag = new DragHandler(block, () => this.enabled(), {
|
|
433
|
+
onStart: (x, y) => this.start(x, y),
|
|
434
|
+
onMove: (x, y) => this.move(x, y),
|
|
435
|
+
onEnd: () => this.submit(),
|
|
436
|
+
onCancel: () => this.abort()
|
|
437
|
+
});
|
|
438
|
+
this.keyboard = new KeyboardHandler(block, this.state, {
|
|
439
|
+
onOffset: (offset) => this.keyMove(offset),
|
|
440
|
+
onCommit: () => this.submit()
|
|
441
|
+
});
|
|
442
|
+
this.drag.attach();
|
|
443
|
+
this.keyboard.attach();
|
|
444
|
+
}
|
|
445
|
+
detach() {
|
|
446
|
+
this.drag?.detach();
|
|
447
|
+
this.keyboard?.detach();
|
|
448
|
+
this.drag = null;
|
|
449
|
+
this.keyboard = null;
|
|
450
|
+
}
|
|
451
|
+
/** 新一轮验证码:滑块归零、轨迹清空。 */
|
|
452
|
+
reset() {
|
|
453
|
+
this.recorder.reset();
|
|
454
|
+
this.state.markOffset = 0;
|
|
455
|
+
this.paint();
|
|
456
|
+
}
|
|
457
|
+
/** 弹窗隐藏 / 重置:中止进行中的拖动与待触发的键盘提交,保留事件绑定。 */
|
|
458
|
+
suspend() {
|
|
459
|
+
this.drag?.cancel();
|
|
460
|
+
this.keyboard?.cancelCommit();
|
|
461
|
+
this.state.cancelFrame();
|
|
462
|
+
this.state.moving = false;
|
|
463
|
+
this.block?.classList.remove(CLS.dragging);
|
|
464
|
+
}
|
|
465
|
+
start(clientX, clientY) {
|
|
466
|
+
this.startClientX = clientX;
|
|
467
|
+
this.startClientY = clientY;
|
|
468
|
+
this.state.busy = true;
|
|
469
|
+
this.state.moving = true;
|
|
470
|
+
this.recorder.begin();
|
|
471
|
+
this.block?.classList.add(CLS.dragging);
|
|
472
|
+
}
|
|
473
|
+
move(clientX, clientY) {
|
|
474
|
+
const offset = Math.max(0, Math.min(clientX - this.startClientX, this.state.maxOffset));
|
|
475
|
+
const inverse = this.state.scale > 0 ? 1 / this.state.scale : 1;
|
|
476
|
+
this.recorder.record(
|
|
477
|
+
offset * inverse,
|
|
478
|
+
this.state.markTopY + (clientY - this.startClientY) * inverse
|
|
479
|
+
);
|
|
480
|
+
if (offset === this.state.markOffset) {
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
this.state.markOffset = offset;
|
|
484
|
+
this.state.cancelFrame();
|
|
485
|
+
this.state.rafId = requestAnimationFrame(() => {
|
|
486
|
+
this.paint();
|
|
487
|
+
this.state.rafId = null;
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
keyMove(offset) {
|
|
491
|
+
if (!this.recorder.started) {
|
|
492
|
+
this.recorder.begin();
|
|
493
|
+
}
|
|
494
|
+
this.recorder.record(offset * this.state.invScale, this.state.markTopY);
|
|
495
|
+
this.state.markOffset = offset;
|
|
496
|
+
this.paint();
|
|
497
|
+
}
|
|
498
|
+
submit() {
|
|
499
|
+
this.state.cancelFrame();
|
|
500
|
+
this.state.moving = false;
|
|
501
|
+
this.state.busy = true;
|
|
502
|
+
this.block?.classList.remove(CLS.dragging);
|
|
503
|
+
this.paint();
|
|
504
|
+
this.onCommit(Math.round(this.state.markOffset * this.state.invScale), this.recorder.samples);
|
|
505
|
+
}
|
|
506
|
+
/** 手势被系统打断:不提交,回到可再次拖动的状态。 */
|
|
507
|
+
abort() {
|
|
508
|
+
this.state.cancelFrame();
|
|
509
|
+
this.state.moving = false;
|
|
510
|
+
this.state.busy = false;
|
|
511
|
+
this.block?.classList.remove(CLS.dragging);
|
|
512
|
+
this.reset();
|
|
513
|
+
}
|
|
514
|
+
paint() {
|
|
515
|
+
const block = this.block;
|
|
516
|
+
const offset = this.state.markOffset;
|
|
517
|
+
const max = this.state.maxOffset;
|
|
518
|
+
const percent = max > 0 ? Math.round(offset / max * 100) : 0;
|
|
519
|
+
if (block) {
|
|
520
|
+
block.style.transform = `translateX(${offset}px)`;
|
|
521
|
+
block.setAttribute("aria-valuenow", String(percent));
|
|
522
|
+
}
|
|
523
|
+
const bar = this.dom.query(SEL.progressBar);
|
|
524
|
+
if (bar) {
|
|
525
|
+
bar.style.width = `${percent}%`;
|
|
526
|
+
bar.setAttribute("aria-valuenow", String(percent));
|
|
527
|
+
}
|
|
528
|
+
this.layer.draw();
|
|
529
|
+
}
|
|
530
|
+
enabled() {
|
|
531
|
+
return this.state.type === "slide" && this.state.imgLoaded && !this.state.busy;
|
|
532
|
+
}
|
|
533
|
+
get block() {
|
|
534
|
+
return this.dom.query(SEL.slideBlock);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
class Hint {
|
|
539
|
+
constructor(dom, options) {
|
|
540
|
+
this.dom = dom;
|
|
541
|
+
this.options = options;
|
|
542
|
+
}
|
|
543
|
+
forSlide() {
|
|
544
|
+
const node = this.dom.query(SEL.slideText);
|
|
545
|
+
if (!node) {
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
node.textContent = this.options.slideText;
|
|
549
|
+
node.removeAttribute("style");
|
|
550
|
+
node.classList.remove("d-none");
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
class Loading {
|
|
555
|
+
constructor(dom) {
|
|
556
|
+
this.dom = dom;
|
|
557
|
+
}
|
|
558
|
+
show() {
|
|
559
|
+
const node = this.element;
|
|
560
|
+
if (!node) {
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
const spinner = document.createElement("div");
|
|
564
|
+
spinner.className = "spinner-border text-primary";
|
|
565
|
+
spinner.setAttribute("role", "status");
|
|
566
|
+
const label = document.createElement("span");
|
|
567
|
+
label.className = "visually-hidden";
|
|
568
|
+
label.textContent = "\u52A0\u8F7D\u4E2D";
|
|
569
|
+
spinner.append(label);
|
|
570
|
+
const caption = document.createElement("span");
|
|
571
|
+
caption.textContent = "\u52A0\u8F7D\u4E2D...";
|
|
572
|
+
node.replaceChildren(spinner, caption);
|
|
573
|
+
node.classList.remove(CLS.loadingError);
|
|
574
|
+
node.classList.remove(CLS.hidden);
|
|
575
|
+
}
|
|
576
|
+
hide() {
|
|
577
|
+
const node = this.element;
|
|
578
|
+
if (!node) {
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
node.replaceChildren();
|
|
582
|
+
node.classList.remove(CLS.loadingError);
|
|
583
|
+
node.classList.add(CLS.hidden);
|
|
584
|
+
}
|
|
585
|
+
fail() {
|
|
586
|
+
const node = this.element;
|
|
587
|
+
if (!node) {
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
const text = document.createElement("div");
|
|
591
|
+
text.className = "text-danger small fw-semibold text-center";
|
|
592
|
+
text.append("\u52A0\u8F7D\u5931\u8D25", document.createElement("br"), "\u8BF7\u5237\u65B0\u91CD\u8BD5");
|
|
593
|
+
node.replaceChildren(text);
|
|
594
|
+
node.classList.add(CLS.loadingError);
|
|
595
|
+
node.classList.remove(CLS.hidden);
|
|
596
|
+
}
|
|
597
|
+
get element() {
|
|
598
|
+
return this.dom.query(SEL.loading);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
function el(tag, className = "", attrs = {}) {
|
|
603
|
+
const node = document.createElement(tag);
|
|
604
|
+
if (className) {
|
|
605
|
+
node.className = className;
|
|
606
|
+
}
|
|
607
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
608
|
+
node.setAttribute(name, value);
|
|
609
|
+
}
|
|
610
|
+
return node;
|
|
611
|
+
}
|
|
612
|
+
function buildModal(options) {
|
|
613
|
+
const existing = document.querySelector(SEL.modal);
|
|
614
|
+
if (existing) {
|
|
615
|
+
return existing;
|
|
616
|
+
}
|
|
617
|
+
const modal = el("div", "modal fade xq-captcha-modal", {
|
|
618
|
+
id: "xq-slide-captcha-modal",
|
|
619
|
+
"data-xq-captcha-kind": "slide",
|
|
620
|
+
role: "dialog",
|
|
621
|
+
"aria-modal": "true",
|
|
622
|
+
"aria-label": "\u4EBA\u673A\u9A8C\u8BC1",
|
|
623
|
+
"aria-hidden": "true",
|
|
624
|
+
tabindex: "-1"
|
|
625
|
+
});
|
|
626
|
+
const dialog = el("div", "modal-dialog modal-dialog-centered xq-captcha-dialog");
|
|
627
|
+
const content = el("div", "modal-content xq-captcha-content");
|
|
628
|
+
const header = el("div", "modal-header");
|
|
629
|
+
const title = el("h5", "modal-title h6");
|
|
630
|
+
title.textContent = "\u5B89\u5168\u9A8C\u8BC1";
|
|
631
|
+
header.append(title);
|
|
632
|
+
if (options.showClose) {
|
|
633
|
+
const close = el("button", "btn-close xq-captcha-close", {
|
|
634
|
+
type: "button",
|
|
635
|
+
"aria-label": "\u5173\u95ED"
|
|
636
|
+
});
|
|
637
|
+
header.append(close);
|
|
638
|
+
}
|
|
639
|
+
content.append(header);
|
|
640
|
+
const body = el("div", "modal-body");
|
|
641
|
+
const wrap = el("div", `${SEL.canvasWrap.slice(1)} position-relative w-100 overflow-hidden rounded border`);
|
|
642
|
+
const loading = el(
|
|
643
|
+
"div",
|
|
644
|
+
`${SEL.loading.slice(1)} position-absolute top-0 start-0 w-100 h-100 z-3 d-flex flex-column align-items-center justify-content-center gap-2`
|
|
645
|
+
);
|
|
646
|
+
wrap.append(loading);
|
|
647
|
+
wrap.append(
|
|
648
|
+
el("canvas", `${SEL.bgCanvas.slice(1)} position-relative z-1 d-block w-100 h-auto border-0`, {
|
|
649
|
+
width: "240",
|
|
650
|
+
height: "150",
|
|
651
|
+
"aria-label": "\u9A8C\u8BC1\u7801\u56FE\u7247"
|
|
652
|
+
})
|
|
653
|
+
);
|
|
654
|
+
wrap.append(
|
|
655
|
+
el("canvas", `${SEL.markCanvas.slice(1)} position-absolute top-0 start-0 w-100 h-100 z-2 pe-none`, {
|
|
656
|
+
width: "240",
|
|
657
|
+
height: "150"
|
|
658
|
+
})
|
|
659
|
+
);
|
|
660
|
+
body.append(wrap);
|
|
661
|
+
const error = el("div", "alert alert-danger fade xq-captcha-alert-error", { role: "alert" });
|
|
662
|
+
const success = el("div", "alert alert-success fade xq-captcha-alert-success", {
|
|
663
|
+
role: "status"
|
|
664
|
+
});
|
|
665
|
+
error.style.display = "none";
|
|
666
|
+
success.style.display = "none";
|
|
667
|
+
body.append(error, success);
|
|
668
|
+
const slide = el("div", `${SEL.slideArea.slice(1)} position-relative w-100 overflow-hidden rounded-pill border`);
|
|
669
|
+
const block = el(
|
|
670
|
+
"button",
|
|
671
|
+
`btn btn-primary rounded-circle d-flex align-items-center justify-content-center p-0 user-select-none ${SEL.slideBlock.slice(1)}`,
|
|
672
|
+
{
|
|
673
|
+
type: "button",
|
|
674
|
+
tabindex: "0",
|
|
675
|
+
role: "slider",
|
|
676
|
+
"aria-label": "\u62D6\u52A8\u6ED1\u5757\u5B8C\u6210\u62FC\u56FE",
|
|
677
|
+
"aria-valuemin": "0",
|
|
678
|
+
"aria-valuemax": "100",
|
|
679
|
+
"aria-valuenow": "0"
|
|
680
|
+
}
|
|
681
|
+
);
|
|
682
|
+
const hint = el("span", `${SEL.slideText.slice(1)} d-flex align-items-center justify-content-center pe-none user-select-none`);
|
|
683
|
+
hint.textContent = options.slideText;
|
|
684
|
+
const progress = el("div", "progress xq-captcha-progress");
|
|
685
|
+
const bar = el("div", `progress-bar ${SEL.progressBar.slice(1)}`, {
|
|
686
|
+
role: "progressbar",
|
|
687
|
+
"aria-valuemin": "0",
|
|
688
|
+
"aria-valuemax": "100",
|
|
689
|
+
"aria-valuenow": "0"
|
|
690
|
+
});
|
|
691
|
+
progress.append(bar);
|
|
692
|
+
slide.append(block, hint, progress);
|
|
693
|
+
body.append(slide);
|
|
694
|
+
content.append(body);
|
|
695
|
+
const footer = el("div", "modal-footer");
|
|
696
|
+
const copyright = el("a", `${SEL.copyright.slice(1)} link-secondary small`, {
|
|
697
|
+
href: options.copyright.url,
|
|
698
|
+
target: "_blank",
|
|
699
|
+
rel: "noopener noreferrer",
|
|
700
|
+
title: options.copyright.title
|
|
701
|
+
});
|
|
702
|
+
copyright.textContent = options.copyright.text;
|
|
703
|
+
footer.append(copyright);
|
|
704
|
+
if (options.showRefresh) {
|
|
705
|
+
const refresh = el("button", `btn btn-outline-secondary btn-sm ${SEL.refresh.slice(1)}`, {
|
|
706
|
+
type: "button",
|
|
707
|
+
"aria-label": "\u5237\u65B0\u9A8C\u8BC1\u7801"
|
|
708
|
+
});
|
|
709
|
+
refresh.textContent = "\u5237\u65B0";
|
|
710
|
+
footer.append(refresh);
|
|
711
|
+
}
|
|
712
|
+
content.append(footer);
|
|
713
|
+
dialog.append(content);
|
|
714
|
+
modal.append(dialog);
|
|
715
|
+
document.body.append(modal);
|
|
716
|
+
return modal;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const HOOKS = {
|
|
720
|
+
logPrefix: "xq-slide-captcha",
|
|
721
|
+
type: "slide",
|
|
722
|
+
defaultOptions: resolveOptions({}),
|
|
723
|
+
modalSelector: SEL.modal,
|
|
724
|
+
contentSelector: SEL.content,
|
|
725
|
+
shakeClass: "xq-captcha-shake",
|
|
726
|
+
messageSelectors: { msgOk: SEL.msgOk, msgError: SEL.msgError },
|
|
727
|
+
triggerClasses: { ripple: "xq-captcha-ripple", verified: "xq-captcha-verified" },
|
|
728
|
+
triggerSelector: SEL.trigger
|
|
729
|
+
};
|
|
730
|
+
class SlideBehaviour extends xqCaptchaCore.CaptchaBehaviour {
|
|
731
|
+
onContextReady() {
|
|
732
|
+
this.canvas = new CanvasPair(this.ctx.dom, this.ctx.state);
|
|
733
|
+
this.layer = new SlideLayer(this.canvas, this.ctx.state);
|
|
734
|
+
this.hint = new Hint(this.ctx.dom, this.ctx.options);
|
|
735
|
+
this.loading = new Loading(this.ctx.dom);
|
|
736
|
+
this.slider = new Slider(this.ctx.dom, this.ctx.state, this.layer, (offset, track) => {
|
|
737
|
+
this.ctx.submit({
|
|
738
|
+
captcha_r: String(offset),
|
|
739
|
+
slide_track: JSON.stringify(track)
|
|
740
|
+
});
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
buildModal(options) {
|
|
744
|
+
return buildModal(options);
|
|
745
|
+
}
|
|
746
|
+
attachInteraction() {
|
|
747
|
+
this.slider.attach();
|
|
748
|
+
}
|
|
749
|
+
detachInteraction() {
|
|
750
|
+
this.slider.detach();
|
|
751
|
+
}
|
|
752
|
+
resetForShow() {
|
|
753
|
+
this.slider.suspend();
|
|
754
|
+
this.slider.reset();
|
|
755
|
+
}
|
|
756
|
+
suspendInteraction() {
|
|
757
|
+
this.slider.suspend();
|
|
758
|
+
}
|
|
759
|
+
prepareRefresh() {
|
|
760
|
+
this.slider.reset();
|
|
761
|
+
this.layer.reset();
|
|
762
|
+
this.canvas.setVisible(false);
|
|
763
|
+
this.canvas.clear();
|
|
764
|
+
this.loading.show();
|
|
765
|
+
}
|
|
766
|
+
async applyData(data) {
|
|
767
|
+
this.ctx.state.imgWidth = data.bg_width || 240;
|
|
768
|
+
this.ctx.state.imgHeight = data.bg_height || 150;
|
|
769
|
+
this.ctx.state.markWidth = data.mark_width || 50;
|
|
770
|
+
this.ctx.state.markTopY = typeof data.pos_y === "number" ? data.pos_y : 0;
|
|
771
|
+
this.hint.forSlide();
|
|
772
|
+
this.canvas.resize(this.ctx.state.imgWidth, this.ctx.state.imgHeight);
|
|
773
|
+
this.ctx.emitRefresh();
|
|
774
|
+
try {
|
|
775
|
+
const image = await loadImage(data.image_base64);
|
|
776
|
+
this.canvas.setVisible(true);
|
|
777
|
+
this.canvas.measure();
|
|
778
|
+
this.ctx.state.maxOffset = Math.max(
|
|
779
|
+
0,
|
|
780
|
+
Math.round((this.ctx.state.imgWidth - this.ctx.state.markWidth) * this.ctx.state.scale)
|
|
781
|
+
);
|
|
782
|
+
this.canvas.drawBackground(image);
|
|
783
|
+
this.canvas.clearMark();
|
|
784
|
+
this.layer.setImage(image);
|
|
785
|
+
this.ctx.state.imgLoaded = true;
|
|
786
|
+
this.loading.hide();
|
|
787
|
+
this.slider.reset();
|
|
788
|
+
} catch (error) {
|
|
789
|
+
console.error("xq-slide-captcha: \u9A8C\u8BC1\u7801\u56FE\u7247\u52A0\u8F7D\u5931\u8D25", error);
|
|
790
|
+
this.loading.fail();
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
markFailures() {
|
|
794
|
+
}
|
|
795
|
+
beforeFailRefresh() {
|
|
796
|
+
}
|
|
797
|
+
onModalShown() {
|
|
798
|
+
this.canvas.measure();
|
|
799
|
+
this.layer.draw();
|
|
800
|
+
}
|
|
801
|
+
get track() {
|
|
802
|
+
return this.slider.track;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
class SlideCaptcha extends xqCaptchaCore.BaseCaptcha {
|
|
806
|
+
constructor(options = {}) {
|
|
807
|
+
const behaviour = new SlideBehaviour();
|
|
808
|
+
super(behaviour, HOOKS, options);
|
|
809
|
+
this.slideBehaviour = behaviour;
|
|
810
|
+
}
|
|
811
|
+
init(options) {
|
|
812
|
+
return super.init(options);
|
|
813
|
+
}
|
|
814
|
+
getSlideTrack() {
|
|
815
|
+
return this.slideBehaviour.track;
|
|
816
|
+
}
|
|
817
|
+
bindTools() {
|
|
818
|
+
this.bindButton(SEL.close, () => {
|
|
819
|
+
this.callbacks.emit("close");
|
|
820
|
+
this.hide();
|
|
821
|
+
}, BOUND_ATTR);
|
|
822
|
+
this.bindButton(SEL.refresh, () => this.refresh(), BOUND_ATTR);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
const slideCaptcha = new SlideCaptcha();
|
|
827
|
+
function installGlobal(instance = slideCaptcha) {
|
|
828
|
+
window.xqSlideCaptcha = instance;
|
|
829
|
+
return instance;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
if (typeof window !== "undefined") {
|
|
833
|
+
window.xqSlideCaptcha = slideCaptcha;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
exports.FetchTransport = xqCaptchaCore.FetchTransport;
|
|
837
|
+
exports.HttpError = xqCaptchaCore.HttpError;
|
|
838
|
+
exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
|
|
839
|
+
exports.SlideCaptcha = SlideCaptcha;
|
|
840
|
+
exports.default = SlideCaptcha;
|
|
841
|
+
exports.installGlobal = installGlobal;
|
|
842
|
+
exports.resolveOptions = resolveOptions;
|
|
843
|
+
exports.slideCaptcha = slideCaptcha;
|