JavaScript异步请求及验证码处理:实战案例分析

本文将带您解析一段用于异步请求数据、处理验证码并与用户交互的JavaScript代码。javascriptconst targetUrl = '' // 将目标 URL 填写到 targetUrl 变量中

let url = '' // 根据实际情况填写 url 变量let gethtml = await fetch(url, { headers: { 'Content-type': 'application/x-www-form-urlencoded', }, body: inturl=https%3A%2F%2Fapi.charlotte.ac.cn/gt_manual?gt=${RES.data.gt}%26challenge=${RES.data.challenge}&outurl=&operat=inturl, method: 'post'})let text = await gethtml.text()let match = text.match(//g)let shorturl = match.toString().replace(//g, '')let tohttps = shorturl.replace(/http/g, 'https')await this.e.reply(查询出现验证码,请尽快完成验证: ${tohttps}, false, { at: true })

for (let n = 1; n < 120; n++) { await common.sleep(1000) try { const checkurl = https://api.charlotte.ac.cn/gt_callback?challenge=${RES.data.challenge} const check = await fetch(checkurl, { method: 'GET' }) let res = await check.json() if (res.data) { if (res?.retcode !== 204) { logger.mark([接口][uid:${this.uid || RES.data.uid}][qq: ${this.e.user_id}] 验证通过!) if (this.e.group) { this.e.reply(uid:${this.uid} 验证通过!, true) } else if (this.e.isPrivate) { this.e.reply(uid:${this.uid}验证通过!, true) } res = await _MysApi.getData('verifyVerification', res.data) return res.data.challenge } } } catch (error) { // 处理错误 }}

代码解析

  1. 设置目标URL: 首先,您需要将目标URL的内容填写到 targetUrl 变量中,并将与之相关的URL填写到 url 变量。

  2. 发送异步请求: 代码使用 fetch 函数发送一个POST请求到指定的URL。请求体中包含 inturl 参数,该参数的值需要根据实际情况进行填写。

  3. 提取验证码链接: 代码使用正则表达式 /<input type='text' value='+\S+' disabled='disabled'>/g 从响应文本中提取包含验证码链接的HTML标签,并使用字符串替换方法将其转换为可访问的URL。

  4. 通知用户验证: 代码将提取到的验证码链接发送给用户,并提示用户尽快完成验证。

  5. 轮询验证结果: 代码使用 for 循环每隔一秒钟检查一次验证结果,直到验证成功或达到最大尝试次数。

  6. 处理验证结果: 根据验证结果,代码会输出相应的日志信息,并通知用户验证结果。

注意事项

  • 代码中使用了一些占位符,例如${RES.data.gt}${RES.data.challenge},需要根据实际情况替换为真实的值。* 部分语法可能存在问题,例如缺少错误处理机制,需要根据实际情况进行调整和补充。

希望本文能够帮助您更好地理解这段代码的工作原理。如果您有任何问题,请随时提出!


原文地址: https://www.cveoy.top/t/topic/Q2m 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录