Steam Price Exchanger

Exchange price on steam

目前为 2020-09-01 提交的版本。查看 最新版本

// ==UserScript==
// @name         Steam Price Exchanger
// @version      0.2
// @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...
    unsafeWindow.rub = null
    unsafeWindow.ars = null

    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 = parseFloat(l.snapshotItem(0).firstChild.nodeValue.split('=')[1]);
            console.log(unsafeWindow.rub + " RUB/CNY");
            unsafeWindow.fillprice(unsafeWindow.rub, 'pуб');
            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 = parseFloat(l.snapshotItem(0).firstChild.nodeValue.split('=')[1]);
            console.log(unsafeWindow.ars + " ARS/CNY");
            unsafeWindow.fillprice(unsafeWindow.ars, 'ARS');
            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)
				}); 
			}); 
	    var config = { attributes: true, childList: true, characterData: true, subtree: true}
	    observer.observe(target, config);
	}

    unsafeWindow.fillprice = function (rate, unit){
        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++) {
            if (lists.snapshotItem(i).firstChild.nodeValue == null) {continue};
            if (lists.snapshotItem(i).firstChild.nodeValue.search("¥") != -1) {continue};
            var s = lists.snapshotItem(i).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).toString();
                    
                    if ((lists.snapshotItem(i).classList.contains("col") & lists.snapshotItem(i).classList.contains("search_price")) | 
                    	lists.snapshotItem(i).nodeName == "STRIKE") {
                    	lists.snapshotItem(i).firstChild.nodeValue = lists.snapshotItem(i).firstChild.nodeValue.replace(s[j], s[j].replace(" ", "") + "(" + price + ")").trim();
                    }else {
                    	lists.snapshotItem(i).firstChild.nodeValue = lists.snapshotItem(i).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};
        	var price = lists[i].childNodes[3].nodeValue.replace(".", "");
        	price = price.replace(",", ".");
        	price = "¥" + parseInt(parseFloat(price.match(/[0-9.]+/))*rate).toString();
        	lists[i].childNodes[3].nodeValue = lists[i].childNodes[3].nodeValue.replace(" ", "").trim() + "(" + price + ")";
        }
    }
})();