Greasy Fork

Greasy Fork is available in English.

Steam Price Exchanger

Exchange price on steam

当前为 2020-09-02 提交的版本,查看 最新版本

// ==UserScript==
// @name         Steam Price Exchanger
// @version      0.2.1
// @description  Exchange price on steam
// @author       lzt
// @match        https://store.steampowered.com/*
// @grant        GM_xmlhttpRequest
// @connect      www.baidu.com
// @namespace http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...

    var style = document.createElement("style");
	style.type = "text/css";
	var text = document.createTextNode(".tab_item_discount {width: 180px;}");
	style.appendChild(text);
	var head = document.getElementsByTagName("head")[0];
	head.appendChild(style);

    unsafeWindow.rub = {'rate': 0, 'lock': 0};
    unsafeWindow.ars = {'rate': 0, 'lock': 0};

    GM_xmlhttpRequest({
        method: "get",
        url: 'https://www.baidu.com/s?wd=1RUB',
        responseType: 'text',
        onload: function(r) {
            var parser=new DOMParser();
        	var htmlDoc=parser.parseFromString(r.response, "text/html");
        	var l = document.evaluate("//*[contains(text(), '1俄罗斯卢布')]", htmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
            unsafeWindow.rub['rate'] = parseFloat(l.snapshotItem(0).firstChild.nodeValue.split('=')[1]);
            console.log(unsafeWindow.rub['rate'] + " RUB/CNY");
            unsafeWindow.fillprice(unsafeWindow.rub, 'pуб', 1);
            initobserver(unsafeWindow.rub, 'pуб');
        }
    });

    GM_xmlhttpRequest({
        method: "get",
        url: 'https://www.baidu.com/s?wd=1ARS',
        responseType: 'text',
        onload: function(r) {
        	var parser=new DOMParser();
        	var htmlDoc=parser.parseFromString(r.response, "text/html");
        	var l = document.evaluate("//*[contains(text(), '1阿根廷比索')]", htmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
            unsafeWindow.ars['rate'] = parseFloat(l.snapshotItem(0).firstChild.nodeValue.split('=')[1]);
            console.log(unsafeWindow.ars['rate'] + " ARS/CNY");
            unsafeWindow.fillprice(unsafeWindow.ars, 'ARS', 1);
            initobserver(unsafeWindow.ars, 'ARS');
        }
    });
    function initobserver (rate, unit) {
	    var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
	    var target = document.body;
	    var observer = new MutationObserver(function(mutations) {  
			mutations.forEach(function(mutation) {
				unsafeWindow.fillprice(rate, unit, 300)
				}); 
			}); 
	    var config = { attributes: true, childList: true, characterData: true, subtree: true}
	    observer.observe(target, config);
	}

    unsafeWindow.fillprice = function (rate, unit, delay){
    	if (rate['lock'] == 1) {return -1};
    	rate['lock'] = 1;
        var lists = document.evaluate("//*[contains(text(), '" + unit + "')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        var re = new RegExp(unit == "ARS"?"ARS\\$?\\s*[0-9.,]+":"[0-9.,]+\\s*pуб.?", "ig");
        for(var i = 0; i < lists.snapshotLength; i++) {
        	var item = lists.snapshotItem(i)
            if (item.firstChild.nodeValue == null) {continue};
            if (item.firstChild.nodeValue.search("¥") != -1) {continue};
            if (item.classList.contains("es-regprice") &item.classList.contains("es-flag")) {continue};
            var s = item.firstChild.nodeValue.match(re)
            if (s != null) {
                for(var j = 0; j < s.length; j++) {
                    var price = s[j].replace(".", "");
                    price = price.replace(",", ".");
                    price = "¥" + parseInt(parseFloat(price.match(/[0-9.]+/))*rate['rate']).toString();
                    if ((item.classList.contains("col") & item.classList.contains("search_price")) | 
                    	item.nodeName == "STRIKE") {
                    	item.firstChild.nodeValue = item.firstChild.nodeValue.replace(s[j], s[j].replace(" ", "") + "(" + price + ")").trim();
                    }else {
                    	item.firstChild.nodeValue = item.firstChild.nodeValue.replace(s[j], s[j] + "(" + price + ")").trim()
                    }
                }
            }
        }
        lists = document.getElementsByClassName("col search_price discounted");
        for (var i = 0; i < lists.length; i++) {
        	if (lists[i].childNodes[3].nodeValue == null) {continue};
            if (lists[i].childNodes[3].nodeValue.search("¥") != -1) {continue};
            if (lists[i].childNodes[3].nodeValue.search(unit) == -1) {continue};
        	var price = lists[i].childNodes[3].nodeValue.replace(".", "");
        	price = price.replace(",", ".");
        	price = "¥" + parseInt(parseFloat(price.match(/[0-9.]+/))*rate['rate']).toString();
        	lists[i].childNodes[3].nodeValue = lists[i].childNodes[3].nodeValue.replace(" ", "").trim() + "(" + price + ")";
        }
        if (unit == "ARS") {setTimeout("window.ars['lock'] = 0", delay)}
        	else{setTimeout("window.rub['lock'] = 0", delay)} 
    }
})();