Created
October 31, 2012 13:15
-
-
Save czottmann/3986973 to your computer and use it in GitHub Desktop.
dotjs script, amazon.co.uk: add Euro prices to all price tags
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
/*jshint asi: false, bitwise: false, boss: false, curly: true, debug: false, | |
eqeqeq: true, eqnull: false, evil: false, forin: false, immed: true, | |
laxbreak: true, newcap: true, noarg: true, noempty: false, nonew: false, | |
nomen: false, onevar: true, passfail: false, plusplus: false, regexp: false, | |
undef: true, sub: true, strict: false, white: false, browser: true, | |
devel: true */ | |
/*globals define: false, window: false, jQuery: false */ | |
var scripts = [], | |
scriptTag; | |
// Are there relevant price elements? | |
if ( jQuery(".price, .ourprice, .priceLarge, .s9Price").length ) { | |
// If the current exchange rate is known, enhance the price listing. | |
if ( getRate() ) { | |
updateProductPage(); | |
} | |
// If not, make the relevant functions available to the "outside" page and | |
// trigger the fetch+update process. | |
else { | |
scripts.push( getRate.toString() ); | |
scripts.push( fetchRate.toString() ); | |
scripts.push( setRate.toString() ); | |
scripts.push( updateProductPage.toString() ); | |
scriptTag = document.createElement("script"); | |
scriptTag.innerHTML = scripts.join(";"); | |
document.head.appendChild(scriptTag); | |
fetchRate(); | |
} | |
} | |
// Returns the current numeric GBP-to-EUR exchange rate -- or `0` if there's | |
// no current rate stored. | |
function getRate() { | |
var details = JSON.parse( window.localStorage.getItem("gbp-to-eur-rate") ); | |
if ( details && details.date === ( new Date() ).toDateString() ) { | |
return details.rate; | |
} | |
return 0; | |
} | |
// Requests the current exchange rate from YQL -- the returned script will | |
// automatically trigger `setRate()`. | |
function fetchRate() { | |
jQuery.getScript("http://query.yahooapis.com/v1/public/yql?q=SELECT%20col0%20FROM%20csv%20WHERE%20url%3D%22http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes.csv%3Fs%3DGBPEUR%3DX%26f%3Dl1%26e%3D.csv%22&format=json&callback=setRate"); | |
} | |
// Stores the current exchange rate in localStorage. Called by the YQL call | |
// (JSONP). | |
function setRate(data) { | |
window.localStorage.setItem( | |
"gbp-to-eur-rate", | |
JSON.stringify({ | |
date: ( new Date() ).toDateString(), | |
rate: +data.query.results.row.col0 | |
}) | |
); | |
updateProductPage(); | |
} | |
// Iterate over all price elements and add the German EUR price (incl./excl. | |
// VAT). | |
function updateProductPage() { | |
jQuery.each( jQuery(".price, .ourprice, .priceLarge, .s9Price"), function( i, elem ) { | |
var $elem = jQuery(elem), | |
gbpPrice = +( $elem.text().substr(1) ), | |
dePrice = ( gbpPrice * getRate() ).toFixed(2); | |
if (gbpPrice) { | |
$elem.html( "£" + gbpPrice + " <span style='font-weight: normal; font-size: 85%'>(€" + dePrice + ")</span>" ); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment