UnluckyNinja / 浏览Steam时自动领取Steam贴纸

// ==UserScript==
// @name         浏览Steam时自动领取Steam贴纸
// @name:zh      浏览Steam时自动领取Steam贴纸
// @name:en      Auto claim Steam stickers when browsing Steam
// @namespace    https://github.com/UnluckyNinja
// @version      0.2
// @description  在2023春季促销期间自动领取贴纸(若有),
// @description:en  Auto claim free stickers in spring sale (if any)
// @author       UnluckyNinja
// @license      MIT
// @match        https://store.steampowered.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=steampowered.com
// @grant        none
// ==/UserScript==

(async function() {
    'use strict';

    function log(text){
        console.log(`[自动领春促贴纸油猴脚本] ${text}`)
    }

    // 为避免脚本在促销期之外进行不必要的请求,超过时间直接跳过
    if (Date.now()/1000 > 1679677200) { // 3月25日凌晨1点,促销应该是在国内时间24日凌晨1点结束,加一天容错期
        log('未在春促期内,自动跳过,你可以移除该脚本了,或者等待下次更新')
        return
    }

    // before any request, check if there is a web api token on the page, if not, request to a valid page, if no valid response again, take it as not claimable
    let webapi_token = null
    if( window.application_config?.dataset?.loyalty_webapi_token){
        webapi_token = JSON.parse(window.application_config.dataset.loyalty_webapi_token)
    } else {
        const res = await fetch('/category/action')
        const html = await res.text()
        const doc = new DOMParser().parseFromString(html, 'text/html')
        const token = doc.getElementById('application_config')?.dataset?.loyalty_webapi_token
        if (!token){
            log('未找到有效api token,是否未登录?')
            return
        }
        webapi_token = JSON.parse(token)
    }

    // can claim check
    const res = await fetch(`https://api.steampowered.com/ISaleItemRewardsService/CanClaimItem/v1/?access_token=${webapi_token}`)
    const json = await res.json()

    const can_claim = !!json.response?.can_claim
    const next_claim_time = json.response?.next_claim_time

    // request to /ClaimItem
    if(can_claim){
        await fetch(`https://api.steampowered.com/ISaleItemRewardsService/ClaimItem/v1/?access_token=${webapi_token}`, { method: 'POST'})
        log('领取完成')
    } else {
        if(next_claim_time){
            log('今日已领取,下次领取时间在:'+new Date(next_claim_time * 1000).toLocaleString())
        } else {
            log('无可领取内容,跳过')
        }
    }
})();