JD overseas price

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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>");
			}
		}
	});
});