// ==UserScript==
// @name 京东购物车图书豆瓣评分
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 京东购物车图书显示豆瓣评分
// @author 罗辣耳朵
// @include http*://cart.jd.com/*
// @include http*://item.jd.com/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
var isbnReg = new RegExp('<li title="([-0-9]+)">ISBN:\\1</li>', "gi");
var personsReg = new RegExp('([0-9]+)人评价', "gi");
var $items = $(".item-form");
var bookRatedURL = "http://111.229.55.106/isbn/";
var rankInfoCache = {};
function addDoubanRankInfo(addedItem, data) {
var $star = $("<div class='plus' style='color:#5FD9CD'></div>");
try {
var persons = parseInt(data.persons);
var star = parseFloat(data.star);
if (star >= 7.5 && persons > 999) {
var $itemParent = addedItem.parent();
var background = "#869B74";
if (star >= 9.0){
background = "#EACF02";
}
else if (star >= 8.5) {
background = "#6C890B";
}
else if (star >= 8.0) {
background = "#ABC327";
}
$itemParent.css({"background":background});
}
if (data.star == "0.0") {
$star.text("评价人数不足,暂无评分");
}
else {
$star.text("评分:" + data.star + "(" + data.persons + "人评价)");
}
addedItem.after($star);
}
catch (e) {
$star.text(data.star + data.persons);
addedItem.after($star);
console.log(e);
}
var $link = $("<a href='" + data.url + "' style='color:#FFB5A1; text-decoration:underline' target='_blank'>直达豆瓣</a>");
$star.append($link);
}
var hostname = location.hostname;
if (hostname.startsWith("cart")) {
// 购物车处理流程
$.each($items, (i, e) => {
var $item = $(e);
var $itemInfo = $item.find(".goods-item .p-name a");
var href = "https:" + $itemInfo.attr("href");
GM_xmlhttpRequest({
method: 'GET',
url: href,
overrideMimeType: "text/xml",
synchronous: true,
headers: {
'User-agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)',
'Accept': 'text/html, application/xhtml+xml, */*',
},
onload: (data) => {
if (data && data.status == 200) {
var content = data.responseText;
if (isbnReg.test(content)) {
var isbn = RegExp.$1;
GM_xmlhttpRequest({
method: 'GET',
url: bookRatedURL + isbn,
responseType: "json",
synchronous: true,
headers: {
'User-agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)',
'Accept': 'text/html, application/xhtml+xml, */*',
},
onload: (data) => {
if (data.status == 200) {
var result = data.response;
var persons = parseInt(result.persons);
var rankInfo = {
url: result.url,
star: result.star,
originalPersons: result.persons,
persons: persons
};
var skuid = $item.parent().attr("skuid");
rankInfoCache[skuid] = rankInfo;
addDoubanRankInfo($item, rankInfo);
}
personsReg.lastIndex = 0;
},
onerror:function () {
console.log("get book rated error");
}
});
isbnReg.lastIndex = 0;
}
else {
console.log(href + " not found isbn");
}
}
},
onerror:function () {
console.log("get book detail html error");
}
});
});
}
else {
// 详情页处理流程
var isbn = "";
var parameters = $(".p-parameter ul li");
for (var i=0; i<parameters.length; i++) {
var $parameter = $(parameters[i]);
if ($parameter.text().startsWith("ISBN")) {
isbn = $parameter.text();
isbn = isbn.substring("ISBN:".length);
break;
}
}
if (isbn) {
GM_xmlhttpRequest({
method: 'GET',
url: bookRatedURL + isbn,
responseType: "json",
synchronous: true,
headers: {
'User-agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)',
'Accept': 'text/html, application/xhtml+xml, */*',
},
onload: (data) => {
if (data.status == 200) {
var result = data.response;
var persons = parseInt(result.persons);
var rankInfo = {
url: result.url,
star: result.star,
originalPersons: result.persons,
persons: persons
};
var $addItem = $(".sku-name");
var $star = $("<div class='plus' style='color:#5FD9CD'></div>");
try {
var personNum = parseInt(rankInfo.persons);
var star = parseFloat(rankInfo.star);
if (star >= 7.5 && personNum > 999) {
var background = "#869B74";
if (star >= 9.0){
background = "#EACF02";
}
else if (star >= 8.5) {
background = "#6C890B";
}
else if (star >= 8.0) {
background = "#ABC327";
}
$star.css({"background":background});
}
if (rankInfo.star == "0.0") {
$star.text("评价人数不足,暂无评分");
}
else {
$star.text("评分:" + rankInfo.star + "(" + rankInfo.persons + "人评价)");
}
$addItem.append($star);
}
catch (e) {
$star.text(rankInfo.star + rankInfo.persons);
$addItem.append($star);
console.log(e);
}
var $link = $("<a href='" + rankInfo.url + "' style='color:#FFB5A1; text-decoration:underline' target='_blank'>直达豆瓣</a>");
$star.append($link);
}
},
onerror:function () {
console.log("get book rated error");
}
});
}
}
$(".jdcheckbox").bind("click", function(e) {
var $items = $(".item-item");
$.each($items, (i, e) => {
var $item = $(e);
var skuid = $item.attr("skuid");
if (rankInfoCache[skuid]) {
setInterval(() => {
addDoubanRankInfo($item.children(".item-form"), rankInfoCache[skuid]);
},2000);
}
});
});
})();