To increase usability on a website I want to change the following with Greasemonkey (JavaScript) :
data-bind="text: 'Price: ' + db.totalpr().toFixed(2) + ' GBP'"`
to
data-bind="text: 'Price: ' + db.totalpr().toFixed(2)*current_exchange_rate + ' USD'"`
Had tried
document.body.innerHTML = document.body.innerHTML.replace(text_to_find, text_to_replace)
but a page loses events and no data is being loaded: "Price" loads nothing and stays empty.
Then I've found this: Replace text in a website
function replaceTextOnPage(from, to){
getAllTextNodes().forEach(function(node){
node.nodeValue = node.nodeValue.replace(new RegExp(quote(from), 'g'), to);
});
function getAllTextNodes(){
var result = [];
(function scanSubTree(node){
if(node.childNodes.length)
for(var i = 0; i < node.childNodes.length; i++)
scanSubTree(node.childNodes[i]);
else if(node.nodeType == Node.TEXT_NODE)
result.push(node);
})(document);
return result;
}
function quote(str){
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}
}
But, unfortunately, it's not working in my case: it can replace "Price" to any text I want but not the
db.totalpr().toFixed(2)
to
"db.totalpr().toFixed(2)*current_exchange_rate"
Any ideas how to make it work without losing events?
Update:
<div class="row">
<div class="col-md-5">
<h5 data-bind="text: 'Price: ' + db.totalpr().toFixed(2) + ' GBP'" style="margin-left:7px"></h5>
</div>
</div>