Greasy Fork

Greasy Fork is available in English.

获取优书网评分

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

当前为 2018-06-27 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         获取优书网评分
// @namespace    http://tampermonkey.net/
// @description  知轩藏书书籍详情页添加优书网评分和直链
// @author       tianxin
// @match        *://www.zxcs8.com/post/*
// @match        *://www.yousuu.com/book/*
// @grant        GM.xmlHttpRequest
// @connect      www.yousuu.com
// @connect      www.zxcs8.com
// @version      0.2
// ==/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.split(':').pop();
        getResult('http://www.yousuu.com/search/' + bookName + bookAuthor + '?type=all', function(response){
            var rate = 0;
            var rateNum = 0;
            var rateText = '暂未查询到优书网评分';
            var rateItem = '<p class="yinyong"><span style="font-size:14px;color:#FF0000;font-weight:bold">'+ rateText +'</span><p>';
            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,"");
            }
          //  console.log(matchAuthor + ':' + bookAuthor + ';' + matchName + ':' + bookName);
            var obj = document.getElementsByClassName('yinyong')[0];
            if(!obj){
                obj = document.getElementsByClassName('pagefujian')[0];
            }
            if(rate != 0 || rateNum != 0){
                var bookLink = 'http://www.yousuu.com' + matchTitle.getElementsByTagName('a')[0].pathname;
                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>';
            }
            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);
                        })
                    }
                })
            }
            return '';
        })
    }
})();