Last active
September 14, 2016 18:36
-
-
Save gerswin/bad59e82d57980c1f6ef0c9e4b6c1b59 to your computer and use it in GitHub Desktop.
bolivares y usd en exito.com
This file contains hidden or 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== | |
// @name Exito Price | |
// @namespace http://tampermonkey.net/ | |
// @version 0.4 | |
// @description bolivares y dolares en exito.com | |
// @author gerswin | |
// @require http://code.jquery.com/jquery-latest.js | |
// @match http://www.exito.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function formatCurrency(num) { | |
num = num.toString().replace(/\$|\,/g, ''); | |
if (isNaN(num)) num = "0"; | |
var sign = (num == (num = Math.abs(num))); | |
num = Math.floor(num * 100 + 0.50000000001); | |
var cents = num % 100; | |
num = Math.floor(num / 100).toString(); | |
if (cents < 10) cents = "0" + cents; | |
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) | |
num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3)); | |
return (((sign) ? '' : '-') + num + ',' + cents); | |
} | |
if (localStorage.getItem("lastcheck") === null) { | |
localStorage["lastcheck"] = Math.floor(Date.now() / 1000); | |
$.getJSON( "https://s3.amazonaws.com/dolartoday/data.json", function( data ) { | |
localStorage["usdata"] = JSON.stringify(data); | |
}); | |
} | |
var lastCheck = localStorage.getItem("lastcheck"); | |
var valores; | |
function setPrice(valores){ | |
$( ".price" ).each(function( index ) { | |
var precio = $( this ).text().match(/\d+/g).join([]); | |
var bolivar =formatCurrency(precio / valores.COL.compra); | |
var usd= formatCurrency(precio / valores.USDCOL.ratetrm); | |
$(this).parent().append('<h6 class="price">'+bolivar+' BsF</h6>'); | |
$(this).parent().append('<h6 class="price">'+usd+' USD</h6>'); | |
}); | |
$(".otherMedia").find("span").each(function( index ) { | |
var precio = $( this ).text().match(/\d+/g).join([]); | |
var bolivar =formatCurrency(precio / valores.COL.compra); | |
var usd= formatCurrency(precio / valores.USDCOL.ratetrm); | |
$(this).parent().parent().append('<h6 class="price">'+bolivar+' BsF</h6>'); | |
$(this).parent().parent().append('<h6 class="price">'+usd+' USD</h6>'); | |
}); | |
} | |
if (Math.floor(Date.now() / 1000) - lastCheck > 3600) | |
{ | |
$.getJSON( "https://s3.amazonaws.com/dolartoday/data.json", function( data ) { | |
setPrice(data); | |
localStorage["usdata"] = JSON.stringify(data); | |
}); | |
}else{ | |
setPrice(JSON.parse(localStorage.getItem("usdata"))); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment