Skip to content

Instantly share code, notes, and snippets.

@nuved
Created August 24, 2015 10:11
Show Gist options
  • Save nuved/7fe8d794dc623049f2af to your computer and use it in GitHub Desktop.
Save nuved/7fe8d794dc623049f2af to your computer and use it in GitHub Desktop.
// ==UserScript==
// @encoding UTF-8
// @name Digikala
// @description Replaces
// @namespace http://novid.ir
// @include http://www.digikala.com/*
// @include http://digikala.com/*
// @version 1
// ==/UserScript==
function newPrice() {
var newPrice = [];
var price = document.getElementsByClassName('newprice');
for (var i = 0; i < price.length; ++i) {
newPrice.push(parseFloat(price[i].innerHTML.replace(/[^\d\.]+/, '')));
}
return newPrice;
}
function oldPrice() {
var oldPrice = [];
var price = document.getElementsByClassName('oldprice');
for (var i = 0; i < price.length; ++i) {
oldPrice.push(parseFloat(price[i].innerHTML.replace(/[^\d\.]+/, '')));
}
return oldPrice;
}
function toPercent(oldPrice, newPrice) {
var oldPrice = typeof oldPrice !== 'undefined' ? oldPrice : 0;
if (oldPrice === 0) {
return 0;
} else {
var percent = ((newPrice * 100) / oldPrice);
return (100 - percent);
}
}
function totalPercent() {
var tPercent = [];
var nPrice = newPrice();
var oPrice = oldPrice();
for (var i = 0; i < nPrice.length; ++i) {
tPercent.push(toPercent(oPrice[i], nPrice[i]));
}
return tPercent;
}
function appendPercent() {
var aPercent = [];
var aPercent = totalPercent();
$(".newprice").append("<p class=\"appendprice\">درصد تخفیف</p>");
$('.appendprice').each(function(i) {
$(this).attr('id', 'appendprice' + (i));
$('#appendprice' + (i)).html(parseInt(aPercent[i]) + " درصد تخفیف");
});
}
appendPercent();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment