Skip to content

Instantly share code, notes, and snippets.

@Guidhouse
Last active August 9, 2017 10:12
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 Guidhouse/e973a5ebfd55447ad2832c99426be3ba to your computer and use it in GitHub Desktop.
Save Guidhouse/e973a5ebfd55447ad2832c99426be3ba to your computer and use it in GitHub Desktop.
rebate = $('.save-wrap .product-price-save');
offer = $('.product-image .product-price-save');
price = $('.price')
findRebate = (s) => {el = $(s).find(rebate)[0]; return( !!el)? parseInt(el.innerText.substr(5, 3)) : NaN };
findPrice = (s) => {el = $(s).find(price)[0]; return( !!el)? parseInt(el.innerText) : NaN };
findPercentage = (s) => (findRebate(s)/(findPrice(s) + findRebate(s)) * 100);
setText = (s) => {el = $(s).find(offer)[0]; if (!!el) { el.innerText = findPercentage(s).toPrecision(4) + " %" }};
rebateSort = (a, b) => {return findRebate(b) - findRebate(a) };
priceSort = (a, b) => {return findPrice(a) - findPrice(b)};
percentageSort = (a, b) => {return findPercentage(b) - findPercentage(a)};
pp = document.getElementsByClassName('result-container')[0];
[...pp.getElementsByTagName('hr')].forEach((p) => pp.removeChild(p));
products = [...document.getElementsByClassName('product')];
products.sort(percentageSort);
products.forEach((i, el) => { setText(i); pp.appendChild(i);});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment