Greasy Fork

Greasy Fork is available in English.

Buff批量购买商品

Buff商品界面批量购买

当前为 2021-09-07 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Buff批量购买商品
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Buff商品界面批量购买
// @author       jklujklu
// @include      https://buff.163.com/goods/*
// @icon         https://g.fp.ps.netease.com/market/file/59b156975e6027bce06e8f6ceTyFGdsj
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const LIST = 'https://buff.163.com/api/market/goods/sell_order'
    const ORDER = 'https://buff.163.com/api/market/goods/buy/preview'
    const PAY = 'https://buff.163.com/api/market/goods/buy'

    // 购买最大单价
    let max_price = 0;
    // 购买最大数量
    let max_count = 1;
    // 查询单页数量
    let page_size = 20;

    let item_id = document.querySelector('.add-bookmark').getAttribute('data-target-id');
    document.querySelector('#asset_tag-filter').style.minHeight = '70px'

    const header = document.querySelector('.market-header')
    const div = document.createElement('div')
    header.insertBefore(div, header.children[0])

    const right = document.querySelector('.criteria .l_Right')
    const btn = document.createElement('div')
    btn.innerText = '一键购买'
    btn.classList.add('i_Btn','i_Btn_mid');
    btn.style.background = '#45536C'
    btn.onclick = run;
    right.insertBefore(btn, right.children[0])

    const price_input = document.createElement('input')
    price_input.classList.add('i_Text')
    price_input.style.margin = '5px'
    price_input.style.width = '80px'
    price_input.placeholder = 'Max Price'
    right.insertBefore(price_input, right.children[0])

    const number_input = document.createElement('input')
    number_input.classList.add('i_Text')
    number_input.style.margin = '5px'
    number_input.style.width = '80px'
    number_input.placeholder = 'Max Count'
    right.insertBefore(number_input, right.children[0])


    function getCookie(name) {
        var prefix = name + "="
        var start = document.cookie.indexOf(prefix)

        if (start == -1) {
            return null;
        }

        var end = document.cookie.indexOf(";", start + prefix.length)
        if (end == -1) {
            end = document.cookie.length;
        }

        var value = document.cookie.substring(start + prefix.length, end)
        return unescape(value);
    }

    function ajax(method, url, data){
        return new Promise(resolve=>{
            if(method === 'get'){
                $.getJSON(url, data, rs => {
                    // console.log(rs);
                    resolve(rs)
                })
            }else if(method === 'post'){
                $.ajax({
                    type: "POST",
                    url: url,
                    data: data,
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader("X-CSRFToken", getCookie('csrf_token'));
                    },
                    contentType: "application/json",
                    dataType: "json",
                    success: rs => resolve(rs)
                });
            }

        })
    }

    const params = {
        game:'csgo',
        goods_id:item_id,
        page_num:1,
        sort_by:'default',
        mode:null,
        allow_tradable_cooldown:1,
        _:Date.parse(new Date()),
        page_size: page_size
    }

    async function run(){
        div.innerHTML = '';
        max_price = parseFloat(price_input.value);
        max_count = parseInt(number_input.value);
        div.innerHTML += (`你设定的价格为:${max_price}, 数量为:${max_count}</br>`);

        div.innerHTML += (`获取最新商品列表</br>`);

        const rs = await ajax('get',LIST, params);
        const items = rs['data']['items'];

        const newItems = items.filter(e => e['price'] < max_price);
        div.innerHTML += (`共筛选出:${newItems.length}个商品!</br>`);

        let success_count = 0;
        let price_count = 0;
        for (let index = 0; index < newItems.length; index++) {
            const element = newItems[index];
            const price = element['price'];
            const id = element['id'];
            console.log(price, id);

            // 下订单
            const order_params = {
                game: "csgo",
                sell_order_id: id,
                goods_id: item_id,
                price: price,
                allow_tradable_cooldown: "0",
                cdkey_id:null,
                _:Date.parse(new Date()),
            }
            const rs = await ajax('get',ORDER, order_params);

            // 商品已被购买
            if(rs['code'] !== 'OK'){
                console.log(rs['error']);
                div.innerHTML += (`错误:${rs['error']}</br>`);
                continue;
            }

            const is_enough = rs['data']['pay_methods'][0]['enough']
            const error = rs['data']['pay_methods'][0]['error']
            // 余额不足
            if(!is_enough){
                console.log(error);
                div.innerHTML += (`错误:${error}</br>`);
                break;
            }
            // 其他错误,如不支持支付宝购买
            if(error){
                console.log(error);
                div.innerHTML += (`错误:${error}</br>`);
                continue;
            }

            const coupon_params = {
                state: "unuse",
                coupon_type: "reduction",
                order_amount: price,
                sell_order_id: id,
                _: Date.parse(new Date()),
            }

            // await ajax('get', COUPON, coupon_params);

            const data = {
                "game":"csgo",
                "goods_id":item_id,
                "sell_order_id":id,
                "price":price,
                "pay_method":3,
                "allow_tradable_cooldown":0,
                "token":"","cdkey_id":""
            }
            const pay_rs = await ajax('post', PAY, JSON.stringify(data));
            console.log(pay_rs);
            if(pay_rs['code'] === 'OK'){
                success_count = success_count + 1;
                price_count = price_count + parseFloat(price);
                console.log('购买成功!');
                div.innerHTML += (`${id}购买成功,共花费${price}</br>`);
                if(success_count === max_count){
                    break;
                }
            }else{
                console.log(pay_rs['error']);
            }

        }

        div.innerHTML += (`总计购买成功:${success_count}, 共花费${price_count}</br>`);
    }
})();