Greasy Fork is available in English.

优书网 <=> 知轩藏书

知轩藏书书籍详情页添加优书网评分和直链,优书网书籍详情页添加知轩藏书下载链接

2018-06-28 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name         优书网 <=> 知轩藏书
// @namespace    http://tampermonkey.net/
// @description  知轩藏书书籍详情页添加优书网评分和直链,优书网书籍详情页添加知轩藏书下载链接
// @author       tianxin
// @match        *://www.zxcs8.com/post/*
// @match        *://www.yousuu.com/book/*
// @match        *://www.zxcs8.com/sort/*
// @grant        GM.xmlHttpRequest
// @connect      www.yousuu.com
// @connect      www.zxcs8.com
// @version      0.3
// ==/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 getRate(args,callback)
{
    var bookName = args.bookName;
    var bookAuthor = args.bookAuthor;
    getResult('http://www.yousuu.com/search/' + bookName + '?type=book', function(response){
        var rate = 0;
        var rateNum = 0;
        var bookLink = '';
        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;
        var matchAbstract = matchBook.getElementsByClassName('abstract')[0].innerHTML.split('<br>');
        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,"");
            bookLink = 'http://www.yousuu.com' + matchTitle.getElementsByTagName('a')[0].pathname;
        }
        var data = new Object();
        data.rate = rate;
        data.rateNum = rateNum;
        data.bookLink = bookLink;
        callback(data);
    });
}
(function() {
    'use strict';
    var site = location.hostname;
    if(site == 'www.zxcs8.com'){
        var tag = location.pathname.split('/')[1];
        if(tag == 'post'){
            var bookText = document.getElementsByTagName('h1')[0].innerHTML;
            var bookName = bookText.match('《(.*?)》')[1];
            var bookAuthor = bookText.split(':').pop();
            var args = {bookName:bookName,bookAuthor:bookAuthor};
            getRate(args,function(data){
                var rate = data.rate;
                var rateNum = data.rateNum;
                var bookLink = data.bookLink;
                var rateItem = '<p class="yinyong"><span style="font-size:14px;color:#FF0000;font-weight:bold">暂未查询到优书网评分</span><p>';
                if(rate != 0 || rateNum != 0){
                    rateItem = '<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>';
                }
                var obj = document.getElementsByClassName('yinyong')[0];
                if(!obj){
                    obj = document.getElementsByClassName('pagefujian')[0];
                }
                obj.insertAdjacentHTML('beforebegin',rateItem);
            });
        }else if(tag = 'sort'){
            var bookList = Array.prototype.slice.call(document.querySelectorAll('#plist'));
            bookList.forEach(function(item){
                var bookName = item.firstElementChild.innerText.match('《(.*?)》')[1];
                var bookAuthor = item.firstElementChild.innerText.split(':').pop();
                var args = {bookName:bookName,bookAuthor:bookAuthor};
                getRate(args,function(data){
                    var rate = data.rate;
                    var rateNum = data.rateNum;
                    var bookLink = data.bookLink;
                    if(rate != 0 || rateNum != 0){
                        var rateItem = '<a href= "' + bookLink + '" target = "_blank">&nbsp;&nbsp;&nbsp;评分:' + rate + '&nbsp;&nbsp;&nbsp;人数:' + rateNum + '</a>';
                        var obj = item.lastElementChild.querySelector('div');
                        obj.insertAdjacentHTML('beforebegin',rateItem);
                    }
                });
            });
        }
    }else if(site == 'www.yousuu.com'){
        var youBookName = document.getElementsByClassName('col-sm-7')[0].children[0].innerText;
        var youBookAuthor = document.getElementsByClassName('list-unstyled list-sm')["0"].firstChild.children["0"].innerText;
        getResult('http://www.zxcs8.com/index.php?keyword=' + youBookName,function(response){
            var html = document.createElement( 'html' );
            html.innerHTML = response;
            var bookList = Array.prototype.slice.call(html.getElementsByTagName('dl'));
            if(bookList.length > 0){
                bookList.forEach(function(item){
                    var bookSum = item.children["0"];
                    var bookLink = bookSum.getElementsByTagName('a')[0].href;
                    var matchBookName = bookSum.innerText.match('《(.*?)》')[1];
                    var matchBookAuthor = bookSum.innerText.split(':').pop();
//                    console.log(matchBookName + ':' + youBookName + ';' + matchBookAuthor + ':' + youBookAuthor );
                    if(matchBookName == youBookName && matchBookAuthor == youBookAuthor){
                        getResult(bookLink,function(response){
                            var html = document.createElement( 'html' );
                            html.innerHTML = response;
                            var downloadLink = html.getElementsByClassName('down_2')[0].getElementsByTagName('a')[0].href;
                            var obj = document.getElementsByClassName('sokk-book-button-groups')[0];
                            var button = '<div class="btn-group"><a href="' + downloadLink + '" target="_blank" class="btn btn-warning">去下载</a></div>';
                            obj.insertAdjacentHTML('beforeend',button);
                        })
                    }
                })
            }
        })
    }
})();