// ==UserScript==
// @name 优书网 <=> [知轩藏书/早安电子书]
// @namespace http://tampermonkey.net/
// @description [知轩藏书/早安电子书]添加优书网评分和直链,优书网书籍详情页添加[知轩藏书/早安电子书]下载链接
// @author tianxin
// @match *://zxcs8.com/sort/*
// @match *://zxcs8.com/post/*
// @match *://www.zxcs8.com/sort/*
// @match *://www.zxcs8.com/post/*
// @match *://www.yousuu.com/book/*
// @match *://www.zadzs.com/txt/*
// @grant GM.xmlHttpRequest
// @connect www.yousuu.com
// @connect www.zxcs8.com
// @connect www.zadzs.com
// @version 0.4
// ==/UserScript==
let sourceConfig = {
'zxcs8.post':{
bookName:function(item){
return document.querySelector('h1').innerText.match('《(.*?)》')[1];
},
bookAuthor:function(item){
return document.querySelector('h1').innerText.split(':').pop();
},
maxNum:5,
rateItem:function(rate,rateNum,bookLink){
return '<p class="yinyong"><span style="font-size:14px;color:#FF0000;font-weight:bold"><a href = "' + bookLink + '" target="_blank">优书网</a>评分: ' + rate + '</span><p><p class="yinyong"><span style="font-size:14px;color:#FF0000;font-weight:bold">评分人数: ' + rateNum + '</span><p>';
},
anchorObj:function(item){
let obj = document.querySelector('.yinyong');
return obj === undefined ? document.querySelector('.pagefujian') : obj;
},
anchorPos:'beforebegin',
},
'zxcs8.sort':{
bookName:function(item){
return item.firstElementChild.innerText.match('《(.*?)》')[1];
},
bookAuthor:function(item){
return item.firstElementChild.innerText.split(':').pop();
},
maxNum:5,
rateItem:function(rate,rateNum,bookLink){
return '<a href= "' + bookLink + '" target = "_blank"> 评分:' + rate + ' 人数:' + rateNum + '</a>'
},
anchorObj:function(item){
return item.lastElementChild.querySelector('div');
},
anchorPos:'beforebegin',
},
'zadzs':{
bookName:function(item){
return document.querySelector('h3[title]').title;
},
bookAuthor:function(item){
return document.querySelector('h3[title]>span>a').innerText;
},
maxNum:5,
rateItem:function(rate,rateNum,bookLink){
return '<tr><td width="42px"><a href="'+ bookLink + '" target = "blank">评分:</a></td><td>' + rate + '</td></tr><tr><td width="42px">人数:</td><td>' + rateNum + '</td></tr>';
},
anchorObj:function(item){
return document.querySelector('.m-bookstatus>table>tbody');
},
anchorPos:"afterbegin",
}
};
let sourceHandle = {
'zxcs8.post':function(site,callback){
let option = {site:site,item:''};
return callback(option,sourceConfig);
},
'zxcs8.sort':function(site,callback){
let bookList = Array.prototype.slice.call(document.querySelectorAll('#plist'));
bookList.forEach(function(item){
let option = {site:site,item:item};
callback(option,sourceConfig);
});
},
'zadzs':function(site,callback){
let option = {site:site,item:''};
return callback(option,sourceConfig);
},
};
let sourceRoute = {
'www.zxcs8.com':function(){
let tag = location.pathname.split('/')[1];
if(['sort','post'].includes(tag)){
return 'zxcs8.' + tag;
}
},
'zxcs8.com':function(){
let tag = location.pathname.split('/')[1];
if(['sort','post'].includes(tag)){
return 'zxcs8.' + tag;
}
},
'www.zadzs.com':function(){
return 'zadzs';
}
};
let downloadConfig = {
"zxcs8":{
'siteName':'知轩藏书',
'searchUrl':function(args){
return 'http://www.zxcs8.com/index.php?keyword=' + args.bookName;
},
'bookList':function(item){
return Array.prototype.slice.call(item.getElementsByTagName('dl'));
},
'bookName':function(item){
return item.children["0"].innerText.match('《(.*?)》')[1];
},
'bookAuthor':function(item){
return item.children["0"].innerText.split(':').pop();
},
'bookLink':function(item){
return item.children["0"].getElementsByTagName('a')[0].href;
},
'downloadLink':function(item){
return item.querySelector('.down_2>a').href;
},
},
"zadzs":{
'siteName':'早安电子书',
'searchUrl':function(args){
return 'http://www.zadzs.com/plus/search.php?kwtype=0&q=' + args.bookName;
},
'bookList':function(item){
return Array.prototype.slice.call(item.getElementsByClassName('searchItem'));
},
'bookName':function(item){
return item.querySelector('.book>h5>a').innerText;
},
'bookAuthor':function(item){
return item.querySelector('.book>.price').innerText.split(':').pop();
},
'bookLink':function(item){
return 'http://www.zadzs.com' + item.querySelector('.book>.cover').pathname;
},
'downloadLink':function(item){
return 'http://www.zadzs.com' + item.querySelector('.book>cover').href;
},
},
};
let downloadHandle = {
'zxcs8':function(options,callback){
let siteConfig = downloadConfig[options.site];
getResult(options.bookLink,function(response){
let html = document.createElement( 'html' );
html.innerHTML = response;
let downloadLink = siteConfig.downloadLink(html);
callback({downloadLink:downloadLink,siteName:siteConfig.siteName})
});
},
'zadzs':function(options,callback){
let args = {downloadLink:options.bookLink,siteName:downloadConfig[options.site].siteName};
callback(args);
}
};
let downloadRoute = {
'www.yousuu.com':function(){
return 'www.yousuu.com';
}
};
function removeElement(element){
if(element){
let parent = element.parentNode;
if(parent){
parent.removeChild(element);
}
}
}
function addDownLoadLink(options,config){
let siteConfig = config[options.site];
let youBookName = document.getElementsByClassName('col-sm-7')[0].children[0].innerText;
let youBookAuthor = document.getElementsByClassName('list-unstyled list-sm')["0"].firstChild.children["0"].innerText;
getResult(siteConfig.searchUrl({bookName:youBookName}),function(response){
let html = document.createElement( 'html' );
html.innerHTML = response;
let bookList = siteConfig.bookList(html);
if(bookList.length > 0){
bookList.some(function(item){
let bookLink = siteConfig.bookLink(item);
let matchBookName = siteConfig.bookName(item);
let matchBookAuthor = siteConfig.bookAuthor(item);
console.log(options.site + ':' + matchBookName + ':' + youBookName + ';' + matchBookAuthor + ':' + youBookAuthor );
if(matchBookName === youBookName && matchBookAuthor === youBookAuthor){
downloadHandle[options.site]({site:options.site,bookLink:bookLink},insertDownLink);
}
});
}
})
};
function insertDownLink(options){
let obj = document.querySelector('#download-items');
let item = '<li><a href="'+ options.downloadLink +'" target="_blank">'+ options.siteName + '</a></li>';
obj.insertAdjacentHTML('beforeend',item);
if(obj.parentElement.style.display === 'none'){
obj.parentElement.setAttribute('style', 'display:run-in');
}
}
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 getRate(args,callback)
{
let bookName = args.bookName;
let bookAuthor = args.bookAuthor;
let maxNum = (args.maxNum === undefined) ? 1 : args.maxNum;
getResult('http://www.yousuu.com/search/' + bookName + '?type=book', function(response){
let rate = 0;
let rateNum = 0;
let bookLink = '';
let html = document.createElement( 'html' );
html.innerHTML = response;
let matchBook = Array.slice(Array.prototype.slice.call(html.getElementsByClassName('col-lg-4 col-md-6 col-xs-12')),0,maxNum);
matchBook.some(function(item){
let matchTitle = item.querySelector('.title');
let matchName = matchTitle.innerText;
let matchAbstract = item.querySelector('.abstract').innerHTML.split('<br>');
let matchAuthor = matchAbstract[0].split(':')[1].replace(/<\/?[^>]*>/g,'').trim();
if(matchAuthor === bookAuthor && matchName === bookName){
rate = item.querySelector('.num2star').innerHTML;
rateNum = item.querySelector('.rating').innerText.replace(/[^0-9]/ig,"");
bookLink = 'http://www.yousuu.com' + matchTitle.querySelector('a').pathname;
return true;
}
});
callback({rate:rate,rateNum:rateNum,bookLink:bookLink});
});
}
function insertRate(options,config){
let siteConfig = config[options.site];
let args = {bookName:siteConfig.bookName(options.item),bookAuthor:siteConfig.bookAuthor(options.item),maxNum:siteConfig.maxNum};
getRate(args,function(data){
let rate = data.rate;
let rateNum = data.rateNum;
let bookLink = data.bookLink;
if(rate !== 0 || rateNum !== 0){
let rateItem = siteConfig.rateItem(rate,rateNum,bookLink);
siteConfig.anchorObj(options.item).insertAdjacentHTML(siteConfig.anchorPos,rateItem);
}
});
}
(function() {
'use strict';
if(Object.keys(sourceRoute).includes(location.hostname)){
let site = sourceRoute[location.hostname]();
sourceHandle[site](site,insertRate);
}
if(Object.keys(downloadRoute).includes(location.hostname)){
let sites = Object.keys(downloadConfig);
let select = '<div class="btn-group" id="download-list" style="display:none"><button type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="btn btn-warning dropdown-toggle"><span>去下载</span> <span class="caret"></span></button><ul class="dropdown-menu" id="download-items"></ul></div>';
let obj = document.getElementsByClassName('sokk-book-button-groups')[0];
obj.insertAdjacentHTML('beforeend',select);
sites.forEach(function(item){
let options = {site:item};
addDownLoadLink(options,downloadConfig);
});
}
})();