I am trying to create a Chrome Extension that will replace the price of products on Amazon with the number of hours that would be needed to work at your given hourly rate to afford to buy the item.
My code currently looks as follows:
const hourlyWage = 15;
const priceElements = document.querySelectorAll("*:not(script):not(style)"); // select all elements on the page except for scripts and styles
//console.log(priceElements); // log the value of priceElements to the console
priceElements.forEach(function(element) {
const priceMatch = element.innerText ? element.innerText.match(/\$\d+\.\d{2}/) : null; // use a regex to find a string that starts with a $ and has two decimal places
console.log(priceMatch)
if (priceMatch !== null) {
const priceString = priceMatch[0].replace(/[^0-9.]/g, "");
const price = parseFloat(priceString);
const hours = price / hourlyWage;
const roundedHours = Math.round(hours * 100) / 100;
element.innerText = `${roundedHours} hours of work`;
}
});
The goal was for this code to execute and replace the price on the Amazon with "2 hours of work" rather than $30.
Now, upon going to an amazon product page, the entire page is replaced with "2 hours of work" rather than just the prices on the page.