* Simulate a click with proper event dispatching. * @param {Element} el
(el)
| 344 | * @param {Element} el |
| 345 | */ |
| 346 | function simulateClick(el) { |
| 347 | throwIfStopped(); |
| 348 | if (!el) { |
| 349 | throw new Error('无法点击空元素。'); |
| 350 | } |
| 351 | |
| 352 | const form = el.form || el.closest?.('form') || null; |
| 353 | const strategy = typeof getActivationStrategy === 'function' |
| 354 | ? getActivationStrategy({ |
| 355 | tagName: el.tagName, |
| 356 | type: el.getAttribute?.('type') || el.type || '', |
| 357 | hasForm: Boolean(form), |
| 358 | pathname: location.pathname || '', |
| 359 | }) |
| 360 | : { method: 'click' }; |
| 361 | |
| 362 | let method = strategy.method || 'click'; |
| 363 | |
| 364 | if (method === 'requestSubmit' && form && typeof form.requestSubmit === 'function') { |
| 365 | form.requestSubmit(el); |
| 366 | } else if (typeof el.click === 'function') { |
| 367 | method = 'click'; |
| 368 | el.click(); |
| 369 | } else { |
| 370 | method = 'dispatchEvent'; |
| 371 | el.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true })); |
| 372 | } |
| 373 | |
| 374 | console.log(LOG_PREFIX, `已点击(${method}): ${el.tagName} ${el.textContent?.slice(0, 30) || ''}`); |
| 375 | log(`已点击(${method}) [${el.tagName}] "${el.textContent?.trim().slice(0, 30) || ''}"`); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Wait a specified number of milliseconds. |
no test coverage detected