Skip to content

Instantly share code, notes, and snippets.

@killerbytes
Last active July 27, 2017 01:42
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 killerbytes/ff1ceee6e5ef0119b1b6f65ee87c51c9 to your computer and use it in GitHub Desktop.
Save killerbytes/ff1ceee6e5ef0119b1b6f65ee87c51c9 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name AMAZON currency converter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.amazon.com/*
// @match https://www.amazon.co.uk/*
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
$(function() {
'use strict';
let host = window.location.host;
let base;
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'PHP',
minimumFractionDigits: 2,
});
if(host === 'www.amazon.co.uk'){
base = 'GBP';
}else{
base = 'USD';
}
getRate(base);
function getRate(base){
function fetchRate(){
fetch(`//api.fixer.io/latest?base=${base}&symbols=PHP`).then(function(response){
response.json().then(res=>{
let amazon = {};
amazon[base] = {
date: (new Date()).toDateString(),
rate: res.rates.PHP
};
localStorage.setItem('AMAZON', JSON.stringify(amazon));
convertPrices(res.rates.PHP, base);
});
});
}
var rate = JSON.parse(localStorage.getItem('AMAZON')) || {};
if(!rate[base]) {
fetchRate();
}else{
if(rate[base].date === (new Date()).toDateString()){
convertPrices(rate[base].rate, base);
}else{
fetchRate();
}
}
}
function convertPrices(rate, base){
let prices;
switch(base){
case 'GBP':
prices = $('.a-color-price');
break;
default:
prices = $('.a-link-normal .a-color-base, .a-color-price');
break;
}
prices.each(function(){
switch(base){
case 'GBP':
let price = parseFloat(this.innerText.trim().substr(1)) * rate;
$(this).text(formatter.format(price));
break;
default:
if($(this).attr('aria-label')){
let price = parseFloat($(this).attr('aria-label').substr(1)) * rate;
$(this).text(formatter.format(price));
}else{
let price = parseFloat(this.innerText.trim().substr(1)) * rate;
if(price) $(this).text(formatter.format(price));
}
break;
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment