-
Notifications
You must be signed in to change notification settings - Fork 0
/
discuz-short-url.user.js
158 lines (141 loc) · 5.27 KB
/
discuz-short-url.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
// ==UserScript==
// @name Discuz Short URL
// @name:zh-cn Discuz 短链 URL
// @namespace https://github.com/Vinfall/UserScripts
// @version 1.2.2
// @author Vinfall
// @match *://*/?mod=forumdisplay*
// @match *://*/?mod=viewthread*
// @match *://*/forum.php?mod=forumdisplay*
// @match *://*/forum.php?mod=viewthread*
// @match *://*/home.php?mod=space&uid=*
// @match *://*/space-uid-*.html
// @match *://*/suid-*
// @exclude-match *://*/?*goto*
// @exclude-match *://*/?mod=redirect*
// @exclude-match *://*/*username=*
// @exclude-match *://*/admin.php?*
// @exclude-match *://*/forum.php?*goto*
// @exclude-match *://*/forum.php?mod=redirect*
// @grant none
// @run-at document-start
// @license Apache-2.0
// @description Show short URL on Discuz forums
// @description:zh-cn 在 Discuz 论坛自动转换短链 URL
// ==/UserScript==
// Other match URL not added
// *://*/forum-*-*
// *://*/forum-*-*.html
// *://*/forum/search.php?*
// *://*/forum/viewforum.php?f=*
// *://*/showforum-*.html
// TODO:
// - Support more fields (only thread is supported) like forum, space uid (suid)
// - Enumerate & Test all mod methods
// - Support page info in extra param
(function () {
'use strict';
// 检测 Discuz 版本
// const detectDiscuzVersion = () => {
// const metas = document.getElementsByTagName('meta');
// for (let i = 0; i < metas.length; i++) {
// const meta = metas[i];
// if (meta.name.toLowerCase() === 'generator') {
// const content = meta.content.toLowerCase();
// if (content.includes('discuz! x3.4')) {
// return 'x3.4';
// } else if (content.includes('discuz! x')) {
// return 'x';
// }
// }
// }
// return null;
// };
// const discuzVersion = detectDiscuzVersion();
// if (discuzVersion !== 'x3.4') {
// console.log('Discuz version is not x3.4, no conversion will be performed.');
// return;
// }
// 定义字典
const domainStyles = [
{
style: (protocol, domain, tid, page) => `${protocol}//${domain}/t${tid}-${page}-1`,
domains: ['keylol.com'],
},
{
style: (protocol, domain, tid, page) => `${protocol}//${domain}/thread-${tid}-${page}-1.html`,
domains: ['bbs.a9vg.com', 'bbs.3dmgame.com', 'game.ali213.net'],
},
];
const currentUrl = window.location.href;
const urlObj = new URL(currentUrl);
const protocol = urlObj.protocol;
const domain = urlObj.hostname;
// const domain = urlObj.host; // with port, e.g. iamdumb.admin:666
// suid-*
const suidMatch = currentUrl.match(/suid-(\d+)/);
if (suidMatch && suidMatch[1]) {
let newUrl = `${protocol}//${domain}?${suidMatch[1]}`;
window.location.replace(newUrl);
return;
}
// space-uid-*
const spaceUidMatch = currentUrl.match(/space-uid-(\d+)\.html/);
if (spaceUidMatch && spaceUidMatch[1]) {
let newUrl = `${protocol}//${domain}?${spaceUidMatch[1]}`;
window.location.replace(newUrl);
return;
}
// 解析 URL param
const params = new URLSearchParams(urlObj.search);
// 缩短 space uid, e.g. ?12345
if (params.get('uid')) {
const uid = params.get('uid');
let newUrl = `${protocol}//${domain}?${uid}`;
window.location.replace(newUrl);
return;
}
// 检测 pid 参数
// 检查 URL 末尾, e.g. forum.php?mod=viewthread&tid=123456&page=3#pid19912345
let pid = '';
const pidMatchInHash = currentUrl.match(/#pid(\d+)/);
if (pidMatchInHash) {
pid = pidMatchInHash[0]; // e.g. #pid19912345
}
// 检查 extra 参数, e.g. forum.php?mod=viewthread&tid=123456&page=3&extra=#pid19912345
const extra = params.get('extra') || '';
const pidMatchInExtra = extra.match(/#pid(\d+)/);
if (!pid && pidMatchInExtra) {
pid = pidMatchInExtra[0]; // e.g. extra=#pid19912345
}
// 检查 tid 参数是否存在
if (params.get('tid')) {
const protocol = urlObj.protocol;
const domain = urlObj.hostname;
const tid = params.get('tid');
// 如果没有 page 参数,则默认为 1
// TODO: 部分 Discuz 论坛会把页数信息放在 extra 里
const page = params.get('page') || 1;
let newUrl;
// 根据域名匹配字典中的 URL 样式
let matched = false;
for (const entry of domainStyles) {
if (entry.domains.some((d) => domain.includes(d))) {
newUrl = entry.style(protocol, domain, tid, page);
matched = true;
break;
}
}
// 如果没有匹配到,默认使用 thread-123456-1-1 格式
// if (!matched) {
// newUrl = `${protocol}//${domain}/thread-${tid}-${page}-1.html`;
// }
// 如果有 pid 参数,加到 newUrl 之后
if (newUrl && pid) {
newUrl += pid;
}
if (newUrl) {
window.location.replace(newUrl);
}
}
})();