-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBIT-补足页面标题.user.js
234 lines (222 loc) · 6.03 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
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
// ==UserScript==
// @name BIT-补足页面标题
// @namespace http://tampermonkey.net/
// @version 1.2.12
// @description 修改页面标题
// @license GPL-3.0-or-later
// @supportURL https://github.com/YDX-2147483647/BIT-enhanced/issues
// @author Y.D.X.
// @match https://*.bit.edu.cn/*
// @match http://*.bit.edu.cn/*
// @match https://www.yanhekt.cn/*
// @exclude https://lexue.bit.edu.cn/*
// @exclude https://www.bit.edu.cn/xww/*
// @exclude https://webvpn.bit.edu.cn/*
// @exclude http://jxzxehallapp.bit.edu.cn/*
// @exclude https://jxzxehallapp.bit.edu.cn/*
// @grant none
// ==/UserScript==
(function () {
'use strict'
/**
* @typedef {object} Site
* @property {string} host 例:“jwb”“jwb.bit.edu.cn”
* @property {(string | { selector: string, post: (element: Element) => string? })[]} title_selectors 优先使用在前面的 selector
* @property {string?} name 默认从`<title>`获取
*/
/* spell-checker: disable */
/** @type {Site[]} */
const sites = [
{ // 教学中心
host: 'jxzx',
title_selectors: [
'.pageArticle > .articleTitle > h2',
'.pagelistTitle > h2'
]
},
{ // Information Technology Center
host: 'itc',
title_selectors: [
'.pageArticle > .pageArticleTitle > h3#shareTitle',
'body > div > div.subPage > div.sub_right > h2',
'body > div > div.subPage > div > h2'
]
},
{ // 教务处
host: 'jwc',
title_selectors: [
'.pageArticle > .aca_article > h2',
'.pageArticle > .articleTitle > h2'
]
},
{ // 教务部
name: '教务部',
host: 'jwb',
title_selectors: [
'h3#shareTitle',
'.gp-bread + h2.gpColumnTitle'
]
},
{ // World Wide Web
host: 'www',
title_selectors: [
'.subPage > .container > .subRight > .newsTitle > h1',
'.subPage > .container > .listTitle02 > h2',
'.bread > .container > a:nth-last-child(1)'
]
},
{ // I
host: 'i',
title_selectors: [
// 从首页进去,怎么单击都是页面内跳转,URL不变。想检测的话要一直运行这个脚本,干脆算了吧……
'.special-detail-banner > div > .special-detail-title > h2',
'.service-name',
'table.mini-tabs-header > tbody > tr > td.mini-tab-active > span.mini-tab-text'
]
},
{ // 学生事务中心
host: 'student',
title_selectors: [
'#home .page_box .txt > h2',
'#home .page_box .top > h2'
]
},
{ // 世纪(学生工作部、武装部、心理健康教育与咨询中心)
host: 'century',
title_selectors: [
'h2'
]
},
{ // 徐特立
host: 'xuteli',
title_selectors: [
'.articleTitle h2'
]
},
{ // 延河课堂
host: 'www.yanhekt.cn',
title_selectors: [
'.header-menu .ant-menu-item-selected',
'#root div[class^=secHeading] > span[class^=title]',
'[class^=liveHeader], [class^=mobileLiveHeader]',
'.title-bar > :last-child',
'.course-intro-title'
]
},
{ // 信息公开
host: 'xxgk',
title_selectors: [
'.pageArticleTitle h3',
'.gp-subRight .articleTitle02'
]
},
{ // 明德
host: 'mingde',
title_selectors: [
'.pageArticleTitle > h3',
'.gp-subRight .articleTitle02',
'.subPage h2'
]
},
{ // 校医院
host: 'xyy',
title_selectors: [
'.rt_tit > h1',
'.lt_tit > h2'
]
},
{ // School of Information and Electronics
host: 'sie',
title_selectors: [
'.pageArticle > .pageArticleTitle > h3#shareTitle',
'h2.listTitle',
'.gp-bread > .gp-container > a:last-child'
]
},
{ // Radar
host: 'radar',
title_selectors: [
{
selector: '.detailBox .name',
post: (el) => {
if (el.childNodes.length === 2 && el.childNodes[0].nodeType === Node.TEXT_NODE && el.childNodes[1].classList?.contains('gender')) {
return el.childNodes[0].textContent
}
}
},
'h3.gp-title',
'.bread > .gp-container > a:last-child'
]
}
]
/* spell-checker: enable */
function match_site () {
const site = sites.find(s => [
`${s.host}.bit.edu.cn`,
s.host
].includes(window.location.host))
if (site === undefined) {
return null
} else {
if (!site.name) {
site.name = document.title
.match(/(北京)?(理工)?(大学)?(?<name>.+)/)
.groups.name.trim()
.replace(/网$/, '')
}
return site
}
}
/**
* Applies function to the elements of the array and returns the first non-empty result.
*
* https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find_map
*
* @template T
* @template U
* @param {Array<T>} array
* @param {(element: T) => U?} fn
* @returns {U?}
*/
function find_map (array, fn) {
for (const element of array) {
const result = fn(element)
if (result) {
return result
}
}
}
/**
* @param {Site} site
* @returns success
*/
function change_title_for (site) {
const raw_title = find_map(site.title_selectors, s => {
if (typeof s === 'string') {
return document.querySelector(s)?.textContent
} else {
const el = document.querySelector(s.selector)
if (el) {
return s.post(el)
}
}
})
if (!raw_title) {
return false
}
const title = raw_title.replace(/^[>-]/, '').trim()
if (!title) {
return false
}
document.title = `${title} - ${site.name}${site.name ? ' |' : ''} 北京理工大学`
return true
}
const site = match_site()
if (site) {
const success = change_title_for(site)
if (!success) {
// 适应 I、延河课堂
setTimeout(() => change_title_for(site), 1500)
}
}
})()