-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormal.js
163 lines (146 loc) · 5.22 KB
/
normal.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
$(function () {
var deleteTagList = ['BIG', 'I', 'B'];
var separatorList = ['—', '-'];
var workPageList = ['talk'];
var findImage = function (node) {
if (node.tagName == 'IMG') {
deleteImages(node);
}
var child = node.firstChild;
while (child) {
// 防止检查到其他讨论条
if (child.tagName != 'DL') {
findImage(child);
}
child = child.nextSibling;
}
}
var deleteImages = function (image) {
// if (image.height >= 22.4) {
// if (image.parentNode.tagName == 'SPAN' || image.parentNode.tagName == 'DIV') {
// image.parentNode.remove();
// }
// else {
image.remove();
// }
// }
}
var deleteTag = function (node) {
var child = node.firstChild;
while (child) {
node.parentNode.insertBefore(child, node);
child = child.nextSibling;
}
node.remove();
}
var findSpecialTag = function (node) {
var makeDeleteTag = false;
deleteTagList.forEach((value) => { if (node.tagName == value) makeDeleteTag = true; })
var temp = node.parentNode, child;
if (makeDeleteTag) {
deleteTag(node);
child = temp.firstChild;
} else {
child = node.firstChild;
}
while (child) {
findSpecialTag(child);
child = child.nextSibling;
}
}
var clean = function (node) {
// 将给定元素及其所有子元素的class和style全部清除
if (node.nodeType != 3) {
node.removeAttribute("style");
node.removeAttribute("class");
if (node.tagName != 'DL') {
var child = node.firstChild;
while (child) {
clean(child);
child = child.nextSibling;
}
}
}
}
var isUserLink = function (node) {
// 检查所有子元素,查找是否有用户链接
if (node.tagName == 'A' && (node.title.search('用户') != -1 || node.title.search(/user/gi) != -1)) {
return true;
}
var child = node.firstChild;
while (child) {
// 如果tag是DL不继续向下检索,避免检索到其他讨论条
if (child.tagName != 'DL' && isUserLink(child)) {
return true;
}
child = child.nextSibling;
}
return false;
}
var cleanAllUserLink = function (node) {
var child = node.firstChild;
while (child) {
if (isUserLink(child)) {
clean(child);
}
child = child.nextSibling;
}
}
var cleanFromSeparator = function (node) {
var allClean = false;
var child = node.firstChild;
while (child) {
// 如果满足下述条件,任何情况都清理触发条件之后的任何元素
if (child.nodeType == 3) {
separatorList.forEach((value) => { if (child.nodeValue.lastIndexOf(value) != -1) allClean = true; })
}
// 如果已发现分隔符,那么清除所有样式。
if (allClean) {
clean(child);
}
child = child.nextSibling;
}
if (allClean == false) {
// 如果不加分割符,就清除所有用户链接的样式
cleanAllUserLink(node);
}
}
var DealSignNode = function (node) {
// 如果时间戳在span标签内,清除span标签及子元素的任何样式。
// if (node.parentNode.tagName == 'SPAN') {
// clean(node.parentNode);
// }
// 获取时间戳外文本上层的子元素。(防止时间戳被套Span标签)
var parent = node.parentNode;
while (parent.nodeType != 1 || (parent.tagName != 'P' && parent.tagName != 'DD'
&& parent.tagName != 'CITE' && parent.tagName != 'DIV')) {
parent = parent.parentNode;
}
findImage(parent);
cleanFromSeparator(parent);
findSpecialTag(parent);
}
var checkAllElement = function (node) {
var regex = /[1-9]\d{3}年(?:0?[1-9]|1[012])月(?:0?[1-9]|[12]\d|3[01])日 *(?:[((](?:[金木水火土日月]|(?:星期)?[一二三四五六日])[))])? *(?:[01]\d|2[0-3]):(?:[0-5]\d)(?::[0-5]\d)? *[((]([CJ]ST|UTC(?:[+-](?:[1-9]|1[012]))?)[))]/g;
if (node.nodeType == 3) {
// 如果找到时间戳就进行签名样式处理
if (node.nodeValue.match(regex)) {
DealSignNode(node);
}
}
else {
// 不需要的元素就继续向下检查
var child = node.firstChild;
while (child) {
checkAllElement(child);
child = child.nextSibling;
}
}
};
var start = false;
workPageList.forEach((value) => { if (mw.config.values['wgPageName'].search(value) != -1) start = true; })
if (start) {
console.log('[讨论页简洁化] 已加载');
checkAllElement(document.getElementsByClassName('mw-parser-output')[0]);
}
});