Skip to content

Instantly share code, notes, and snippets.

@holys
Last active December 14, 2015 10:49
Show Gist options
  • Save holys/5074554 to your computer and use it in GitHub Desktop.
Save holys/5074554 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name GDUFS library helper
// @namespace http://library.gdufs.edu.cn
// @version 0.1.0
// @description Show the available books amount in GDUFS library.
// @match http://book.douban.com/*
// @match http://opac.gdufs.edu.cn:8118/apsm/recommend/recommend_nobor.jsp*
// @copyright 2012-2013, Link, hbc,cdh(later)
// @require http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js
// @require http://isbn.jpn.org/js/isbn.js
// @require http://isbn.jpn.org/js/isbn-groups.js
// ==/UserScript==
// @grant GM_xmlhttpRequest
var helper = {
pages: {
subject: {},
subject_search: {}
},
utils: {},
tpl: {},
parser: {},
main: {}
};
/*utils*/
// parser
helper.parser.book_meta = function (raw){
var author = $('#info a ', raw).html();
if (author !== null){
author = author.trim();
}
var publisher = /出版社: (.*)/.exec($('#info', raw).text());
if (publisher !== null){
publisher = publisher[1].trim();
}
var pubdate = /出版年: (.*)/.exec($('#info', raw).text());
if (pubdate !== null){
pubdate = /[\d]+/.exec(pubdate[1].trim());
pubdate = pubdate[0];
}
var price = /定价: (.*)/.exec($('#info', raw).text());
if (price !== null){
price = price[1].trim();
}
var isbn = /ISBN: (.*)/.exec($('#info', raw).text());
if (isbn !== null){
isbn = isbn[1].trim();
}
console.log('book_meta begin to work');
return{
title: $('h1 span', raw).html(),
author: author,
publisher: publisher,
pubdate: pubdate,
price: price,
isbn: isbn
};
};
// recommend buying books
helper.pages.recommend = function (){
var book = /douban_ref=(.*)+/.exec(document.URL);
if(!book){
return;
}
GM_xmlhttpRequest({
method: 'GET',
url: book[1],
onload: function(resp){
var book_meta;
if (resp.status !== 200){
return;
}
// console.log(resp.status);
book_meta = helper.parser.book_meta(
$(resp.response).filter('div#wrapper')
);
// console.log($(resp.response).filter('div#wrapper'));
$('[name="Z13_TITLE"]').val (book_meta.title);
$('[name="Z13_AUTHOR"]').val(book_meta.author);
console.log(book_meta.author);
$('[name="Z13_IMPRINT"]').val(book_meta.publisher);
$('[name="Z13_YEAR"]').val( book_meta.pubdate);
$('[name="Z13_ISBN_ISSN"]').val (book_meta.isbn);
$('[name="Z13_PRICE"]').val (book_meta.price);
}
});
};
helper.main = function() {
var type = /[com,8118]\/([\w]+)\/*/.exec(document.URL);
type = (type !== null)?(type[1].trim()):('index');
if(type==='subject') {
helper.pages.subject();
}else if(type==='subject_search') {
helper.pages.subject_search();
}else if(type==='apsm') {
helper.pages.recommend();
}else {
console.log(type);
}
};
helper.main();
@holys
Copy link
Author

holys commented Mar 7, 2013

// ==UserScript==
// @name       GDUFS library helper
// @namespace  http://library.gdufs.edu.cn
// @version    0.1.0
// @description  Link Douban Book and GDUFS Library
// @match      http://book.douban.com/*
// @match      http://opac.gdufs.edu.cn:8118/apsm/recommend/recommend_nobor.jsp*
// @copyright  2012-2013, Link, hbc,cdh(later)
// @require http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js
// @grant GM_xmlhttpRequest
// ==/UserScript==

var helper = {
    pages: {
        subject: {}
    },
    utils: {},
    //tpl: {},
    url: 'http://opac.gdufs.edu.cn',
    parser: {},
    main: {}
};

/*utils*/
helper.utils.tmpl = function(str, data) {
    var re = /\{\{([\w ]+)\}\}/, ret = str;
    var r;

    while ((r = re.exec(ret)) !== null) {
        ret = ret.replace(r[0], data[r[1].trim()]);
    }

    return ret;
};

// parser
helper.parser.book_meta = function (raw){

    var author = $('#info a ', raw).html();
    if (author !== null){
        author = author.trim();
    }


    var publisher = /: (.*)/.exec($('#info', raw).text());
    if (publisher !== null){
        publisher = publisher[1].trim();
    }

    var pubdate = /: (.*)/.exec($('#info', raw).text()); 
    if (pubdate !== null){
        pubdate = /[\d]+/.exec(pubdate[1].trim());

        pubdate = pubdate[0];

    }

    var price = /: (.*)/.exec($('#info', raw).text());
    if (price !== null){
        price = price[1].trim();
    }

    var isbn = /ISBN: (.*)/.exec($('#info', raw).text());
    if (isbn !== null){
        isbn = isbn[1].trim();
    }
    console.log('book_meta begin to work');
    return{
        title: $('h1 span', raw).html(),
        author: author,
        publisher: publisher,
        pubdate: pubdate,
        price: price,
        isbn: isbn
    };
};

//pages

helper.pages.checkLibrary = function(){
    var isbn ;
    var mode = true;
    var base_search_url_isbn = "http://opac.gdufs.edu.cn:8991/F/?func=find-b&find_code=ISB&request={{data}}&local_base=GWD01&filter_code_1=WLN";
    var search_url = base_search_url_isbn.replace("{{data}}", isbn);
    GM_xmlhttpRequest({
        method: 'GET',
        url: search_url,
        onload: function(resp){
            if (resp.status !== 200){
                return;
            }
        if($('#feedbackbar',resp).html().length !== 6)
            mode = false;
            return mode;

        }
    });
};
console.log(mode);
var href = window.location.toString();
//console.log('isbn is '+isbn);
helper.pages.subject = function(isbn,mode){
    //var search_url;
    var href = window.location.toString();
    //var recommend_url = 'http://opac.gdufs.edu.cn:8118/apsm/recommend/recommend_nobor.jsp?douban_ref={{href}}';
    var style = '"float:left;display: inline-block;background: #33A057;border: 1px solid #2F7B4B;color: white;padding: 1px 10px;border-radius:3px;margin-right: 8px;';

    // if ( href.indexOf('subject')!=-1 ) {
    //     search_url = base_search_url_isbn.replace("{{data}}", isbn);
    // }

    // if (){
    //     var button = $('<a href="'+search_url+'" style=style target="_blank">借阅</a>');
    // }
    // else {
    //     var button = $('<a href="'+recommend_url+'" style=style target="_blank">荐购</a>');
    // }
    var button ;
    if(mode==1){
            button = helper.utils.tmpl('<a href="{{url}}:8991/F?func=find-b&find_code=ISB&request={{isbn}}&local_base=GWD01&filter_code_1=WAN"',{url:helper.url,isbn:isbn});
    }
    else{
            button = helper.utils.tmpl('<a href="{{url}}:8118/apsm/recommend/recommend_nobor.jsp?douban_ref={{href}}" style="{{style}}" target="_blank">借阅 </a>',{url:helper.url,href:href,style:style});
    }
    return button;
};



// recommend buying books
helper.pages.recommend = function (){
    var book = /douban_ref=(.*)+/.exec(document.URL);

    if(!book){
        return;
    }

    GM_xmlhttpRequest({
        method: 'GET',
        url: book[1],
        onload: function(resp){
            var book_meta;

            if (resp.status !== 200){
                return;
            }
            // console.log(resp.status);

            book_meta = helper.parser.book_meta(
                $(resp.response).filter('div#wrapper')
            );
            // console.log($(resp.response).filter('div#wrapper'));


            $('[name="Z13_TITLE"]').val (book_meta.title);

            $('[name="Z13_AUTHOR"]').val(book_meta.author);
            console.log(book_meta.author);

            $('[name="Z13_IMPRINT"]').val(book_meta.publisher);
            $('[name="Z13_YEAR"]').val( book_meta.pubdate);
            $('[name="Z13_ISBN_ISSN"]').val (book_meta.isbn);
            $('[name="Z13_PRICE"]').val (book_meta.price);


        }

    });

};

//select one to execute
helper.main = function() {
    var type = /[com,8118]\/([\w]+)\/*/.exec(document.URL);
    type = (type !== null)?(type[1].trim()):('index');

    if(type==='subject') {
        helper.pages.subject();
    }else if(type==='apsm') {
        helper.pages.recommend();
    }else {
        console.log(type);
    }
};

//program entry
helper.main();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment