Amazon a Dolar Tarjeta

Mostrar precios en dolar tarjeta

目前為 2015-10-15 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          Amazon a Dolar Tarjeta
// @namespace     sharkiller-amazon-dolar-tarjeta
// @description   Mostrar precios en dolar tarjeta
// @version       1.1
// @include       http://www.amazon.com/*
// @include       https://www.amazon.com/*
// @include       http://amazon.com/*
// @include       https://amazon.com/*
// @include       http://www.amazon.co.uk/*
// @include       https://www.amazon.co.uk/*
// @include       http://amazon.co.uk/*
// @include       https://amazon.co.uk/*
// @include       http://www.amazon.ca/*
// @include       https://www.amazon.ca/*
// @include       http://amazon.ca/*
// @include       https://amazon.ca/*
// @include       http://www.amazon.es/*
// @include       https://www.amazon.es/*
// @include       http://www.amazon.fr/*
// @include       https://www.amazon.fr/*
// @require       http://code.jquery.com/jquery-latest.js
// @grant         GM_getValue
// @grant         GM_setValue
// @grant         GM_xmlhttpRequest
// @grant         GM_addStyle
// ==/UserScript==

(function() {
if( typeof jQuery === 'undefined' ){
    return false;
}
GM_addStyle(`
.price-ars {
  color: green;
  display: inline-block;
  margin: 0 8px;
  background: linear-gradient(to bottom,lightblue,white,lightblue);
  border-radius: 8px;
  padding: 0 6px;
}
#sc-buy-box .price-ars {
  font-size: 12px;
  display: block;
  padding: 0;
  margin: 0;
  background: none;
}
div#sc-buy-box {
  margin-bottom: -14px;
}
`);

String.prototype.endsWith = function (pattern) {
	var d = this.length - pattern.length;
	return d >= 0 && this.lastIndexOf(pattern) === d;
};

var amazonCurrencies = ["USD", "GBP", "CAD", "EUR"];
var currencyFrom;

function regularPriceParser(price, currency) {
	return parseFloat(price.replace(/,/g, "."));
}

var currencies = {
	"USD" : {
		symbol: "$",
		priceRegex: /\$\s*([\d,.]+\d)/
	},

	"GBP" : {
		symbol: "£",
		priceRegex: /£\s*([\d,.]+\d)/
	},

	"CAD" : {
		symbol: "CDN$",
		priceRegex: /CDN\$\s*([\d,.]+\d)/
	},

	"EUR" : {
		symbol: "EUR",
		priceRegex: /EUR\s*([\d,.]+\d)/
	}
};

if (document.domain.endsWith("com")) {
	currencyFrom = "USD";
// amazon.co.uk
} else if (document.domain.endsWith("co.uk")) {
	currencyFrom = "GBP";
// amazon.ca
} else if (document.domain.endsWith("ca")) {
	currencyFrom = "CAD";
// amazon.fr
} else if (document.domain.endsWith("fr")) {
	currencyFrom = "EUR";
} else {
	return;
}

var LAST_RUN = "last_run_";
var CURRENCY_RATE = "currency_rate_";

var decimalPlaces = 2;
var prefixCurrencySymbol = true;
var taxPorcentage = 35;

var rounding = Math.pow(10, decimalPlaces);

var rate = GM_getValue(CURRENCY_RATE + currencyFrom);
var lastRun = GM_getValue(LAST_RUN + currencyFrom, "01/01/0001");
var currencyTo = 'ARS';
var todayDate = new Date();
var todayString = todayDate.getDate() + "/" + todayDate.getMonth() + "/" + todayDate.getFullYear();
var currencyToSymbol = 'ARS $';

function fetchCurrencyData(coin, callback) {
	GM_xmlhttpRequest({
		method: "GET",
		url: "http://download.finance.yahoo.com/d/quotes.csv?s=" + coin + currencyTo + "=X&f=l1&e=.csv",
		onload: function(responseDetails) {
			var rate = responseDetails.responseText.replace(/[\r\n]/g, "");
			GM_setValue(CURRENCY_RATE + coin, rate);
			GM_setValue(LAST_RUN + coin, todayString);
			callback();
		},
		onerror: function(responseDetails) {
			alert("Error fetching currency data for " + coin);
		}
	});
}

function appendConversion(price, matched) {
	var originalPrice = regularPriceParser(matched, currencyFrom);

    console.log(originalPrice);
	if (isNaN(originalPrice)) {
		return price;
	}

	var converted = formatCurrency(originalPrice * (rate * (taxPorcentage / 100 + 1)), rounding,
		currencyToSymbol, prefixCurrencySymbol);

	return price + "<div class='price-ars'>" + converted + "</div>";
}

function formatCurrency(num, rounding, symbol, prefix) {
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * rounding + 0.50000000001);
	cents = num % rounding;

	num = Math.floor(num / rounding).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));
	}

	if (prefix) {
		return (symbol + ((sign)?'':'-') + num + '.' + cents);
	} else {
		return (((sign)?'':'-') + num + '.' + cents + symbol);
	}
}
    
function convertCurrency(carrito) {
    if(carrito && jQuery('.a-size-base.a-text-bold:contains(Order summary)').length == 0 && jQuery('.a-size-base.a-text-bold:contains(Récapitulatif de commande)').length == 0){
        return false;
    }else{
        if(jQuery('#sc-buy-box.price-ars-cart').length == 0){
            jQuery('#sc-buy-box').addClass('price-ars-cart');
        }else{
            return false;
        }
    }
    console.log('Cambiando precios', carrito);
    
	var currency = currencies[currencyFrom];

    jQuery('.a-column.a-span3.a-text-right.a-span-last.sc-value').removeClass('a-span3').addClass('a-span4');
    jQuery('.a-column.a-span9.sc-text').removeClass('a-span9').addClass('a-span8');
    jQuery('a.a-popover-trigger.a-declarative:contains(Estimated)').html('Shipping & handling <i class="a-icon a-icon-popover"></i>');

    jQuery('span:not(:has(*))').each(function(){
        var text = jQuery(this).text();
        if( jQuery(this).children().length == 0 && text.indexOf(currency.symbol) != -1 && text.indexOf('ARS') == -1)
            jQuery(this).html( text.replace(currency.priceRegex, appendConversion) );
    });
}

jQuery( document ).ready(function(){
    if (rate === undefined || todayString !== lastRun) {
        fetchCurrencyData(currencyFrom, function() {
            rate = GM_getValue(CURRENCY_RATE + currencyFrom);
            convertCurrency();
        });
    } else {
        console.log(rate);
        convertCurrency();
    }

    jQuery('.a-fixed-right-grid-col.a-col-right').bind("DOMSubtreeModified",function(e){
        convertCurrency(true);
    });

    /*jQuery('#sc-buy-box').after('<button id="precioCarrito" type="button" class="a-button a-button-span12 a-button-primary" style="margin: 10px 0;"><span class="a-button-inner" style="padding: 7px 0;">Actualizar precios del carrito</span></button>');
    jQuery('#precioCarrito').click(function(){
        convertCurrency(true);
    });*/
});
})();