Skip to content

Instantly share code, notes, and snippets.

@divinity76
Last active November 5, 2021 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divinity76/eef43078ea3f023c62a9a74a6a8188da to your computer and use it in GitHub Desktop.
Save divinity76/eef43078ea3f023c62a9a74a6a8188da to your computer and use it in GitHub Desktop.
(function(){
"use strict";
const USD_PER_EURO = 1.15; // used to be 1.123
const USD_PER_NOK = 0.12;
let recur=function(ele){
if(ele.nodeName === "SCRIPT"){
return;
}
if(ele.nodeName==="#text"){
// euro->usd
let text=ele.nodeValue;
let rex = /(?<euro_text>(?<euro_amount>\d+(?:\.\d+)?)\s*\€)/;
let exec = rex.exec(text);
if(!exec){
rex = /(?<euro_text>\€\s*(?<euro_amount>\d+(?:\.\d+)?))/;
exec = rex.exec(text);
}
if(exec){
let euro_text = exec.groups.euro_text;
let euro_amount = exec.groups.euro_amount;
let USD = euro_amount * USD_PER_EURO;
text = text.substring(0,exec.index) + USD.toFixed(2) + "$" + text.substring(exec.index + euro_text.length);
ele.nodeValue = text;
console.log(text);
}
// nok -> usd
rex = /(?<nok_text>kr\s*(?<nok_amount>\d+(?:(?:\.|\,)\d+)?))/;
exec = rex.exec(text);
if(exec){
let nok_text = exec.groups.nok_text;
let nok_amount = exec.groups.nok_amount.replace(",",".");
let USD = nok_amount * USD_PER_NOK;
text = text.substring(0,exec.index) + USD.toFixed(2) + "$" + text.substring(exec.index + nok_text.length);
ele.nodeValue = text;
console.log(text);
}
}
if(!ele.childNodes || !ele.childNodes.length){
return;
}
for(let i=0;i<ele.childNodes.length;++i){
recur(ele.childNodes[i]);
}
};
recur(document);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment