-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBIT-补足下载附件时的默认文件名.user.js
131 lines (121 loc) · 3.79 KB
/
BIT-补足下载附件时的默认文件名.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
// ==UserScript==
// @name BIT-补足下载附件时的默认文件名
// @namespace http://tampermonkey.net/
// @version 0.2.14
// @description 补充附件中 <a> 元素的 download 属性
// @license GPL-3.0-or-later
// @supportURL https://github.com/YDX-2147483647/BIT-enhanced/issues
// @author Y.D.X.
// @match https://jxzx.bit.edu.cn/*.htm
// @match https://jwb.bit.edu.cn/*.htm
// @match https://student.bit.edu.cn/*
// @match https://www.bit.edu.cn/*.htm
// @match https://opt.bit.edu.cn/*.htm
// @match https://mingde.bit.edu.cn/*.htm
// @match https://xuteli.bit.edu.cn/*.htm
// @match https://xcb.bit.edu.cn/*.htm
// @match https://grd.bit.edu.cn/*.htm
// @match https://sie.bit.edu.cn/*.htm
// @match https://cs.bit.edu.cn/*.htm
// @grant none
// ==/UserScript==
(function () {
'use strict'
// 优先使用在前面的 title_selector
const matches = [
{ // 教学中心
host: 'jxzx',
attachments_selector: '.pageArticle > .Annex > ul > li > a'
},
{ // 教务部
host: 'jwb',
attachments_selector: '.Annex > ul > li > a'
},
{ // World Wide Web
host: 'www',
attachments_selector: '.article .Annex > ul > li > a'
},
{ // Student
host: 'student',
attachments_selector: '.fujian > ul > li > a'
},
{ // Optics
host: 'opt',
attachments_selector: '.fujian > ul > li > a'
},
{ // 明德
host: 'mingde',
attachments_selector: '.pageArticle .Annex > ul > li > a'
},
{ // 徐特立
host: 'xuteli',
attachments_selector: '.annex > ul > li > a'
},
{ // 宣传部
host: 'xcb',
attachments_selector: '.rt_fujian > ul > li > a'
},
{ // Graduate
host: 'grd',
attachments_selector: '.gp-annex3 > ul > li > a'
},
{ // School of Information and Electronics
host: 'sie',
attachments_selector: '.annexList > li > a'
},
{ // Computer Science
host: 'cs',
attachments_selector: '.fujian > ul > li > a'
}
]
/**
* 猜测带有扩展名的文件名
*
* @param {string} text
* @param {string} url
* @returns {string}
*/
function guess_filename (text, url) {
const SUFFIX_PATTERN = /\.[0-9a-zA-Z]+$/
const match_text = SUFFIX_PATTERN.exec(text)
const suffix_by_text = match_text !== null ? match_text[0] : null
const match_url = SUFFIX_PATTERN.exec(url)
const suffix_by_url = match_url !== null ? match_url[0] : null
if (suffix_by_url === null) {
// 若`url`无更多信息,则照抄`text`
return text
} else {
// 若`url`提供了信息
if (suffix_by_text === null) {
// 若`text`缺扩展名,则加上扩展名
// 这是最常见的情况。
return text + suffix_by_url
} else {
// 若`text`也疑似有扩展名,则与`url`的比较一下,看情况决定是否加上
if (suffix_by_text === suffix_by_url) {
return text
}
if (suffix_by_text.match(/^\.[0-9]+$/) || (suffix_by_text.length < suffix_by_url)) {
// 例:`text`是`XeTeX-3.141592653-2.6-0.999996`,那么`suffix_by_text`会是`.999996`,并非真的扩展名。
return text + suffix_by_url
} else {
return text
}
}
}
}
function set_attachments_filenames () {
const site_host = window.location.host
for (const s of matches) {
if (site_host === `${s.host}.bit.edu.cn`) {
document.querySelectorAll(s.attachments_selector).forEach(attach => {
if (!attach.download) {
attach.download = guess_filename(attach.textContent, attach.href)
}
})
break
}
}
}
set_attachments_filenames()
})()