// ==UserScript== // @id xuqb@lib.pku.edu.cn // @name 豆瓣读书揭示北大馆藏 // @namespace lib.pku.edu.cn // @version 1.0 // @description 为豆瓣读书增加北京大学图书馆相关资源链接。图书页面右侧的“在哪儿借这本书”中将出现红色图标及“北京大学图书馆”链接,点击链接可以打开北大馆藏目录中该书的检索结果。如果该书有 ISBN(国际标准书号)但查无此书,或没有 ISBN,则改为提供一个按书名检索的链接。 // @author 馆君 // @match https://book.douban.com/subject/* // @connect 162.105.138.200 // @grant GM_xmlhttpRequest // @grant GM_setClipboard // @grant GM_addStyle // @grant GM_setValue // @grant GM_getValue // @downloadURL none // ==/UserScript== GM_addStyle("@charset utf-8; #borrowinfo ul img {height: 16px; vertical-align: text-bottom; padding-right: 2px;}"); function getDoc(url, meta, callback) { // XHR 回调函数模板 GM_xmlhttpRequest({ method: 'GET', url: url, headers: { 'User-agent': window.navigator.userAgent, 'Content-type': null }, onload: function(responseDetail) { var doc = ''; if (responseDetail.status == 200) { doc = new DOMParser().parseFromString(responseDetail.responseText, 'text/html'); if (doc === undefined) { doc = document.implementation.createHTMLDocument(""); doc.querySelector('html').innerHTML = responseText; } } callback(doc, responseDetail, meta); } }); } (function() { 'use strict'; if($("#borrowinfo").length <= 0) // 如果借阅信息面板不存在,模仿常例创建之 $("#buyinfo").after($("

在哪儿借这本书 · · · · · ·

")); $("#borrowinfo ul.more-after").prepend($("
  • 北京大学图书馆……检索中,请稍等
  • ")); // 创建北大图书馆项目 var fieldTI = $("h1 span").text(); // 获取豆瓣上的当前书名 var searchTIurl = "http://162.105.138.200/uhtbin/cgisirsi/x/北大中心馆/0/5?searchdata1=" + fieldTI + "&library=ALL&srchfield1=TI%5ETITLE%5E%5ETitle%25"; var fieldISBN = $("#info").text().match(/ISBN:.+\d+X?/, "i"); // 获取 ISBN 号(豆瓣的 ISBN 不包含任何连字符) if (fieldISBN) { fieldISBN = fieldISBN[0].match(/\d+X?/, "i")[0]; // 截取数字部分 var searchISBNurl = "http://162.105.138.200/uhtbin/isbn/" + fieldISBN; getDoc(searchISBNurl, null, function(doc, resp, meta) { var searchSumHits = $("div.searchsum_hits .content p", doc).text(); // 单个命中结果 if (!searchSumHits.match(/\d+\D+of\D+\d+/)) { // 无论中英文版 OPAC 都是这个格式 var hitListSearchSummary = $("#hitlist .hit_list .hitstop .searchsummary", doc).text(); // 多个命中结果 if (!/检索到\D+\d+\D+题名/.test(hitListSearchSummary) && !/search found\D+\d+\D+titles/.test(hitListSearchSummary)) { $("#PKULib").append($("(ISBN 查无结果)")); $("#searchingPKULib").remove(); return; } } $("#PKULib").wrap($("")); // 生成按 ISBN 检索的链接 $("#searchingPKULib").remove(); }); } else { $("#PKULib").append($("(该书无 ISBN)")); // 因为没有 ISBN 所以增加 sort_by 参数,按从旧到新排序搜索结果 $("#searchingPKULib").remove(); } })();