2013-07-18 57 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); 
}