// ==UserScript==
// @name Douban helper for SYSU
// @name:zh-CN 中大图书馆豆瓣助手
// @description 为豆瓣图书增加中大图书馆藏
// @author Hanchy Hill
// @namespace https://minhill.com
// @include http://book.douban.com/subject/*
// @include http://202.116.64.108:8080/apsm/recommend/recommend_nobor.jsp*
// @include http://read.douban.com/ebook/*
// @require http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// @version 1.5.5
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @grant GM_openInTab
// @grant GM_deleteValue
// @grant GM_addStyle
// @grant GM_registerMenuCommand
// @icon http://tb.himg.baidu.com/sys/portrait/item/3336cdf5d2af706879736963616c6a32
// @downloadURL none
// ==/UserScript==
GM_addStyle("#ISBNLoading,#titleLoading { list-style-type:none; }");
var schoolList=["SYSU","SCUT","SCNU","GDUT","GDUFS","GZHTCM","GZHU","GZARTS","XHCOM"];
//个人选项设置
var prefs={
school:"SYSU",
studentID:"2333333",
password:"Hello_Kitty",
campus:"中山大学东校区",
telephone:"13145201748",
name:"小三",
eMail:"HelloKitty@sysu.edu.cn"
}
function LibMeta(schoolName){
this.state=null;
this.error=false;
this.errorMsg=null;
this.type=null;
this.link=null;
this.items=null;
this.originUrl=null;
this.school=schoolName;
}
function LibItem(school){
this.bookName=null;
this.author=null;
this.bookIndex=null;
this.publisher=null;
this.pubDate=null;
this.school=school;
this.link=null;
this.type="booklist";
}
function StoreItem(school){
this.school=school;
this.storeState=null;
this.borrowTime=null;
this.returnTime="";
this.location=null;
this.bookIndex=null;
this.branch=null;
this.link=null;
this.type="store";
}
///////////////////////////豆瓣图书元信息///////////////////////////////////
bookMeta=(function(){
if(location.href.indexOf('book')==-1){return null;}
if(location.href.indexOf('douban.com/subject')!=-1){
//执行豆瓣图书Func.
var rawBookInfo=document.getElementById("info").innerHTML; //获取info块
var author = document.querySelector("#info a"); //获取作者
if (author){
author = author.innerHTML.trim();
}
var title =document.querySelector('h1 span').textContent;
var bracketIndex = title.indexOf("(");//去除括号,防止搜索进行子匹配操作
if(bracketIndex!=-1){
title=title.slice(0,bracketIndex);
}
var publisher = /出版社:<\/span>(.*)
/.exec(rawBookInfo);
if (publisher !== null){
publisher = publisher[1].trim();
}
var pubdate = /出版年:<\/span>(.*)
/.exec(rawBookInfo);
if (pubdate !== null){
pubdate = /[\d]+/.exec(pubdate[1].trim());
pubdate = pubdate[0];
}
var price = /定价:<\/span>(.*)
/.exec(rawBookInfo);
if (price !== null){
price = price[1].trim();
}
var isbn = /ISBN:<\/span>(.*)
/.exec(rawBookInfo);
if (isbn !== null){
isbn = isbn[1].trim();
}
var bookIndex = /统一书号:<\/span>(.*)
/.exec(rawBookInfo);
if (bookIndex !== null){
bookIndex = bookIndex[1].trim();
}
var rating = document.querySelector('#interest_sectl .rating_num').innerHTML.trim();
if (!rating) {
rating = '暂无评分';
}
}
else if(location.href.indexOf('ebook')!=-1){
var allNodes, isbn=null;
allNodes = document.evaluate('//a[@itemprop="isbn"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);//获取isbn
//alert(allNodes.snapshotItem(0));
if(allNodes.snapshotItem(0)){
isbn=allNodes.snapshotItem(0).innerHTML;
//alert(isbn);
}
var title;
allNodes = document.evaluate('//h1[@itemprop="name"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
if(allNodes.snapshotItem(0)){
title=allNodes.snapshotItem(0).innerHTML;
//alert(title);
}
var publisher;
allNodes = document.evaluate('//span[@itemprop="publisher"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
if(allNodes.snapshotItem(0)){
publisher=allNodes.snapshotItem(0).innerHTML;
//alert(publisher);
}
var author;
allNodes = document.evaluate('//span[@itemprop="author"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
if(allNodes.snapshotItem(0)){
author=allNodes.snapshotItem(0).innerHTML;
//alert(author);
}
var pubdate;
allNodes = document.evaluate('//span[@itemprop="datePublished"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
if(allNodes.snapshotItem(0)){
pubdate=allNodes.snapshotItem(0).innerHTML;
pubdate=pubdate.slice(0,4);
//alert(pubdate);
}
var rating='暂无评分';
var price="";
var bookIndex="";
}
//////////////////////ISBN转为旧格式///////////////////////////////////
function ISBN10(isbn){
if(isbn == null){
return null;
}
else if(pubdate == null){
return isbn;
}
var rawISBN=isbn.slice(3,12);
var checkCode=0;//校验码
for(i=0;i<9;i++){
checkCode+=parseInt(rawISBN[i])*(10-i);
}
checkCode=11-checkCode%11;
if(checkCode==10){checkCode="X";}
var preCode="";
publishDate=Number(pubdate.slice(0,4));
if((publishDate)>=2007){//判断年份以检查是否需要加前缀和修正校验位
preCode=isbn.slice(0,3)+"-";
checkCode=isbn[12];
}
switch(rawISBN[1]){
case '0':
ISBNold=preCode+rawISBN[0]+"-"+rawISBN.slice(1,3)+"-"+rawISBN.slice(3,9)+"-"+checkCode;
break;
case '1':
case '2':
case '3':
ISBNold=preCode+rawISBN[0]+"-"+rawISBN.slice(1,4)+"-"+rawISBN.slice(4,9)+"-"+checkCode;
break;
case '5':
case '7':
ISBNold=preCode+rawISBN[0]+"-"+rawISBN.slice(1,5)+"-"+rawISBN.slice(5,9)+"-"+checkCode;
break;
case '4':
case '8':
ISBNold=preCode+rawISBN[0]+"-"+rawISBN.slice(1,6)+"-"+rawISBN.slice(6,9)+"-"+checkCode;
break;
default:
ISBNold=rawISBN;
break;
}
return ISBNold;
}
var isbn10=ISBN10(isbn,pubdate);
/////////////////////////////////////////////////////////
return{
"title": title,
"author": author,
"publisher": publisher,
"pubdate": pubdate,
"price": price,
"isbn": isbn,
"bookIndex": bookIndex,
"rating": rating,
"isbn10": isbn10
};
})();
//alert(bookMeta.isbn10);
function eBookMeta(){
var allNodes, isbn;
allNodes = document.evaluate('//a[@itemprop="isbn"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);//获取isbn
if(allNodes){
isbn=allNodes.snapshotItem(0).innerHTML;
alert(isbn);
}
var title;
allNodes = document.evaluate('//h1[@itemprop="name"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
if(allNodes){
title=allNodes.snapshotItem(0).innerHTML;
alert(title);
}
var publisher;
allNodes = document.evaluate('//span[@itemprop="publisher"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
if(allNodes){
publisher=allNodes.snapshotItem(0).innerHTML;
alert(publisher);
}
var author;
allNodes = document.evaluate('//span[@itemprop="author"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
if(allNodes){
author=allNodes.snapshotItem(0).innerHTML;
alert(author);
}
var pubdate;
allNodes = document.evaluate('//span[@itemprop="datePublished"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
if(allNodes){
pubdate=allNodes.snapshotItem(0).innerHTML;
pubdate=pubdate.slice(0,4);
//alert(pubdate);
}
}
//各学校元信息
var schoolInfo={
//中山大学
"SYSU":{
name:"中山大学",
anySearchUrl:"http://202.116.64.108:8991/F/?func=find-b&find_code=WRD&request=%s",
anyForeianSearchUrl:"http://202.116.64.108:8991/F/?func=find-b&find_code=WRD&request=%s&local_base=ZSU09",
isbnSearchUrl:"http://202.116.64.108:8991/F/?func=find-b&find_code=ISB&request=%s",
isbnForeianSearchUrl:"http://202.116.64.108:8991/F/?func=find-b&find_code=ISB&request=%s&local_base=ZSU09",
titleSearchUrl:"",
abbrName:"中大",
isGBK:false,
recommendUrl:"http://202.116.64.108:8080/apsm/recommend/recommend_nobor.jsp"
},
//华南理工大学
"SCUT":{
name:"华南理工大学",
anySearchUrl:"http://202.38.232.10/opac/servlet/opac.go?CLANLINK=&CODE=&FIELD1=TITLE&MARCTYPE=&MODE=FRONT&ORGLIB=SCUT&PAGE=&RDID=ANONYMOUS&SCODE=&TABLE=&VAL1=%s&cmdACT=simple.list&libcode=",
isbnSearchUrl:"http://202.38.232.10/opac/servlet/opac.go?CLANLINK=&CODE=&FIELD1=ISBN&MARCTYPE=&MODE=FRONT&ORGLIB=SCUT&PAGE=&RDID=ANONYMOUS&SCODE=&TABLE=&VAL1=%s&cmdACT=simple.list&libcode=",
abbrName:"华理工",
isGBK:false
},
//南中国一般大学
"SCNU":{
name:"华南师范大学",
abbrName:"华师",
anySearchUrl:"http://202.116.41.246:8080/opac/openlink.php?sort=M_TITLE&orderby=ASC&title=%s",
isbnSearchUrl:"http://202.116.41.246:8080/opac/openlink.php?isbn=%s&_m=1",
isGBK:false
},
//广东工业大学
"GDUT":{
name:"广东工业大学",
abbrName:"广工",
anySearchUrl:"http://222.200.98.171:81/searchresult.aspx?anywords=%s&dt=ALL&cl=ALL&dp=20&sf=M_PUB_YEAR&ob=DESC&sm=table&dept=ALL",
isbnSearchUrl:"http://222.200.98.171:81/searchresult.aspx?isbn_f=%s&dt=ALL&cl=ALL&dp=20&sf=M_PUB_YEAR&ob=DESC&sm=table&dept=ALL",
isGBK:true
},
//外语外贸大学
"GDUFS":{
name:"广东外语外贸大学",
abbrName:"广外",
anySearchUrl:"http://opac.gdufs.edu.cn:8991/F/?find_code=WRD&request=%s&func=find-b",
isbnSearchUrl:"http://opac.gdufs.edu.cn:8991/F/?func=find-b&find_code=ISB&request=%s&local_base=GWD01",
isbnForeianSearchUrl:"http://opac.gdufs.edu.cn:8991/F/?func=find-b&find_code=ISB&request=%s&local_base=GWD09",
isGBK:false
},
//广州中医药大学
"GZHTCM":{
name:"广州中医药大学",
abbrName:"广中医",
anySearchUrl:"http://210.38.102.131:86/opac/openlink.php?sort=M_TITLE&orderby=ASC&title=%s",
isbnSearchUrl:"http://210.38.102.131:86/opac/openlink.php?strText=%s&strSearchType=isbn",
isGBK:false
},
//广州大学
"GZHU":{
name:"广州大学",
abbrName:"广大",
anySearchUrl:"http://lib.gzhu.edu.cn:8080/bookle/?id=searchForm&displayPages=15&index=default&matchesPerPage=10&query=%s&searchPage=1&submit=Bookle%20%E6%90%9C%E7%B4%A2",
isbnSearchUrl:"http://lib.gzhu.edu.cn:8080/bookle/?index=default&query=STANDARDNO%3A%28%s%29",
isGBK:false
},
//广州美术学院gzarts
"GZARTS":{
name:"广州美术学院",
abbrName:"广美",
anySearchUrl:"http://121.33.246.167/opac/searchresult.aspx?anywords=%s&dt=ALL&cl=ALL&dp=20&sf=M_PUB_YEAR&ob=DESC&sm=table&dept=ALL&ecx=0",
isbnSearchUrl:"http://121.33.246.167/opac/searchresult.aspx?isbn_f=%s&dt=ALL&cl=ALL&dp=20&sf=M_PUB_YEAR&ob=DESC&sm=table&dept=ALL&ecx=0",
//http://121.33.246.167/opac/searchresult.aspx?isbn_f=7-02-000220-X&dt=ALL&cl=ALL&dp=20&sf=M_PUB_YEAR&ob=DESC&sm=table&dept=ALL&ecx=0
isGBK:true
},
//星海音乐学院
"XHCOM":{},
//广东药学院,由于编码问题和没提供ISBN检索,暂不支持//
}
/////////////////////豆瓣处荐购结束///////////////////////////////////////
//函数:提取isbn搜索元信息
var isbnFilter={
//中山大学
SYSU: {
respond:function (reDetails,frameLocation) {
var fullUrl = reDetails.finalUrl;
if (reDetails.status !== 200&&reDetails.status !== 304){
var msg = new LibMeta("SYSU");
msg.state="error";
msg.errorMsg="ISBN连接错误";
//alert("ISBN连接错误");//后续版本再处理
messageCatcher(msg,frameLocation);
return;
}
//document.getElementById("footer").textContent=reDetails.responseText;
if(reDetails.responseText.indexOf('indexpage')!=-1){
//alert("ISBN查无此书"); //增加荐购
var msg = new LibMeta("SYSU");
msg.state="recommend";
//hasBook = false;
//recommendBook();
messageCatcher(msg,frameLocation);
return;
}
//document.getElementById("footer").textContent=reDetails.responseText;
//var frame =document.createElement("div");
//frame.innerHTML = reDetails.responseText;
//alert(frame.innerHTML);
if(reDetails.responseText.indexOf('Search Results')!=-1){
//alert("跳转到搜索页");
//var frame =document.createElement("div");
//frame.innerHTML = reDetails.responseText;
//document.getElementById("reviews").textContent=frame.innerHTML;
titleFilter.SYSU.filter(reDetails.responseText,fullUrl,frameLocation);
}
else{
isbnFilter.SYSU.filter(reDetails.responseText,frameLocation);
}
return;
},
filter:function(gettxt,frameLocation){
getWhere = function(where,eBook,frameLocation){
//alert(typeof where);
GM_xmlhttpRequest({ //获取列表
method : 'GET',
synchronous : false,
url : where[0],
//url : fullurl,//"http://202.116.64.108:8991/F/?func=find-b&find_code=ISB&request=7101003044",
onload : function (reDetails) {
var libra =document.createElement("div");
libra.innerHTML = reDetails.responseText;
getBookinfo(libra.innerHTML,eBook,frameLocation,reDetails.finalUrl);//回调函数馆藏位置获取
}
});
/////////////////回调函数馆藏位置获取////////////////////////////////////////
getBookinfo = function(webText,eBook,frameLocation,finalUrl){
var hasBook = true;
webText = webText.replace(/[ | ]*\n/g,'').replace(/\n[\s| | ]*\r/g,'').replace(/amp;/g,"");
//alert(webText);
///防止无书籍的情况发生
if(webText.indexOf('无匹配单册')!=-1){
var msg = new LibMeta("SYSU");
msg.state="recommend";
messageCatcher(msg,frameLocation);
}
else{
blockBook = webText.match(/OPAC注释(.*?)<\/tbody>/)[1];
//alert(typeof blockBook);
borrowItem = blockBook.match(/