Steam、Epic历史价格查询

跳转到SteamDB、EpicGamesDB

// ==UserScript==
// @name         Steam、Epic历史价格查询
// @description  跳转到SteamDB、EpicGamesDB
// @author       shopkeeperV
// @namespace    https://greasyfork.org/zh-CN/users/150069
// @version      1.0.3
// @match        https://store.steampowered.com/*
// @match        https://store.epicgames.com/*
// @grant        window.onurlchange
// ==/UserScript==

(function () {
    'use strict';
    let host = location.host;
    let path = location.pathname;
    let db_url;
    if (/steampowered/.test(host)) {
        let page_type;
        let title_eles;
        if (/app/.test(path)) {
            page_type = "app";
            title_eles = document.getElementsByClassName("apphub_AppName");
            let wrappers = document.getElementsByClassName("game_area_purchase_game_wrapper");
            for (let wrapper of wrappers) {
                let item_type;
                let item_id;
                let form = wrapper.getElementsByTagName("form")[0];
                let inputs = form.getElementsByTagName("input");
                for (let input of inputs) {
                    if (/subid|bundleid/.test(input.name)) {
                        if (/subid/.test(input.name)) {
                            item_type = "sub";
                        } else if (/bundleid/.test(input.name)) {
                            item_type = "bundle";
                        }
                        item_id = input.value;
                        break;
                    }
                }
                db_url = "https://steamdb.info/" + item_type + "/" + item_id + "/#pricehistory";
                wrapper.getElementsByTagName("h1")[0].appendChild(createASpan(db_url));
            }
        } else {
            if (/sub/.test(path)) {
                page_type = "sub";
            } else if (/bundle/.test(path)) {
                page_type = "bundle";
            } else return;
            title_eles = document.getElementsByClassName("pageheader");
        }
        let page_id = path.match(/\/([0-9]*?)\//i)[1];
        for (let title_ele of title_eles) {
            db_url = "https://steamdb.info/" + page_type + "/" + page_id + "/#pricehistory";
            title_ele.appendChild(createASpan(db_url));
        }
    }
    if (/epicgames/.test(host)) {
        let timer;
        handler();
        window.addEventListener('urlchange', () => {
            console.log("Epic历史价格查询:监听到地址变化。");
            handler();
        });

        function handler() {
            if (/\/p\//.test(location.pathname)) {
                let ele;
                timer = setInterval(() => {
                    console.log("Epic历史价格查询:正在获取页面id...");
                    ele = document.getElementById("_schemaOrgMarkup-Product");
                    if (ele) {
                        clearInterval(timer);
                        console.log("Epic历史价格查询:已清除定时器。");
                        let content = ele.textContent;
                        let id = content.match(/"sku":"([^"]*):([^"]*)"/)[2];
                        db_url = "https://epicgamesdb.info/p/" + id + "/" + path.split("/").pop();
                        document.getElementsByTagName("h1")[0].appendChild(createASpan(db_url));
                    }
                }, 500);
            }
        }
    }

    function createASpan(url) {
        let span = document.createElement("span");
        span.setAttribute("class", "history_price");
        span.textContent = "查价";
        span.style.cssText = "display:inline-block;margin-left:10px;color:yellow;cursor:pointer;";
        span.onclick = (e) => {
            window.open(url);
        };
        return span;
    }
})();