Greasy Fork

Greasy Fork is available in English.

Humble Bundle Auto Redeem

HB download 页面刮key

当前为 2022-03-19 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Humble Bundle Auto Redeem
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  HB download 页面刮key
// @author       kumi
// @match        https://www.humblebundle.com/downloads?key=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=humblebundle.com
// @license      MIT
// ==/UserScript==

(async function () {
  'use strict';
  const request = async ({ url, method, body, headers }) => {
    const res = await fetch(url, {
      method: method || 'GET',
      body: body || null,
      headers
    })
    if (res.status !== 200) return {}
    return res.json()
  }
  const parentEle = document.querySelector('.js-subproduct-whitebox-holder')
  const ele = document.createElement('div')
  ele.innerHTML = `<button style="display: block; font-size: 20px; margin-bottom: 10px; width: 120px; height: 40px;">全部刮开</button><textarea style="width: 50%; height: 300px;"></textarea>`
  parentEle.insertBefore(ele, parentEle.firstElementChild)
  ele.firstElementChild.onclick = async function() {
    if(this.innerText !== '全部刮开') return
    this.innerText = `正在刮key...`
    const [, orderId] = location.href.match(/downloads\?key=(\w+)/) || []
    const { tpkd_dict: { all_tpks } } = await request({
      url: `https://www.humblebundle.com/api/v1/order/${orderId}?all_tpkds=true`,
    })
    const newGameList = all_tpks.reduce((obj, item) => {
      if (!obj[item.key_type_human_name]) obj[item.key_type_human_name] = []
      obj[item.key_type_human_name].push(item)
      return obj
    }, {})
    let count = 0
    this.innerText = `${count}/${all_tpks.length}`
    const platform = Object.keys(newGameList)
    const gameText = []
    for (let i = 0; i < platform.length; i++) {
      const keyType = platform[i]
      let str = keyType + '平台'
      for (let j = 0; j < newGameList[keyType].length; j++) {
        const gameItem = newGameList[keyType][j]
        try {
          const result = await request({
            url: 'https://www.humblebundle.com/humbler/redeemkey',
            body: `keytype=${gameItem.machine_name}&key=${gameItem.gamekey}&keyindex=${gameItem.keyindex}`,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            },
            method: 'POST'
          })
          str += `\n${gameItem.human_name},` + result.key
          console.log(result)
        } catch (e) {
          str += `\n${gameItem.human_name},请求失败`
          console.error(e)
        } finally {
            this.innerText = `${++count}/${all_tpks.length}`
        }
      }
      gameText.push(str)
    }
    ele.lastElementChild.value = gameText.join('\n\n')
  }

})();