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

192 天前
 SimonOne

不懂 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)
})
1361 次点击
所在节点    分享创造
4 条回复
RangerWolf
192 天前
脚本没用上,但是发现了一个宝藏网站,感谢 op
jpyl0423
192 天前
我本来想用 python 写一个放到青龙面板的,太懒一直没写,直接用 lz 的吧
blankmiss
191 天前
有验证码 模拟登录 很麻烦
SimonOne
191 天前
@blankmiss #3 😂所以改成半自动的,登录上了,拿到 cookie ,偶尔 cookie 失效了就再手动取下。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1040243

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX