JD overseas price

Show the price of overseas.jd.com in jd.com

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        JD overseas price
// @description Show the price of overseas.jd.com in jd.com
// @version     1.0
// @author      Damon Zhou
// @include     http://*.jd.com/*
// @grant       GM_xmlhttpRequest
// @require     http://code.jquery.com/jquery-2.1.4.min.js
// @namespace https://greasyfork.org/users/13165
// ==/UserScript==

var productPage = {FN_SetAllPrice: function(price) {
	return parseFloat(price.USD.jdPrice.replace(/[^\d.]/g, ""));
}};

var currency;
GM_xmlhttpRequest({
	synchronous: true,
	method: "GET",
	url: "http://api.fixer.io/latest?base=USD&symbols=CNY",
	onload: function(response) {
		currency = JSON.parse(response.responseText).rates.CNY;
	}
});

$("li.gl-item, li[sku], li.item-book").each(function() {
	var $oldPriceDiv = $(this).find("div.p-price");
	var $strong = $oldPriceDiv.find("strong");
	var $newPriceDiv = $("<div></div>").attr("class", "p-price");
	$oldPriceDiv.after($newPriceDiv);

	var price = parseFloat($strong.text().replace(/[^\d.]/g, ""));
	var id = $(this).children("div").attr("data-sku") || $strong.attr("class").replace("J_", "");

	GM_xmlhttpRequest({
		method: "GET",
		url: "http://overseas.jd.com/GetCurrency/" + id + "?callback=productPage.FN_SetAllPrice",
		onload: function(response) {
			var usd = eval(response.responseText);
			if (usd) {
				var cny = (usd * currency).toFixed(2);
				var diff = Math.abs(cny - price).toFixed(2);
				var diffpp = ((diff / price) * 100).toFixed();
				var symbol = cny >= price ? "↑" : "↓";
				
				$newPriceDiv.append("<span>¥" + cny + "</span>").append("<span style=\"margin-left: 20px; margin-right: 20px;\">¥" + diff + symbol + "</span>").append("<span>" + diffpp + "%" + symbol + "</span>");
			} else {
				$newPriceDiv.append("<span>No Price</span>");
			}
		}
	});
});