Last active
September 5, 2017 14:25
-
-
Save chancesmith/45da04f92950a01f3fc82430691f0474 to your computer and use it in GitHub Desktop.
ToysRUs price drop percentage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test page: https://www.toysrus.com/search?q=dyl01sep17%20creator | |
// just copy this code in the console on a page | |
// that has all items with a price drop | |
var items = document.getElementsByClassName('product-item') | |
//var prices = items.getElementsByClassName('product-price') | |
var collection = [] | |
for (var i=0; i<items.length; i++) { | |
// get link of item | |
var productLink = items[i].href | |
var productName = items[i].getElementsByClassName('product-item__product-title')[0].textContent | |
// fetch prices | |
var orgPrice = items[i].getElementsByClassName('tru-price__crossed-out')[0].textContent | |
var salePrice = items[i].getElementsByClassName('tru-price__red')[0].textContent | |
// prep prices | |
var orgPriceClean = orgPrice.replace("$", "") | |
var salePriceClean = salePrice.replace("$", "") | |
// compare .tru-price__crossed-out && .tru-price__red | |
var diffPercentage = parseFloat(((Number(salePriceClean) / Number(orgPriceClean))-1)*100).toFixed(0) | |
console.log("Price: "+diffPercentage) | |
// add % to .product-price | |
var newEl = document.createElement('span'); | |
newEl.className = "tru-price product-price__label product-price__label__sale tru-price__red"; | |
newEl.style = "color: pink"; | |
newEl.innerHTML = diffPercentage+"%"; | |
items[i].getElementsByClassName('product-price')[0].appendChild(newEl); | |
// store collection entry | |
var currentEntry = { | |
name: productName, | |
link: productLink, | |
price: { | |
original: orgPriceClean, | |
sale: salePriceClean, | |
diff: diffPercentage | |
} | |
} | |
collection.push(currentEntry) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment