// ==UserScript== // @name 豆瓣中图法分类 // @description 如果存在中图分类,则会在豆瓣信息栏中多显示如“中图分类: 中图分类: G442 (G 文化、 科学、 教育、 体育 ▸ G4 教育 ▸ G44 教育心理学 ▸ G442 学习心理学);学科分类参考: 880 教育学 | 880 教育学 | 88027 教育心理学”,方便对图书进行分类。 // @author 018(lyb018@gmail.com) // @contributor Rhilip // @connect * // @grant GM_xmlhttpRequest // @grant GM_setClipboard // @grant GM_addStyle // @grant GM_setValue // @grant GM_getValue // @grant GM_listValues // @grant GM_deleteValue // @grant GM_registerMenuCommand // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js // @require https://greasyfork.org/scripts/368137-encodeToGb2312/code/encodeToGb2312.js?version=601683 // @include https://book.douban.com/* // @version 0.4.1 // @icon https://img3.doubanio.com/favicon.ico // @run-at document-end // @namespace http://018.ai // @downloadURL none // ==/UserScript== // This Userscirpt can't run under Greasemonkey 4.x platform if (typeof GM_xmlhttpRequest === 'undefined') { alert('不支持Greasemonkey 4.x,请换用暴力猴或Tampermonkey') return } // 不属于豆瓣的页面 if (!/douban.com/.test(location.host)) { return } const IsCourse = 'clc_setting_IsCourse'; const Subject = 'clc_setting_Subject'; const MoreVersion = 'clc_setting_MoreVersion'; const IsticKeys = 'clc_setting_IsticKeys'; ;(function () { 'use strict'; $(document).ready(function () { // 初始化 var isSubject = localStorage.getItem(Subject); if (!isSubject || isSubject.length === 0) { localStorage.setItem(Subject, 1); } var isCourse = localStorage.getItem(IsCourse); if (!isCourse || isCourse.length === 0) { localStorage.setItem(IsCourse, 1); } var moreVersion = localStorage.getItem(MoreVersion); if (!moreVersion || moreVersion.length === 0) { localStorage.setItem(MoreVersion, 1); } var isticKeys = localStorage.getItem(IsticKeys); if (!isticKeys || isticKeys.length === 0) { localStorage.setItem(IsticKeys, 1); } // 设置 $('.top-nav-info').append('中图法分类设置'); $('#clc_setting_btn').click(function(){ if ($('#clc_setting').length > 0) { return; } $('#wrapper > *').hide(); var $clc_setting = $('
中图分类 | \
是否教程 | \
学科分类参考 | \
更多版本 | \
关键字 | \
关键字: ' + keys.join(' | ')); $('div.indent span.all div.intro').eq(0).after('
关键字: ' + keys.join(' | '));
}
}, function(err, meta) {
});
}
function replaceKey(work) {
for (let e of $('div.indent div.intro p')) {
if (!e.querySelector('a')) {
$(e).html($(e).html().replace(eval('/' + work + '/g'), '' + work + ''));
}
}
}
// 请求图书馆联盟
function requestSuperlib(isbn) {
loadDoc('http://book.ucdrs.superlib.net/search?Field=all&channel=search&sw=' + isbn, {isbn: isbn}, function(doc, responseDetail, meta) {
let found = false;
let books = doc.querySelectorAll('.book1');
if (books.length <= 0) {
$('#info').append('中图分类: 查无此信息
');
return;
}
for (let book of books) {
if (found) break;
let a = book.querySelector('.book1 td>table a.px14');//
if (a) {
// superlib 单本书籍查看
let url = a.href.replace(location.host, 'book.ucdrs.superlib.net').replace('https', 'http');
loadDoc(url, {isbn: meta.isbn}, function(doc1, responseDetail1, meta1) {
found = $(".clcText").length > 0;
if (!found) {
let tubox = doc1.querySelector('.tubox dl').textContent;
let isbn1 = opt(/【ISBN号】.*\n/.exec(tubox)).replace(/【ISBN号】|-|\n/g, '');
if (eqisbn(meta1.isbn, isbn1)) {
let clcsText = opt(opt(/【中图法分类号】.*\n/.exec(tubox)).match(/[a-zA-Z0-9\.;]+/));
let clcs = clcsText.split(';');
if (clcs) {
for(let i = 0; i < clcs.length; i++) {
let clc = clcs[i];
let $clc = $('.clcText');
if($clc.length > 0) {
$clc.after('; '
+ clc + ' (...)');
requestClc(clc);
} else {
$('#info').append('中图分类: '
+ clc + ' (...)
');
requestClc(clc);
}
}
}
}
}
}, function(err, meta) {
$('#info').append('中图分类: 查无此信息
');
});
}
}
}, function(err, meta) {
$('#info').append('中图分类: 查无此信息
');
});
}
function trimAuthors(author) {
let authors = author.split(/[(|(|)|)|\/|\.|.|·|•|\s]/g);
var trimAs = [];
for (let a of authors) {
let trima = a.trim().replace('主编', '');
if (trima.length <= 1) continue;
if (trimAs.indexOf(trima) < 0) {
trimAs.push(trima);
}
}
return trimAs;
}
function moreversion(title, author) {
var trimTitle = title.replace(/[\(|(].*[\)|)]/g, '');
doGet('https://book.douban.com/j/subject_suggest?q=' + trimTitle, {isbn: trimTitle}, function(arr, responseDetail, meta) {
if (arr.length <= 0) {
$('#info').append('更多版本: 查无此信息
');
return;
}
$('#info').append('更多版本: ');
var trimAs = trimAuthors(author);
var html = '';
for (let item of arr) {
if (item.type !== 'b' || item.url === location.href) continue;
let trim_author_name = trimAuthors(item.author_name);
if ($(trimAs).filter(trim_author_name).length > 0) {
$('#info').append('' + item.title + '(' + item.year + ', ...)' + ' | ');
}
loadDoc(item.url, {id: item.id}, function(_doc, responseDetail, meta) {
let rating = _doc.querySelector('strong[property*="v:average"]').textContent;
if (rating && (rating = rating.trim()).length >= 1) {
var ratingPeople = _doc.querySelector('div.rating_sum a.rating_people span[property="v:votes"]').textContent;
if (!ratingPeople || ratingPeople.toString().trim().length <= 0) {
ratingPeople = 0;
}
$('#item__' + meta.id).html(rating + "/" + ratingPeople);
} else {
$('#item__' + meta.id).html("0/0");
}
}, function(err, meta) {
$('#item__' + meta.id).html("-/-");
});
}
$('#info').append('详情
');
}, function(err, meta) {
$('#info').append('更多版本: 详情...
');
});
}
// 请求istic,获取分类名称,备用,暂不启用。
function requestClc(clc) {
doPost('https://ct.istic.ac.cn/site/clc/getByClassName?t=' + new Date().getTime(),
{ "Content-Type": "application/x-www-form-urlencoded" }, 'classname=' + clc, {clc: clc}, function(json, responseDetail, meta) {
if (json.length == 0 && meta.clc.length > 0) {
if (meta.clc.includes('.')) {
requestClc(meta.clc.replace(/\.\d*$/, ''));
} else {
requestClc(meta.clc.replace(/\d$/, ''));
}
return;
}
let clcs = [];
let jsonMap = {};
let pid;
let level;
for (let i = 0; i < json.length; i ++) {
if (clcs.length == 0 && json[i].classNum.split('/').includes(clc)) {
clcs.push(json[i].classNum + ' ' + hanldeClcText(json[i].className));
pid = json[i].pid;
level = json[i].level;
} else if( json[i].level < level) {
jsonMap[json[i].id + ''] = json[i];
}
}
clcText(clc, pid, jsonMap, clcs);
htmlclc(clc, clcs);
}, function(err, meta) {
requestClcB(meta.clc);
});
}
// 处理clc文字
function clcText(clc, pid, jsonMap, rets) {
if (!jsonMap || !rets) return;
if( jsonMap[pid] ) {
rets.unshift(jsonMap[pid].classNum + ' ' + hanldeClcText(jsonMap[pid].className));
if (jsonMap[pid].level <= 2) return;
clcText(clc, jsonMap[pid].pid, jsonMap, rets);
}
}
function htmlclc(clc, rets) {
let clcText = $('#clcText_' + clc.replace('.', ''));
if (rets.length > 0) {
if (clcText.text() == '(...)' ) {
clcText.html('(' + rets.join(' ▸ ') + ')');
}
} else {
if (clcText.text() == '(...)' ) {
clcText.html('(查无此信息)');
}
}
}
function hanldeClcText(txt) {
var splits = txt.split(/、|(|)|\(|\)/);
var retss = [];
for (var s of splits) {
if (s.length == 0) continue;
retss.push(' ' + s + '');
if (localStorage.getItem(Subject) == '1') {
requestxhma(s);
}
}
return retss.join('、');
}
// 请求clcindex,获取分类名称
function requestClcB(clc) {
var url = 'https://www.clcindex.com/category/' + clc;
loadDoc(url, {clc: clc, url: url}, function(doc, responseDetail, meta) {
let clcs = [];
for (let li of doc.querySelectorAll('.breadcrumb li.active')) {
if (!li) continue;
var txtContent = li.textContent.trim();
var clcCode;
var index = txtContent.indexOf(' ');
if (index > 0) {
clcCode = txtContent.substr(0, index);
txtContent = txtContent.substr(index + 1);
}
clcs.push(clcCode + ' ' + hanldeClcText(txtContent));
}
$('#clc_' + meta.clc).attr('href', meta.url);
htmlclc(meta.clc, clcs);
}, function(err, meta) {
if (err.status == 404) {
if (meta.clc.includes('.')) {
requestClcB(meta.clc, meta.clc.replace(/\.\d*$/, ''));
} else {
requestClcB(meta.clc, meta.clc.replace(/\d$/, ''));
}
return;
}
let clcText = $('#clcText');
if (clcText.text() == '(...)' ) {
clcText.html('(无法获取)');
}
});
}
// 请求xhma,获取学科
function requestxhma(s) {
var url = 'http://xkfl.xhma.com/search?w=' + s;
loadDoc(url, {}, function(doc, responseDetail, meta) {
let spans = doc.querySelectorAll('.data li:not(.t) span');//code
if (spans.length == 1) {
let name = spans[0].nextElementSibling.textContent.trim();
if (name.includes(s)) {
let code = spans[0].textContent.trim();
appendsubject(' ' + code + ' ' + name + '');
}
} else if (spans.length > 1) {
for (let span of spans) {
if (!span) continue;
let name = span.nextElementSibling.textContent.trim();
if (name != s && (name != s + '学')) continue;
let code = span.textContent.trim();
appendsubject('' + code + ' ' + name + '');
}
}
}, function(err, meta) {
});
}
function appendsubject(subject) {
let clcText = $('#subjectText');
if (clcText.length == 0) {
$('#info').append('学科分类参考: ' + subject + '');
} else {
clcText.append(' | ' + subject + '');
}
}
// 判断两个ISBN是否相等,如:9787100005586与7100005582,具体阅读:http://www.banquanye.com/article/id-812
function eqisbn(val1, val2) {
if (!val1 || (val1.length != 13 && val1.length != 10) || !val2 || (val2.length != 13 && val2.length != 10)) return false;
let no1 = getISBNNo(val1);
let no2 = getISBNNo(val2);
return no1 == no2;
}
// 获取ID
function getIDFromURL(url) {
if (!url) return '';
var id = url.match(/subject\/.*\//g);
if (!id) return '';
return id[0].replace(/subject|\//g, '');
}
// 获取ISBN的No,去掉前三位的978以及最后一位验证码
function getISBNNo(val) {
if (!val || (val.length != 13 && val.length != 10)) return;
if (val.length == 13) {
return val.substr(3, 9);
} else if (val.length == 10) {
return val.substr(0, 9);
}
}
// 判断,空返回空字符串
function opt(val) {
if (!val) return '';
if (val instanceof Array) {
if (val.length > 0) {
return val[0];
}
} else {
return val;
}
}
// 对使用GM_xmlhttpRequest返回的html文本进行处理并返回DOM树
function page_parser(responseText) {
// 替换一些信息防止图片和页面脚本的加载,同时可能加快页面解析速度
responseText = responseText.replace(/s+src=/ig, ' data-src='); // 图片,部分外源脚本
responseText = responseText.replace(/