Greasy Fork

Greasy Fork is available in English.

获取优书网评分

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

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

// ==UserScript==
// @name         获取优书网评分
// @namespace    http://tampermonkey.net/
// @description  知轩藏书书籍详情页添加优书网评分和直链
// @author       tianxin
// @match        *://www.zxcs8.com/post/*
// @grant        GM.xmlHttpRequest
// @connect      www.yousuu.com
// @version      0.1.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.match(':(.*?)$')[1];
        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;
            //Object.values(matchTag).forEach(function(item){
              //  matchName += item.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);
        })
    }
})();