// ==UserScript== // @name 获取优书网评分 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 知轩藏书书籍详情页添加优书网评分和直链 // @author tianxin // @match *://www.zxcs8.com/post/* // @grant GM.xmlHttpRequest // @connect www.yousuu.com // @downloadURL none // ==/UserScript== function getResult(url, callback) { GM.xmlHttpRequest({ method: 'GET', url: url, onload: function(response) { if (response.status >= 200 && response.status < 400){ callback(response.responseText);} else{ console.log( 'Error getting ' + url + ' (' + this.status + ' ' + this.statusText + '): ' + this.responseText); } }, onerror: function(response) { console.log('Error during GM.xmlHttpRequest to ' + url + ': ' + response.statusText); } }); } (function() { 'use strict'; var site = location.hostname; if(site == 'www.zxcs8.com'){ var bookText = document.getElementsByTagName('h1')[0].innerHTML; var bookName = bookText.match('《(.*?)》')[1]; var bookAuthor = bookText.match(':(.*?)$')[1]; getResult('http://www.yousuu.com/search/' + bookName + '?type=book', function(response){ var rate = 0; var rateNum = 0; var rateText = '暂未查询到优书网评分'; var rateItem = '

'+ rateText +'

'; var html = document.createElement( 'html' ); html.innerHTML = response; var matchBook = html.getElementsByClassName('col-lg-4 col-md-6 col-xs-12')[0]; var matchTitle = matchBook.getElementsByClassName('title')[0]; var matchName = matchTitle.innerText; //Object.values(matchTag).forEach(function(item){ // matchName += item.innerText; //}) var matchAbstract = matchBook.getElementsByClassName('abstract')[0].innerHTML.split('
'); var matchAuthor = matchAbstract[0].split(':')[1].replace(/<\/?[^>]*>/g,'').trim(); if(matchAuthor == bookAuthor && matchName == bookName){ rate = matchBook.getElementsByClassName('num2star')[0].innerHTML; rateNum = matchBook.getElementsByClassName('rating')[0].innerText.replace(/[^0-9]/ig,""); } // console.log(matchAuthor + ':' + bookAuthor + ';' + matchName + ':' + bookName); var obj = document.getElementsByClassName('yinyong')[0]; if(rate != 0 || rateNum != 0){ var bookLink = 'http://www.yousuu.com' + matchTitle.getElementsByTagName('a')[0].pathname; rateItem = '

' + '优书网' + '' + '评分: ' + rate + '

' + '

评分人数: ' + rateNum + '

'; } obj.insertAdjacentHTML('beforebegin',rateItem); }) } })();