V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
SimonOne
V2EX  ›  分享创造

写了个 surge 脚本, hifini 自动定时签到

  •  
  •   SimonOne · 223 天前 · 1415 次点击
    这是一个创建于 223 天前的主题,其中的信息可能已经有所发展或是发生改变。

    不懂 js 的苦手写的难受死了(其实除了 abap 别的也不会)。

    花了一晚上,主要时间浪费在“网络请求是异步的,必须等到前一个函数获取 sign 后才能开始下一个函数签到”,但是我没有用 js 异步的经验,一直在 google 怎么样能让上一个函数异步完成拿到 sign 后再签到。

    贴出来给大家看下,要是有大哥能帮忙优化下就更好了。😆

    const jobName = "hifini.com 签到"
    const cookie = $persistentStore.read("hifiniDailyBonusCookie")
    const recentSignDate = $persistentStore.read("hifiniDailyBonusRecentSignDate")
    const today = new Date()
    const month = today.getMonth() + 1
    const signDate = `${today.getFullYear()}${month}${today.getDate()}`
    
    // 获取 sign
    function hifiniGetSign() {
      return new Promise(resolve => {
        console.log("获取签到的 sign")
        // 获取签到的 sign
        const request = {
          url: "https://www.hifini.com/",
          headers: {
            "Cookie": cookie,
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
          }
        }
        $httpClient.get(request, (error, response, data) => {
          console.log("发送请求")
          if (response.status == 200) {
            var regex = /var sign = \"(\w+)";/
            var match = regex.exec(data);
            if (match !== null) {
              var sign = match[1]
              console.log(`已取得 sign=${sign}`)
              resolve(sign)
            } else {
              console.log(`未通过正则表达式${regex}找到匹配的 sign`)
              $done()
            }
          } else {
            console.log(`获取 sign 失败,返回码${response.status}`)
            $done()
          }
        })
      })
    }
    
    // 签到
    function hifiniSign(sign) {
      if (signDate !== recentSignDate) {
        console.log("本机今天尚未签到,开始签到请求")
        const request = {
          url: "https://www.hifini.com/sg_sign.htm",
          headers: {
            "Accept-Language": "zh-Hans,zh-CN;q=0.9,zh;q=0.8",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Cookie": cookie,
            "x-requested-with": "XMLHttpRequest",
            "Referer": "https://www.hifini.com/",
            "Sec-Ch-Ua": "\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\"",
            "Sec-Ch-Ua-Mobile": "?0",
            "Sec-Ch-Ua-Platform": "\"macOS\"",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-origin",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
          },
          body: `sign=${sign}`
        }
        $httpClient.post(request, (error, response, data) => {
          console.log("发送签到请求")
          if (response.status == 200) {
            if (data == null) {
              console.log("请求成功,返回空")
              const title = jobName
              const subTitle = "请求成功,返回空,检查下"
              $notification.post(title, subTitle, "")
            } else {
              if (JSON.parse(data).code == 0) {
                const title = jobName
                const subTitle = "签到成功"
                const detail = JSON.parse(data).message
                console.log(detail)
                $persistentStore.write(signDate, "hifiniDailyBonusRecentSignDate")
                $notification.post(title, subTitle, detail)
              } else if (JSON.parse(data).code == -1) {
                const title = jobName
                const subTitle = "签到跳过"
                const detail = JSON.parse(data).message
                console.log(detail)
                $persistentStore.write(signDate, "hifiniDailyBonusRecentSignDate")
                $notification.post(title, subTitle, detail)
              } else {
                const title = jobName
                const subTitle = "签到失败"
                console.log(detail)
                const detail = `error: ${error}, response: ${response}, data: ${data}`
                $notification.post(title, subTitle, detail)
              }
            }
          } else {
            console.log(`请求失败,返回码${response.status}`)
            const title = jobName
            const subTitle = `请求失败,返回码${response.status}`
            $notification.post(title, subTitle, "")
          }
          $done()
        })
      } else {
        console.log("今天已经签过")
        const title = jobName
        const subTitle = "签到跳过"
        const detail = "今天已经签过啦!"
        $notification.post(title, subTitle, detail)
        $done()
      }
    }
    
    hifiniGetSign().then(sign => {
      hifiniSign(sign)
    })
    
    4 条回复    2024-05-14 19:41:33 +08:00
    RangerWolf
        1
    RangerWolf  
       222 天前   ❤️ 1
    脚本没用上,但是发现了一个宝藏网站,感谢 op
    jpyl0423
        2
    jpyl0423  
       222 天前
    我本来想用 python 写一个放到青龙面板的,太懒一直没写,直接用 lz 的吧
    blankmiss
        3
    blankmiss  
       222 天前
    有验证码 模拟登录 很麻烦
    SimonOne
        4
    SimonOne  
    OP
       222 天前
    @blankmiss #3 😂所以改成半自动的,登录上了,拿到 cookie ,偶尔 cookie 失效了就再手动取下。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2874 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:51 · PVG 20:51 · LAX 04:51 · JFK 07:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.