Steam Gems-to-Price Helper

处理垃圾库存的好帮手 ╰( ̄▽ ̄)╭

目前為 2016-12-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Steam Gems-to-Price Helper
// @namespace    http://nota.moe/
// @version      0.4
// @description  处理垃圾库存的好帮手 ╰( ̄▽ ̄)╭
// @author       NotaStudio
// @match        *://steamcommunity.com/*/inventory/*
// @grant        GM_info
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-end
// @license      GPLv3
// ==/UserScript==

/* 
 * ChangeLog
 * 20161224 0.4
 * 添加初始设置
 * 添加手动设置货币单位功能
 * 修复了快速点击物品时同时显示多个价格的 bug
 * 降低了请求宝珠价格的频率,可能会有微弱的性能提升(或性能下降 o(≧v≦)o~~ )
 * 圣诞快乐~
 * 20161217 0.3
 * 临时修复国区价格显示为美元的 bug
 * 20161217 0.2
 * 修复价格显示为"约为 undefined NaN" 的 bug
 * 20161216 0.1-alpha
 * 初次发布
 */

(function($) {
    const ver = GM_info.script.version,
        dateVer = '2016.12.24';

    console.log(`Steam Gems-to-Price Helper ${ver}\nCreated by Nota\n${dateVer}`);

    let gemsCount,
        gemsPrice,
        gemsValue,
        valueSpan,
        localAmountToken;

    let currency = {
        "USD": 1,
        "GBP": 2,
        "EUR": 3,
        "CHF": 4,
        "RUB": 5,
        "PLN": 6,
        "BRL": 7,
        "JPY": 8,
        "NOK": 9,
        "IDR": 10,
        "MYR": 11,
        "PHP": 12,
        "SGD": 13,
        "THB": 14,
        "VND": 15,
        "KRW": 16,
        "TRY": 17,
        "UAH": 18,
        "MXN": 19,
        "CAD": 20,
        "AUD": 21,
        "NZD": 22,
        "CNY": 23,
        "INR": 24,
        "CLP": 25,
        "PEN": 26,
        "COP": 27,
        "ZAR": 28,
        "HKD": 29,
        "TWD": 30,
        "SAR": 31,
        "AED": 32
    };

    let setup = (init) => {
        let initTips = [`Steam Gems-to-Price Helper 已升级到 ${ver} 版本! 请进行初始设置.<br><br>`, `<br><br> 以后, 您可以随时按下 Alt+G 更改本页设置.`],
            form = `<form style="font-size: 16px;line-height: 25px;text-align: center;" class="currencyForm"><b>我的币种代码是</b><select class="sg2ph_select" style="font-weight: bold;"></select><br><i>不支持符号写在数字后方的货币单位,如欧元</i><br><i>不支持符号大于一位的货币单位,如港币</i></form>`;
        if (init) form = initTips[0] + form + initTips[1];
        ShowAlertDialog('设置 Steam Gems-to-Price Helper 币种', form);
        let nowID = GM_getValue('currencyId',23);
        for (let c in currency) {
            let opt = $(`<option value="${currency[c]}">${c}</option>`);
            if (currency[c] == nowID) opt.attr('selected', 'selected');
            opt.appendTo($('.sg2ph_select'));
        }
        let realButton = $('.newmodal:not(#market_sell_dialog) .btn_grey_white_innerfade');
        realButton.css('visibility', 'hidden');
        let overrideButton = $(`<div class="btn_green_white_innerfade btn_medium sg2ph_button"><span>确定(将会刷新页面)</span></div>`);
        overrideButton.insertAfter(realButton);
        overrideButton.on('click', (() => {
        	GM_setValue('currencyId',$('.sg2ph_select').val());
        	GM_setValue('currentVersion',ver);
            realButton.click();
            location.reload();
        })); // Steam 提供的模态窗口不支持回调函数,所以小小地 hack 了一下,用自定义的确认按钮替换掉原来的
    };

    if (GM_getValue('currentVersion') !== ver) setup(true); // 初始设置

    let parseAmount = (amount) => Number.parseFloat(amount.slice(2)); // 去除货币符号和空格

    let showValue = (xhr) => {
        gemsCount = Number.parseFloat(xhr.responseJSON.goo_value); // 获取当前物品可分解的宝珠数量
        gemsValue = (gemsPrice / 1000 * gemsCount).toFixed(2);
        valueSpan = $('span.item_scrap_value:visible')[0]; // span.item_scrap_value 会对应两个 SPAN 元素,其中可见者则为当前宝珠价值
        valueSpan.innerHTML += `<span style="color: #FF0;cursor: help;" class="sg2ph_valueBox" title="价格显示错误?按 Alt+G 修改设置!">&nbsp;&nbsp;&nbsp;&nbsp;约为${localAmountToken} ${gemsValue}</span>`;
    };

    let ajaxHandler = (event, xhr, settings) => {
        if (settings.url.match(/ajaxgetgoovalueforitemtype/)) { // Steam 会在点击某一库存物品时进行 3 次 jQuery AJAX 请求.筛选出请求宝珠数量的那一次
            setTimeout((() => (showValue(xhr))), 100);
            $('.sg2ph_valueBox').remove();
        }
    };

    let refreshPrice = () => {
        let apiUrl = `https://steamcommunity.com/market/priceoverview/?appid=753&currency=${GM_getValue('currencyId',1)}&market_hash_name=753-Sack%20of%20Gems`,
            xhr = new XMLHttpRequest(),
            data;

        xhr.open('GET', apiUrl, false);
        xhr.send();
        data = JSON.parse(xhr.responseText);
        localAmountToken = data.lowest_price.slice(0, 1);
        gemsPrice = parseAmount(data.lowest_price); // 获取当前"一袋宝珠"价格

        setTimeout(refreshPrice, 45000);
    };

    let keyHandler = (e) => {
    	if (e.keyCode == 71 && e.altKey) setup(false); // 同时按下 Alt+G
    };

    refreshPrice();
    $(document).keydown(keyHandler);
    $(document).ajaxComplete(ajaxHandler);

})(jQuery);