Search code examples
javascripthtmlgoogle-chrome-extension

Is it possible to replace the existing price on a website to a converted one with JavaScript?


Past few days I have been trying to write some code for my Browser extension, and I have been stuck on converting the prices on the website.

I got so far to realise that I can technically get an array of prices trough document.querySelectionAll('.price__blocks'), but it doesn't let my code run that worked for me in a playground, because it gives out an error: *VM8032:2 Uncaught TypeError: nodes[i].replace is not a function* at :2:38

Playground Code

vals = ["80.00$", "50.00$", "70.00$", "69.99$"];
for (var i = 0; i < 4; ++i){
var priceNum = parseFloat(vals[i].replace(/$/g, ""))
priceNum = priceNum * 0.000020 + " BTC"
console.log(priceNum)
}

vals = ["80.00$", "50.00$", "70.00$", "69.99$"];


for (var i = 0; i < 4; ++i){
  var priceNum = parseFloat(vals[i].replace(/$/g, ""))
  priceNum = priceNum * 0.000020 + " BTC"
  console.log(priceNum)
}

Actual Code

var nodes = document.querySelectorAll('.price__block')
for (var i = 0; i < nodes.length; ++i){
var priceNum = parseFloat(nodes[i].replace(/€/g, ""))
priceNum = priceNum * 0.000020 + " BTC"
nodes[i].innerHTML = priceNum;
}

var nodes = document.querySelectorAll('.price__block')
for (var i = 0; i < nodes.length; ++i){
    var priceNum = parseFloat(nodes[i].replace(/€/g, ""))
    priceNum = priceNum * 0.000020 + " BTC"
    nodes[i].innerHTML = priceNum;
  }

I'll attach screenshots for better understanding of my issue images of VisualStudio Code and Playground


Solution

  • Thanks to ProfesorAbronsius, I was able to get a solution!

    var nodes = document.querySelectorAll('.price__block')
    
    for (var i = 0; i < nodes.length; ++i){
        var priceNum = parseFloat(nodes[i].textContent.replace(/€/g, ""))
        priceNum = priceNum * 0.000020 + " BTC"
        nodes[i].innerHTML = priceNum;
      }

    Also ran into a new issue with this solution, but a minor one, it keeps multiplying the price every time I switch tabs, but I'm sure it's a quick solution!

    Thank you!