2013-07-18 54 views
1

我使用greasmonkey脚本将特定页面中的货币转换为使用API​​的INR。 我能够获得美元兑印度卢比的当前货币汇率,也能够将货币符号替换为印度卢比,但不能将货币值转换为当前汇率。我正在使用以下脚本:greasemonkey货币转换器

$(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 

₹0.50, ₹1.00, ₹20.00, ₹200.00 

,但我想要的是货币转换也与汇率这样的:

₹29.5, ₹59, ₹1180, ₹11800 

我没有得到这个高达..

PLease help ..

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

能否请你告诉一个链接到你所创建的Greasemonkey的脚本?此外,它是可定制的,并可以在任何网站上工作? –

回答

2

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

使用DOM方法遍历页面。请注意,货币价值有多种格式,因此转换它们可能会变得复杂。

这里是整个页面适度健壮的代码(没有I帧)You can see it in action at jsFiddle

// ==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; 
    } 
} 


如果这仅仅是几页,使用jQuery或document.querySelectorAll()处理只是元素,你例如:

var targElements = document.querySelectorAll ("div.priceTable"); 

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