问题
I m making a greasmonkey script to convert currency in a specific page to INR using API. I am able to get the current currency rate of USD-INR and also able to replace currencies symbols to INR but m not able to convert the currency value to current rate. I am using this script:
$(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);
}
});
});
The above script replace all the occurrences of $s to Rs. in a page like:
$0.50, $1.00, $20.00, $200.00
to
₹0.50, ₹1.00, ₹20.00, ₹200.00
but what i want is to convert the currency also with exchange rate like:
₹29.5, ₹59, ₹1180, ₹11800
I am not getting upto this..
PLease help..
**OR SOMEONE HAVE BETTER IDEA TO DO THIS CONVERSION. PLZ SUGGEST**
回答1:
You need to recurse or loop through just the nodes that have the dollar values. Never use replace() or RegExp on .html() or innerHTML. Not only will this trash the target page, but you will have a devil of a time getting the desired results.
Recurse through the page using DOM methods. Note that currency values come in many formats, so converting them can get complex.
Here's moderately robust code for the whole page (sans iframes) 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;
}
}
If this is just for a few pages, use jQuery or document.querySelectorAll() to process just the elements you are interested in. For example:
var targElements = document.querySelectorAll ("div.priceTable");
for (var J = targElements.length - 1; J >= 0; --J) {
changeDollarsToRupees (targElements[J], convRate);
}
回答2:
I remanded to use Google Finance
http://www.google.com/finance/converter?a=1&from=USD&to=INR
above link is better then http://www.google.com/ig/calculator
Some time Google ig-calculator stop working but Google finance always available
来源:https://stackoverflow.com/questions/17719228/greasemonkey-currency-converter