|
| 1 | +/** |
| 2 | + * Captcha Helper Feature |
| 3 | + * Hỗ trợ nhập captcha: normalize input, auto-submit on blur/Enter |
| 4 | + */ |
| 5 | + |
| 6 | +import { Feature } from '../../core'; |
| 7 | +import { normalizeCaptchaInput } from '../../utils'; |
| 8 | + |
| 9 | +// ============================================ |
| 10 | +// Types & Interfaces (extensibility) |
| 11 | +// ============================================ |
| 12 | + |
| 13 | +/** |
| 14 | + * Interface cho captcha handler - dễ mở rộng cho trang khác |
| 15 | + */ |
| 16 | +interface CaptchaPageHandler { |
| 17 | + /** URL pattern để match */ |
| 18 | + urlPattern: RegExp; |
| 19 | + /** Selector cho input field */ |
| 20 | + inputSelector: string; |
| 21 | + /** Selector cho submit button */ |
| 22 | + submitSelector: string; |
| 23 | + /** Optional: selector cho captcha image (for future auto-solve) */ |
| 24 | + imageSelector?: string; |
| 25 | +} |
| 26 | + |
| 27 | +// ============================================ |
| 28 | +// Page Handlers |
| 29 | +// ============================================ |
| 30 | + |
| 31 | +const CAPTCHA_HANDLERS: CaptchaPageHandler[] = [ |
| 32 | + // SSO Login page |
| 33 | + { |
| 34 | + urlPattern: /\/sso\?token=/, |
| 35 | + inputSelector: '#ctl00_txtimgcode', |
| 36 | + submitSelector: '#ctl00_butLogin', |
| 37 | + imageSelector: '#ctl00_Image1', |
| 38 | + }, |
| 39 | + // Register page (đăng ký học phần) |
| 40 | + { |
| 41 | + urlPattern: /\/register\//, |
| 42 | + inputSelector: '#ctl02_txtimgcode', |
| 43 | + submitSelector: '#ctl02_btnSubmit', |
| 44 | + imageSelector: '#ctl02_Image1', |
| 45 | + }, |
| 46 | +]; |
| 47 | + |
| 48 | +// ============================================ |
| 49 | +// CaptchaHelper Feature |
| 50 | +// ============================================ |
| 51 | + |
| 52 | +export class CaptchaHelperFeature extends Feature { |
| 53 | + private inputEl: HTMLInputElement | null = null; |
| 54 | + private submitEl: HTMLElement | null = null; |
| 55 | + private currentHandler: CaptchaPageHandler | null = null; |
| 56 | + |
| 57 | + // Debounce timer |
| 58 | + private normalizeTimer: ReturnType<typeof setTimeout> | null = null; |
| 59 | + private readonly DEBOUNCE_DELAY = 150; // ms |
| 60 | + |
| 61 | + // Event listener references for cleanup |
| 62 | + private handleInput = this.onInput.bind(this); |
| 63 | + private handleKeyDown = this.onKeyDown.bind(this); |
| 64 | + private handleBlur = this.onBlur.bind(this); |
| 65 | + |
| 66 | + constructor() { |
| 67 | + super({ |
| 68 | + id: 'captcha-helper', |
| 69 | + name: 'Captcha Helper', |
| 70 | + description: 'Hỗ trợ nhập captcha: tự động chuyển chữ thường, loại bỏ dấu, submit khi Enter/blur', |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Override shouldRun để chỉ chạy trên các trang có captcha |
| 76 | + */ |
| 77 | + override shouldRun(): boolean { |
| 78 | + if (!super.shouldRun()) return false; |
| 79 | + |
| 80 | + const url = window.location.pathname + window.location.search; |
| 81 | + return CAPTCHA_HANDLERS.some(h => h.urlPattern.test(url)); |
| 82 | + } |
| 83 | + |
| 84 | + init(): void { |
| 85 | + this.log.i('Initializing...'); |
| 86 | + |
| 87 | + // Tìm handler phù hợp với URL hiện tại |
| 88 | + const url = window.location.pathname + window.location.search; |
| 89 | + this.currentHandler = CAPTCHA_HANDLERS.find(h => h.urlPattern.test(url)) || null; |
| 90 | + |
| 91 | + if (!this.currentHandler) { |
| 92 | + this.log.w('No matching captcha handler found'); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // Tìm elements |
| 97 | + this.inputEl = document.querySelector<HTMLInputElement>(this.currentHandler.inputSelector); |
| 98 | + this.submitEl = document.querySelector<HTMLElement>(this.currentHandler.submitSelector); |
| 99 | + |
| 100 | + if (!this.inputEl) { |
| 101 | + this.log.w('Captcha input not found:', this.currentHandler.inputSelector); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (!this.submitEl) { |
| 106 | + this.log.w('Submit button not found:', this.currentHandler.submitSelector); |
| 107 | + } |
| 108 | + |
| 109 | + // Attach event listeners |
| 110 | + this.inputEl.addEventListener('input', this.handleInput); |
| 111 | + this.inputEl.addEventListener('keydown', this.handleKeyDown); |
| 112 | + this.inputEl.addEventListener('blur', this.handleBlur); |
| 113 | + |
| 114 | + // Focus input để người dùng có thể gõ ngay |
| 115 | + this.inputEl.focus(); |
| 116 | + |
| 117 | + this.log.i('Ready! Input:', this.currentHandler.inputSelector); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Xử lý input: debounce normalize |
| 122 | + */ |
| 123 | + private onInput(): void { |
| 124 | + // Clear timer cũ |
| 125 | + if (this.normalizeTimer) { |
| 126 | + clearTimeout(this.normalizeTimer); |
| 127 | + } |
| 128 | + |
| 129 | + // Set timer mới - normalize sau DEBOUNCE_DELAY ms |
| 130 | + this.normalizeTimer = setTimeout(() => { |
| 131 | + this.normalizeInput(); |
| 132 | + }, this.DEBOUNCE_DELAY); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Normalize input value |
| 137 | + */ |
| 138 | + private normalizeInput(): void { |
| 139 | + if (!this.inputEl) return; |
| 140 | + |
| 141 | + // Clear timer nếu có |
| 142 | + if (this.normalizeTimer) { |
| 143 | + clearTimeout(this.normalizeTimer); |
| 144 | + this.normalizeTimer = null; |
| 145 | + } |
| 146 | + |
| 147 | + const original = this.inputEl.value; |
| 148 | + const normalized = normalizeCaptchaInput(original); |
| 149 | + |
| 150 | + if (original !== normalized) { |
| 151 | + this.inputEl.value = normalized; |
| 152 | + // Đặt cursor ở cuối |
| 153 | + this.inputEl.setSelectionRange(normalized.length, normalized.length); |
| 154 | + this.log.d(`Normalized: "${original}" → "${normalized}"`); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Xử lý keydown: submit khi Enter |
| 160 | + */ |
| 161 | + private onKeyDown(e: KeyboardEvent): void { |
| 162 | + if (e.key === 'Enter') { |
| 163 | + e.preventDefault(); |
| 164 | + this.normalizeInput(); |
| 165 | + this.submit(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Xử lý blur: normalize và submit khi out focus |
| 171 | + */ |
| 172 | + private onBlur(): void { |
| 173 | + this.normalizeInput(); |
| 174 | + |
| 175 | + // Chỉ submit nếu có giá trị |
| 176 | + if (this.inputEl?.value.trim()) { |
| 177 | + this.submit(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Submit form |
| 183 | + */ |
| 184 | + private submit(): void { |
| 185 | + const value = this.inputEl?.value.trim() || ''; |
| 186 | + const CAPTCHA_LENGTH = 5; |
| 187 | + |
| 188 | + if (value.length < CAPTCHA_LENGTH) { |
| 189 | + this.log.d(`Need ${CAPTCHA_LENGTH} chars, got ${value.length}`); |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + if (this.submitEl) { |
| 194 | + this.log.i('Submitting...'); |
| 195 | + this.submitEl.click(); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + destroy(): void { |
| 200 | + // Clear timer |
| 201 | + if (this.normalizeTimer) { |
| 202 | + clearTimeout(this.normalizeTimer); |
| 203 | + this.normalizeTimer = null; |
| 204 | + } |
| 205 | + |
| 206 | + // Cleanup event listeners |
| 207 | + if (this.inputEl) { |
| 208 | + this.inputEl.removeEventListener('input', this.handleInput); |
| 209 | + this.inputEl.removeEventListener('keydown', this.handleKeyDown); |
| 210 | + this.inputEl.removeEventListener('blur', this.handleBlur); |
| 211 | + } |
| 212 | + |
| 213 | + this.inputEl = null; |
| 214 | + this.submitEl = null; |
| 215 | + this.currentHandler = null; |
| 216 | + } |
| 217 | +} |
| 218 | + |
0 commit comments