Javascript greasemonkey货币转换器

Javascript greasemonkey货币转换器,javascript,greasemonkey,Javascript,Greasemonkey,我正在制作一个greasmonkey脚本,使用API将特定页面中的货币转换为INR。 我可以得到美元兑印度卢比的当前汇率,也可以将货币符号替换为印度卢比,但我无法将货币值转换为当前汇率。我正在使用以下脚本: $(function(){ GM_xmlhttpRequest({ method: "GET", url: "http://www.google.com/ig/calculator?hl=en&q=1usd=?inr", onl

我正在制作一个greasmonkey脚本,使用API将特定页面中的货币转换为INR。 我可以得到美元兑印度卢比的当前汇率,也可以将货币符号替换为印度卢比,但我无法将货币值转换为当前汇率。我正在使用以下脚本:

$(function(){
    GM_xmlhttpRequest({
        method: "GET",
        url: "http://www.google.com/ig/calculator?hl=en&q=1usd=?inr",
        onload: function(d){
            d=JSON.stringify(eval("(" + d.responseText + ")"));
            d=$.parseJSON(d);
            p=Math.round(d.rhs.replace(/[^\d\.]/g, ''));
            var replaced=$('body').html().replace(/\$(?:\s+)*(\d+\,\.)?/g, '₹$1');
            $('body').html(replaced);
        }
    });
});
上述脚本将替换页面中出现的所有$s到Rs,如下所示:

$0.50, $1.00, $20.00, $200.00

但我想用汇率来兑换货币,比如:

₹29.5, ₹59, ₹1180, ₹11800
我还没到这个地步

请帮忙

**OR SOMEONE HAVE BETTER IDEA TO DO THIS CONVERSION. PLZ SUGGEST**

您需要递归或循环遍历具有美元值的节点。切勿在
.html()
innerHTML
上使用
replace()
或RegExp。这不仅会破坏目标页面,而且会让你在一段时间内得到想要的结果

使用DOM方法在页面中递归。请注意,货币值有多种格式,因此转换它们可能会很复杂

以下是用于整个页面的适度健壮的代码(无iframes):


我还押使用谷歌金融

上面的链接更好


有一段时间Google ig calculator停止工作,但Google finance始终可用

能否显示您创建的greasemonkey脚本的链接?此外,它是可定制的,可以在任何网站上工作吗?
**OR SOMEONE HAVE BETTER IDEA TO DO THIS CONVERSION. PLZ SUGGEST**
// ==UserScript==
// @name     _Global currency converter, dollars to rupees
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant    GM_xmlhttpRequest
// ==/UserScript==

GM_xmlhttpRequest ( {
    method:     "GET",
    url:        'http://rate-exchange.appspot.com/currency?from=USD&to=INR',
    //Google sends malformed response, not JSON.
    //url:      'http://www.google.com/ig/calculator?hl=en&q=1usd=?inr',

    onload:     function (rsp){
        var rspJSON     = JSON.parse (rsp.responseText);
        var convRate    = rspJSON.rate;
        console.log (rspJSON, convRate);

        changeDollarsToRupees (document.body, convRate);
    }
} );

function changeDollarsToRupees (node, convRate) {
    if (node.nodeType === Node.TEXT_NODE) {
        if (/\$/.test (node.nodeValue) ) {
            processTextNode (node, convRate);
        }
    }
    else if (node.nodeType === Node.ELEMENT_NODE) {
        for (var K = 0, numNodes = node.childNodes.length;  K < numNodes;  ++K) {
            changeDollarsToRupees (node.childNodes[K], convRate);
        }
    }
}

function processTextNode (node, convRate) {
    /*-- Results like:
        ["Three values: ", "$1.10", " ", "$2.20", " ", "$3.00.", ""]
    */
    var moneySplit  = node.nodeValue.split (/((?:\+|\-)?\$[0-9.,]+)/);
    if (moneySplit  &&  moneySplit.length > 2) {
        /*-- Money values will be odd array index, loop through
            and convert all.
        */
        for (var J = 1, L = moneySplit.length;  J < L;  J += 2) {
            var dolVal = parseFloat (moneySplit[J].replace (/\$|,|([.,]$)/g, "") );

            if (typeof dolVal === "number") {
                //var rupVal = "Rs" + Math.round (dolVal * convRate);
                var rupVal = "Rs" + (dolVal * convRate).toFixed (2);
            }
            else {
                var rupVal = moneySplit[J] + " *Err*";
            }
            moneySplit[J] = rupVal;
        }
        //-- Rebuild and replace the text node with the changed value (s).
        var newTxt      = moneySplit.join ("");
        node.nodeValue  = newTxt;
    }
}
var targElements = document.querySelectorAll ("div.priceTable");

for (var J = targElements.length - 1;  J >= 0;  --J) {
    changeDollarsToRupees (targElements[J], convRate);
}