Last active
August 31, 2015 13:34
-
-
Save nuved/e1946e5ae62d1aa64eba to your computer and use it in GitHub Desktop.
fix autoruning scrip when all DOM page load
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
// ==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(){ | |
setTimeout(function(){ | |
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(); | |
console.log("testttt"); | |
for (var i = 0; i < nPrice.length; ++i) { | |
console.log(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(); | |
}, 3000); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment