JD overseas price

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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