Greasy Fork

Greasy Fork is available in English.

Show prices on Steam followedgames page

在Steam社区"已关注的游戏"页面显示游戏的价格

当前为 2022-11-04 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Show prices on Steam followedgames page
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  在Steam社区"已关注的游戏"页面显示游戏的价格
// @author       lyzlyslyc
// @match        http*://steamcommunity.com/*/followedgames/
// @icon         https://store.steampowered.com/favicon.ico
// @grant        GM_xmlhttpRequest
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...

    let interval = 200;
    setInterval(requestPrice,interval);

    function requestPrice(){
        //获取关注列表
        let apps = document.querySelector(".games_list_rows").children;
        for(let i=0;i<apps.length;i++){
            if(apps[i].hasPrice||apps[i].hasPrice=="loading")continue;
            //在显示区域内才加载,防止请求过多
            if(!isAppVisible(apps[i]))continue;
            let appUrl = apps[i].querySelector("a").href;
            if(appUrl){
                console.log("loading");
                //获取appid
                let appId = appUrl.match(/app\/(\d+)/)[1];
                //发送查询请求
                GM_xmlhttpRequest({
                    method: "get",
                    url: `https://store.steampowered.com/api/appdetails?appids=${appId}&cc=cn`,
                    responseType: "json",
                    timeout: 3e4,
                    onload: function (result) {
                        result = result.response;
                        //可能返回空数组
                        if (result) {
                            let price = null;
                            //可能应用已下架
                            if(!result[appId].success){
                                price = {
                                    discount_percent: 0,
                                    final : 0,
                                    final_formatted: "Not for Sale"
                                };
                            }
                            else if(result[appId].data.is_free){
                                price = {
                                    discount_percent: 0,
                                    final : 0,
                                    final_formatted: "Free to Play"
                                };
                            }
                            //获取价格
                            else price = result[appId].data.price_overview;
                            //可能暂无定价
                            if(!price){
                                price = {
                                    discount_percent: 0,
                                    final : 0,
                                    final_formatted: "Coming Soon"
                                };
                            }
                            let content = generatePrice(price);
                            let span = document.createElement("span");
                            span.innerHTML = content;
                            span.className = "game_purchase_action_bg";
                            let a = apps[i].querySelector(".followed_game_actions a");
                            a.parentElement.prepend(span);
                            apps[i].hasPrice = true;
                        }
                    },
                    ontimeout:function(){
                        let content =
                            `<span class="discount_block  no_discount discount_block_inline" data-price-final="0">`+
                            '<div class="discount_prices">'+
                            `<div class="discount_final_price">Request Timeout</div>`+
                            '</div>'+
                            '</span>';
                        let span = document.createElement("span");
                        span.innerHTML = content;
                        span.className = "game_purchase_action_bg";
                        let a = apps[i].querySelector(".followed_game_actions a");
                        a.parentElement.prepend(span);
                        apps[i].hasPrice = false;
                    }
                })
                apps[i].hasPrice = "loading";
            }
    }
    }

    function generatePrice(price){
        let content = "";
        let template = "";
        if(price.discount_percent==0){
            template =
                `<span class="discount_block  no_discount discount_block_inline" data-price-final="${price.final}">`+
                    '<div class="discount_prices">'+
                        `<div class="discount_final_price">${price.final_formatted}</div>`+
                    '</div>'+
                '</span>';
        }
        else {
            template =
                `<span class="discount_block  discount_block_inline" data-price-final="${price.final}">`+
                    `<div class="discount_pct">-${price.discount_percent}%</div>`+
                    '<div class="discount_prices">'+
                        `<div class="discount_original_price">${price.initial_formatted}</div>`+
                        `<div class="discount_final_price">${price.final_formatted}</div>`+
                    '</div>'+
                '</span>'
        }
        content+=template;
        return content;
    }

    function isAppVisible(app){
        let s = app.offsetTop;    // 元素相对于页面顶部的距离
        let x = app.offsetHeight;    //元素自身高度
        let t = document.documentElement.scrollTop;  // 页面在垂直方向上滚动的距离
        let y = document.documentElement.clientHeight;   //窗口可视区域的高度
        let isHidden = (s+x) < t || (s > (t+y));
        return !isHidden
    }
})();