-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path豆瓣中图法分类.user.js
602 lines (533 loc) · 24.6 KB
/
豆瓣中图法分类.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// ==UserScript==
// @name 豆瓣中图法分类
// @description 如果存在中图分类,则会在豆瓣信息栏中多显示如“中图分类: 中图分类: G442 (G 文化、 科学、 教育、 体育 ▸ G4 教育 ▸ G44 教育心理学 ▸ G442 学习心理学);学科分类参考: 880 教育学 | 880 教育学 | 88027 教育心理学”,方便对图书进行分类。
// @author 018([email protected])
// @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.0
// @icon https://img3.doubanio.com/favicon.ico
// @run-at document-end
// @namespace http://018.ai
// ==/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);
}
$('.top-nav-info').append('<a href="javascript:;" id="clc_setting_btn">中图法分类设置</a>');
$('#clc_setting_btn').click(function(){
if ($('#clc_setting').length > 0) {
return;
}
$('#wrapper > *').hide();
var $clc_setting = $('<div id="clc_setting">\
<h1>\
<span>豆瓣中图法分类</span>\
</h1>\
<div>\
<table>\
<tbody>\
<tr>\
<td><input type="checkbox" checked lable="中图分类" disabled><span>中图分类</span></td>\
</tr>\
<tr>\
<td><input type="checkbox" ' + (localStorage.getItem(IsCourse) === '1' ? 'checked' : '') + ' lable="是否教程" data-config="' + IsCourse + '"><span>是否教程</span></td>\
</tr>\
<tr>\
<td><input type="checkbox" ' + (localStorage.getItem(Subject) === '1' ? 'checked' : '') + ' lable="学科分类参考" data-config="' + Subject + '"><span>学科分类参考</span></td>\
</tr>\
<tr>\
<td><input type="checkbox" ' + (localStorage.getItem(MoreVersion) === '1' ? 'checked' : '') + ' lable="更多版本" data-config="' + MoreVersion + '"><span>更多版本</span></td>\
</tr>\
<tr>\
<td><input type="checkbox" ' + (localStorage.getItem(IsticKeys) === '1' ? 'checked' : '') + ' lable="关键字" data-config="' + IsticKeys + '"><span>关键字</span></td>\
</tr>\
</tbody>\
</table>\
</div>\
<br>\
<h2>设置完成后刷新即可。</h2>\
</div>');
$clc_setting.on('click', 'input', function() {
localStorage.setItem($(this).data('config'), $(this).prop('checked') ? 1 : 0);
});
$('#wrapper').append($clc_setting);
});
// 分类
var infos = $('#info').text();
infos = infos.replace(/^[\xA0\s]+/gm, '')
.replace(/[\xA0\s]+$/gm, '')
.replace(/\n+/g, '\n')
.replace(/:\n+/g, ': ')
.replace(/]\n/g, ']')
.replace(/】\n/g, '】')
.replace(/\n\/\n/g, '/');
var title = $('#wrapper h1').eq(0).text().replace(/\n| /g, '');
var isbn, author;
for (var section of Object.values(infos.split('\n'))) {
if (!section || section.trim().length <= 0) continue;
let index = section.indexOf(':');
if (index <= -1) continue;
let key = section.substr(0, index).trim();
let value = section.substr(index + 1).trim();
switch (key) {
// book
case "作者":
author = value;
break;
case "ISBN":
isbn = value;
break;
default:
break;
}
}
if( isbn && title ) {
// superlib 搜索
requestSuperlib(isbn);
}
if (localStorage.getItem(MoreVersion) == '1') {
if( author && title ) {
moreversion(title, author);
}
}
var abstract;
var h2s = $('div.related_info h2');
for (var i = 0; i < h2s.length; i++) {
let h2 = h2s[i];
let span = h2.querySelector('span');
if(span && span.textContent === '内容简介') {
var intro = h2.nextElementSibling.querySelector('.all div.intro');
if(!intro) {
intro = h2.nextElementSibling.querySelector('div.intro');
}
if(intro) {
abstract = intro.textContent;
}
break;
}
}
if (localStorage.getItem(IsCourse) == '1') {
if (abstract.includes('教科书')) {
$('#mainpic').append('<div class="indent"><span class="tag">教科书</span></div>');
} else if (abstract.includes('教材')) {
$('#mainpic').append('<div class="indent"><span class="tag">教材</span></div>');
} else if (abstract.includes('课程')) {
$('#mainpic').append('<div class="indent"><span class="tag">课程</span></div>');
} else if (abstract.includes('课本')) {
$('#mainpic').append('<div class="indent"><span class="tag">课本</span></div>');
}
}
if (localStorage.getItem(IsticKeys) == '1') {
// 目录
let id = getIDFromURL(location.href);
let dir = $('#dir_' + id + '_full').text();
if (dir) {
dir = dir.replace(/(([\xA0\s]*)\n([\xA0\s]*))+/g, '').replace('· · · · · · (收起)', '');
}
let content = abstract + dir;
if (content.length > 0) {
requestIstic(content);
}
}
})
function requestIstic(abstract) {
let headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Connection": "keep-alive",
"Accept": "application/json, text/javascript, */*; q=0.01",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4229.0 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "https://ct.istic.ac.cn",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": "https://ct.istic.ac.cn/site/term/participle",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Cookie": "name=value"
}
doPost('https://ct.istic.ac.cn/site/term/ceshihjson', headers, 'id=lyb018' + new Date().getTime() + '&inputText=' + encodeURIComponent(abstract), {}, function(json, responseDetail, meta) {
if (json && json.t1 && json.t1.length > 0) {
// 最多取五个
let keys = [];
json.t1.sort(function (a, b) {
return b.frequency - a.frequency;
});
for (let i = 0; i < json.t1.length; i++ ) {
let work = json.t1[i].word.trim();
if(work.length == 0 || ['第', '部分', '章', '●', 'Chapter', '-', '#', '附录'].includes(work)) continue;
keys.push('<span style="color:#EB9108">' + json.t1[i].word + '(' + json.t1[i].frequency + ')' + '</span>');
replaceKey(json.t1[i].word);
if (keys.length >= 5) break;
}
$('div.indent div.intro').eq(0).after('<p class="IsticKeys">关键字: ' + keys.join('<span style="color:#989B9B"> | </span>'));
$('div.indent span.all div.intro').eq(0).after('<p class="IsticKeys">关键字: ' + keys.join('<span style="color:#989B9B"> | </span>'));
}
}, 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'), '<span style="color:#EB9108">' + work + '</span>'));
}
}
}
// 请求图书馆联盟
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('<span class="pl">中图分类:</span> <span style="color:#989B9B">查无此信息</span><br/>');
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('; <a id="clc_' + clc.replace('.', '') + '" target="_blank" href="https://www.clcindex.com/category/' + clc + '">'
+ clc + '</a> <span id="clcText_' + clc.replace('.', '') + '">(...)</span>');
requestClc(clc);
} else {
$('#info').append('<span class="pl">中图分类:</span> <span id="clc" style="display: none;">' + clcsText + '</span><a id="clc_' + clc.replace('.', '') + '" target="_blank" href="https://www.clcindex.com/category/' + clc + '">'
+ clc + '</a> <span id="clcText_' + clc.replace('.', '') + '" class="clcText">(...)</span><br>');
requestClc(clc);
}
}
}
}
}
}, function(err, meta) {
$('#info').append('<span class="pl">中图分类:</span> <span style="color:#989B9B">查无此信息</span><br/>');
});
}
}
}, function(err, meta) {
$('#info').append('<span class="pl">中图分类:</span> <span style="color:#989B9B">查无此信息</span><br/>');
});
}
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('<span class="pl">更多版本:</span> <span style="color:#989B9B">查无此信息</span><br>');
return;
}
$('#info').append('<span class="pl">更多版本:</span> ');
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('<a target="_blank" href="' + item.url + '"><img style="width: 16px;height: 16px;" src="' + item.pic + '" />' + item.title + '(' + item.year + ', <span id="item__' + item.id + '">...</span>)' + '</a><span style="color:#989B9B"> | </span>');
}
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('<a target="_blank" href="https://search.douban.com/book/subject_search?search_text=' + trimTitle + '&cat=1001">详情</a><br>');
}, function(err, meta) {
$('#info').append('<span class="pl">更多版本:</span> <a target="_blank" href="https://search.douban.com/book/subject_search?search_text=' + trimTitle + '&cat=1001">详情...</a><br>');
});
}
// 请求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('<span style="color:#989B9B"> ▸ </span>') + ')');
}
} else {
if (clcText.text() == '(...)' ) {
clcText.html('<span style="color:#989B9B">(查无此信息)</span>');
}
}
}
function hanldeClcText(txt) {
var splits = txt.split(/、|(|)|\(|\)/);
var retss = [];
for (var s of splits) {
if (s.length == 0) continue;
retss.push(' <a target="_blank" href="http://xkfl.xhma.com/search?w=' + s + '">' + s + '</a>');
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('<span style="color:#989B9B">(无法获取)</span>');
}
});
}
// 请求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(' <a target="_blank" href="' + spans[0].nextElementSibling.href + '">' + code + ' ' + name + '</a>');
}
} 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('<a target="_blank" href="' + span.nextElementSibling.href + '">' + code + ' ' + name + '</a>');
}
}
}, function(err, meta) {
});
}
function appendsubject(subject) {
let clcText = $('#subjectText');
if (clcText.length == 0) {
$('#info').append('<span class="pl">学科分类参考:</span> <span id="subjectText"><span>' + subject + '</span></span>');
} else {
clcText.append('<span style="color:#989B9B"> | </span><span>' + subject + '</span>');
}
}
// 判断两个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(/<script[^>]*?>[\S\s]*?<\/script>/ig, ''); //页面脚本
return (new DOMParser()).parseFromString(responseText, 'text/html');
}
// 加载网页
function loadDoc (url, meta, callback, fail) {
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function (responseDetail) {
if (responseDetail.status === 200) {
let doc = page_parser(responseDetail.responseText)
callback(doc, responseDetail, meta)
} else if (fail){
fail(responseDetail, meta);
}
},
onerror: function(err) {
if (fail) {
fail(err, meta);
}
}
})
}
// get请求
function doGet (url, meta, callback, fail) {
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function (responseDetail) {
if (responseDetail.status === 200) {
callback(JSON.parse(responseDetail.responseText), responseDetail, meta)
} else if (fail){
fail(responseDetail, meta);
}
},
onerror: function(err) {
if (fail) {
fail(err, meta);
}
}
})
}
// post请求
function doPost (url, headers, data, meta, callback, fail) {
GM_xmlhttpRequest({
method: "POST",
url: url,
data: data,
headers: headers,
onload: function(responseDetail){
if (responseDetail.status === 200) {
callback(JSON.parse(responseDetail.responseText), responseDetail, meta)
} else if (fail){
fail(responseDetail, meta);
}
},
onerror: function(err) {
if (fail) {
fail(err, meta);
}
}
})
}
})()
// 测试URL:
// https://book.douban.com/subject/26431646/
// https://book.douban.com/subject/26298597/
// https://book.douban.com/subject/5402711/