-
Notifications
You must be signed in to change notification settings - Fork 1
/
知了.user.js
343 lines (313 loc) · 14 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
// ==UserScript==
// @name 知了
// @description 通过国家哲学社会科学文献中心下载
// @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://kns.cnki.net/kns8/defaultresult/index
// @include https://kns.cnki.net/kcms/detail/detail.aspx*
// @version 0.1.1
// @icon https://kns.cnki.net/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 (!/kns.cnki.net/.test(location.host)) {
return
}
;(function () {
'use strict';
$(document).ready(function () {
if (location.href.includes('https://kns.cnki.net/kcms/detail/detail.aspx')) {
detail();
} else if (location.href.includes('https://kns.cnki.net/kns8/defaultresult/index')) {
defaultresult();
}
})
function detail() {
var name = $('div.main div.container div.brief h1').text();
var cbw_name = $('.top-tip a:nth-child(1)').text();
var authors = [];
$('#authorpart a').each(function(){
authors.push($(this).text());
});
if (name && name.length > 0) {
ncpssd(name, authors, cbw_name, function(meta, searchurl, readurl, pdfurl) {
var operate = $('.operate-btn');
if (operate) {
operate.append('<li class="btn-ncpssd"><a style="background-color: #d92129;" target="_blank" id="ncpssdSearch" name="ncpssdSearch" href="' + searchurl + '">搜索...</a></li>');
if (readurl) {
operate.append('<li class="btn-ncpssd"><a style="background-color: #d92129;" target="_blank" id="ncpssdDown" name="ncpssdDown" href="' + readurl + '">阅读全文</a></li>');
} else {
operate.append('<li class="btn-ncpssd"><a title="没找到资源" style="background-color: #aca6a6;" target="_blank" id="ncpssdDown" name="ncpssdDown" onclick="return false" href="javascript:void(0)">阅读全文</a></li>');
}
if (pdfurl) {
operate.append('<li class="btn-ncpssd"><a title="如果点击无法正常下载,请尝试右键「新标签打开」,或者「链接保存为」。" style="background-color: #d92129;" target="_blank" id="ncpssdDown" name="ncpssdDown" href="' + pdfurl + '&type=1">PDF下载</a></li>');
} else {
operate.append('<li class="btn-ncpssd"><a title="没找到资源" style="background-color: #aca6a6;" target="_blank" id="ncpssdDown" name="ncpssdDown" onclick="return false" href="javascript:void(0)">PDF下载</a></li>');
}
}
});
}
}
function defaultresult() {
var interval = setInterval(function() {
if ($('.done').length > 0) return;
$('.result-table-list').addClass('done');
var tr = $('#gridTable table tbody tr');
if (tr.length > 0) {
if ($('.th-ncpssd').length === 0) {
$('.result-table-list>thead>tr').append('<th class="th-ncpssd" style="width: 90px;">知了</th>');
}
tr.each(function(){
let name = $(this).find('.name a').text().replace(/ |\n/g, '');
let authors = $(this).find('.author').text().replace(' ', '').split(';');
let cbw_name = $(this).find('.source').text().replace(/ |\n/g, '');
let cbItem = $(this).find('input.cbItem').val().replace(/!/g, '');
let cls = 'ncpssd-' + cbItem;
$(this).append('<td class="' + cls + '">...</td>');
ncpssd(name, authors, cbw_name, function(meta, searchurl, readurl, pdfurl) {
var td = $('.' + meta.item);
td.html('<a title="Ncpssd搜索" href="' + searchurl + '" target="_blank" style="color: #d92129; padding: 0 2pt 0 0;">搜索</a>');
if (readurl) {
td.append('<a title="阅读全文" href="' + readurl + '" target="_blank" style="color: #d92129; padding: 0 2pt 0 0;">阅读</a>');
} else {
td.append('<span style="color: #9da1a4; padding: 0 2pt 0 0;">阅读</span>');
}
if (pdfurl) {
td.append('<a title="PDF下载:如果点击无法正常下载,请尝试右键「新标签打开」,或者「链接保存为」。" href="' + pdfurl + '" target="_blank" style="color: #d92129; padding: 0 2pt 0 0;">下载</a>');
} else {
td.append('<span style="color: #9da1a4; padding: 0 2pt 0 0;">下载</span>');
}
}, {item: cls});
});
}
}, 200);
}
function ncpssd(title, authors, cbw_name, callback, meta) {
var search0 = "(IKTE=\"" + title + "\" OR IKET=\"" + title + "\")";
var searchname = "题名=\"" + title + "\"";
var search = $.base64.encode(search0, 'utf8');
searchname = $.base64.encode(searchname, 'utf8');
var searchurl = 'http://www.ncpssd.org/Literature/articlelist.aspx?search=' + search + '&searchname=' + searchname + '&nav=0';
var data = '{"search":"' + search0.replace(/"/g, "\\\"") + '", "pageIndex":1, "pageSize":10, "order":"date|DESC"}';
doPost('http://www.ncpssd.org/ajax/SeachHandler.ashx?method=search', {}, data, Object.assign(meta || {}, {authors: authors, searchurl: searchurl, cbw_name: cbw_name}), function(json, responseDetail, meta) {
if (json.pages > 0) {
for (let r of json.result) {
if ((meta.authors.length > 0 && meta.authors.indexOf(r.creator_first) > -1) || meta.cbw_name === r.cbw_name) {
// get!
var readurl = 'http://www.ncpssd.org/Literature/readurl.aspx?id=' + r.id + '&type=1';
callback(meta, meta.searchurl, readurl, r.pdfurl);
return;
}
}
}
callback(meta, meta.searchurl);
}, function(err, meta) {
callback(meta, meta.searchurl);
});
}
// 判断,空返回空字符串
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);
}
}
})
}
})()
/*!
* jquery.base64.js 0.1 - https://github.com/yckart/jquery.base64.js
* Makes Base64 en & -decoding simpler as it is.
*
* Based upon: https://gist.github.com/Yaffle/1284012
*
* Copyright (c) 2012 Yannick Albert (http://yckart.com)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
* 2013/02/10
**/
;(function($) {
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
a256 = '',
r64 = [256],
r256 = [256],
i = 0;
var UTF8 = {
/**
* Encode multi-byte Unicode string into utf-8 multiple single-byte characters
* (BMP / basic multilingual plane only)
*
* Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
*
* @param {String} strUni Unicode string to be encoded as UTF-8
* @returns {String} encoded string
*/
encode: function(strUni) {
// use regular expressions & String.replace callback function for better efficiency
// than procedural approaches
var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
function(c) {
var cc = c.charCodeAt(0);
return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f);
})
.replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
function(c) {
var cc = c.charCodeAt(0);
return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f);
});
return strUtf;
},
/**
* Decode utf-8 encoded string back into multi-byte Unicode characters
*
* @param {String} strUtf UTF-8 string to be decoded back to Unicode
* @returns {String} decoded string
*/
decode: function(strUtf) {
// note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
function(c) { // (note parentheses for precence)
var cc = ((c.charCodeAt(0) & 0x0f) << 12) | ((c.charCodeAt(1) & 0x3f) << 6) | (c.charCodeAt(2) & 0x3f);
return String.fromCharCode(cc);
})
.replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
function(c) { // (note parentheses for precence)
var cc = (c.charCodeAt(0) & 0x1f) << 6 | c.charCodeAt(1) & 0x3f;
return String.fromCharCode(cc);
});
return strUni;
}
};
while(i < 256) {
var c = String.fromCharCode(i);
a256 += c;
r256[i] = i;
r64[i] = b64.indexOf(c);
++i;
}
function code(s, discard, alpha, beta, w1, w2) {
s = String(s);
var buffer = 0,
i = 0,
length = s.length,
result = '',
bitsInBuffer = 0;
while(i < length) {
var c = s.charCodeAt(i);
c = c < 256 ? alpha[c] : -1;
buffer = (buffer << w1) + c;
bitsInBuffer += w1;
while(bitsInBuffer >= w2) {
bitsInBuffer -= w2;
var tmp = buffer >> bitsInBuffer;
result += beta.charAt(tmp);
buffer ^= tmp << bitsInBuffer;
}
++i;
}
if(!discard && bitsInBuffer > 0) result += beta.charAt(buffer << (w2 - bitsInBuffer));
return result;
}
var Plugin = $.base64 = function(dir, input, encode) {
return input ? Plugin[dir](input, encode) : dir ? null : this;
};
Plugin.btoa = Plugin.encode = function(plain, utf8encode) {
plain = Plugin.raw === false || Plugin.utf8encode || utf8encode ? UTF8.encode(plain) : plain;
plain = code(plain, false, r256, b64, 8, 6);
return plain + '===='.slice((plain.length % 4) || 4);
};
Plugin.atob = Plugin.decode = function(coded, utf8decode) {
coded = String(coded).split('=');
var i = coded.length;
do {--i;
coded[i] = code(coded[i], true, r64, a256, 6, 8);
} while (i > 0);
coded = coded.join('');
return Plugin.raw === false || Plugin.utf8decode || utf8decode ? UTF8.decode(coded) : coded;
};
}(jQuery));