// ==UserScript== // @name eBooks Assistant // @namespace https://github.com/caspartse/eBooksAssistant // @version 0.2.3.1 // @description eBooks Assistant for douban.com // @author Caspar Tse // @match https://book.douban.com/subject/* // @require https://cdn.bootcdn.net/ajax/libs/jquery/1.10.0/jquery.min.js // @connect douban.com // @connect amazon.cn // @connect 106.52.138.60 // @grant GM_xmlhttpRequest // @downloadURL none // ==/UserScript== (function() { function queryAmazon(title, isbn) { GM_xmlhttpRequest({ method: "GET", url: "https://www.amazon.cn/s?__mk_zh_CN=亚马逊网站&i=digital-text&k=" + isbn + "&ref=nb_sb_noss&url=search-alias%3Ddigital-text", headers: { 'User-agent': window.navigator.userAgent, }, onload: function(responseDetail) { var doc = responseDetail.responseText; var errorFlag = /("totalResultCount"\:0)|(在Kindle商店中未找到)/.exec(doc); if (!errorFlag) { var regexAmazonUrl = /href="(\S+keywords=\d+[^"]+)"/gi; var amazonUrl = "https://www.amazon.cn" + regexAmazonUrl.exec(doc)[1]; amazonUrl = amazonUrl.replace(isbn, title); var partnerTemplate = ''; if ($(".online-partner").length) { partnerTemplate = '
Kindle
' $(".online-partner .online-type:nth-child(1)").append(partnerTemplate.replace("{templateUrl}", amazonUrl)); } else { partnerTemplate = '
在线试读:
'; $("#link-report").after(partnerTemplate.replace("{templateUrl}", amazonUrl)); } var regexAmazonPrice = /¥([0-9\.]+)<\/span>/gi; var amazonPrice = regexAmazonPrice.exec(doc)[1]; console.log(amazonPrice); var buyItemTemplate = '
  • ' if (amazonPrice == 0.00 ) { buyItemTemplate = '
  • KU可免费借阅
  • ' regexAmazonPrice = /或者¥([0-9\.]+)购买<\/span>/gi; amazonPrice = regexAmazonPrice.exec(doc)[1]; } buyItemTemplate = buyItemTemplate.replaceAll("{templateUrl}", amazonUrl); buyItemTemplate = buyItemTemplate.replace("{templatePrice}", amazonPrice); $("#buyinfo ul:nth-child(2)").prepend(buyItemTemplate); } return; } }); return; } function queryTuring(isbn) { GM_xmlhttpRequest({ method: "GET", url: "http://106.52.138.60:8081/turingebooks?isbn=" + isbn, headers: { 'User-agent': window.navigator.userAgent, }, onload: function(responseDetail) { var result = JSON.parse(responseDetail.responseText); console.log(result); if (result.errmsg == '') { var turingUrl = result.data.url; var turingPrice = result.data.price; var partnerTemplate = ''; if ($(".online-partner").length) { partnerTemplate = '' $(".online-partner .online-type:nth-child(1)").append(partnerTemplate.replace("{templateUrl}", turingUrl)); } else { partnerTemplate = '
    在线试读:
    '; $("#link-report").after(partnerTemplate.replace("{templateUrl}", turingUrl)); } var buyItemTemplate = '
  • ' buyItemTemplate = buyItemTemplate.replaceAll("{templateUrl}", turingUrl); buyItemTemplate = buyItemTemplate.replace("{templatePrice}", turingPrice); $("#buyinfo ul:nth-child(2)").prepend(buyItemTemplate); } return; } }); return; } var regexIsbn = /ISBN:<\/span>\s*(\d+)\s*/g; var isbn = regexIsbn.exec($("#info").html())[1]; console.log(isbn); var title = $("#wrapper h1:nth-child(2) span:nth-child(1)").text().trim(); console.log(title); queryAmazon(title, isbn); queryTuring(isbn); return; })();