User:逆襲的天邪鬼/NoPersonalSigns.js: Difference between revisions
Content added Content deleted
(Created page with "→消灭个性签名 by 逆襲的天邪鬼 TODO: 1. 该红链时红链 2. 防止冤杀: $(function () { var useBlacklist = false; // 若置为f...") |
(via Wikiplus) |
||
Line 1: | Line 1: | ||
/* |
/* |
||
消灭个性签名 |
消灭个性签名 by 逆襲的天邪鬼 |
||
用法: |
|||
by 逆襲的天邪鬼 |
|||
在[[Special:MyPage/common.js]]中加一句: |
|||
mw.loader.load('https://publictestwiki.com/w/index.php?title=User:逆襲的天邪鬼/NoPersonalSigns.js&action=raw&ctype=text/javascript'); |
|||
仅用来对付个别不喜欢配合的人,因此不保证程序正确运行。 |
|||
TODO: |
|||
1. 该红链时红链 |
|||
TODO: |
|||
1. 防止冤杀 |
|||
*/ |
*/ |
||
Line 35: | Line 38: | ||
name.match('^[0-9a-fA-F]+:[0-9a-fA-F:]+$') !== null; |
name.match('^[0-9a-fA-F]+:[0-9a-fA-F:]+$') !== null; |
||
}; |
}; |
||
var isNew = {}; |
|||
var findUserName = function (node) { |
var findUserName = function (node) { |
||
Line 42: | Line 47: | ||
var match = href.match('(title=|/)(User:|User_talk:|Special:Contributions\\/|Special:%E7%94%A8%E6%88%B7%E8%B4%A1%E7%8C%AE\\/)(.*?)($|&|#|\\/)'); |
var match = href.match('(title=|/)(User:|User_talk:|Special:Contributions\\/|Special:%E7%94%A8%E6%88%B7%E8%B4%A1%E7%8C%AE\\/)(.*?)($|&|#|\\/)'); |
||
if (match && match[3]) { |
if (match && match[3]) { |
||
var name = normalizeName(decodeURI(match[3])); |
|||
if (node.className.indexOf('new') !== -1) { |
|||
if (href.indexOf('User_talk:') !== -1) { |
|||
isNew['User talk:' + name] = true; |
|||
} else if (href.indexOf('User:') !== -1) { |
|||
isNew['User:' + name] = true; |
|||
} |
|||
} |
|||
return name; |
|||
} |
} |
||
} else { |
} else { |
||
Line 59: | Line 74: | ||
}; |
}; |
||
var |
var killSigns = function (node) { |
||
// 确定用户名 |
// 确定用户名 |
||
var curNode = node.previousSibling; |
var curNode = node.previousSibling; |
||
Line 87: | Line 102: | ||
// 格杀勿论 |
// 格杀勿论 |
||
var |
var nodes = []; |
||
curNode = lastValidNode; |
curNode = lastValidNode; |
||
while (curNode !== node) { |
while (curNode !== node) { |
||
nodes.push(curNode); |
|||
curNode = curNode.nextSibling; |
curNode = curNode.nextSibling; |
||
} |
} |
||
for (var i = 0; i< |
for (var i = 0; i<nodes.length; i++) { |
||
nodes[i].remove(); |
|||
} |
} |
||
Line 103: | Line 118: | ||
x.text = username; |
x.text = username; |
||
y.text = '留言'; |
y.text = '留言'; |
||
x. |
x.className = 'userlink'; |
||
y.className = 'userlink'; |
|||
x.href = '/wiki/User:' + username; |
|||
y.href = '/wiki/User_talk:' + username; |
y.href = '/wiki/User_talk:' + username; |
||
if (isNew['User:' + username]) { |
|||
x.className += ' new'; |
|||
} |
|||
if (isNew['User talk:' + username]) { |
|||
y.className += ' new'; |
|||
} |
|||
node.parentNode.insertBefore(x, node); |
node.parentNode.insertBefore(x, node); |
||
Line 141: | Line 165: | ||
}; |
}; |
||
replace_text(document.getElementById('bodyContent'), /(\d{4})年(\d{1,2})月(\d{1,2})日 \([一二三四五六日]\) (\d\d):(\d\d) \(UTC\)/g, |
replace_text(document.getElementById('bodyContent'), /(\d{4})年(\d{1,2})月(\d{1,2})日 \([一二三四五六日]\) (\d\d):(\d\d) \(UTC\)/g, killSigns); |
||
}); |
}); |
Revision as of 18:32, 30 April 2017
/*
消灭个性签名 by 逆襲的天邪鬼
用法:
在[[Special:MyPage/common.js]]中加一句:
mw.loader.load('https://publictestwiki.com/w/index.php?title=User:逆襲的天邪鬼/NoPersonalSigns.js&action=raw&ctype=text/javascript');
仅用来对付个别不喜欢配合的人,因此不保证程序正确运行。
TODO:
1. 防止冤杀
*/
$(function () {
var useBlacklist = false; // 若置为false则处理所有人的签名,否则只处理个别人签名。
var blacklist = [
'用户名'
];
// 如果能把名单存到localStorage那么最好
if (localStorage.signBlackList) {
blacklist = localStorage.signBlackList;
}
var normalizeName = function (name) {
return name.replace(/_/g, ' ');
};
if (blacklist) {
for (var i = 0; i < blacklist.length; i++) {
blacklist[i] = normalizeName(blacklist[i]).toLowerCase();
}
}
var isIP = function (name) {
return name.match('^\\d+\\.\\d+\\.\\d+\\.\\d+$') !== null ||
name.match('^[0-9a-fA-F]+:[0-9a-fA-F:]+$') !== null;
};
var isNew = {};
var findUserName = function (node) {
if (node !== null && node.nodeType === 1) {
if (node.tagName === 'A') {
var href = node.href;
var match = href.match('(title=|/)(User:|User_talk:|Special:Contributions\\/|Special:%E7%94%A8%E6%88%B7%E8%B4%A1%E7%8C%AE\\/)(.*?)($|&|#|\\/)');
if (match && match[3]) {
var name = normalizeName(decodeURI(match[3]));
if (node.className.indexOf('new') !== -1) {
if (href.indexOf('User_talk:') !== -1) {
isNew['User talk:' + name] = true;
} else if (href.indexOf('User:') !== -1) {
isNew['User:' + name] = true;
}
}
return name;
}
} else {
var child = node.firstChild;
while (child) {
var n = findUserName(child);
if (n !== null) {
return n;
}
child = child.nextSibling;
}
}
}
return null;
};
var killSigns = function (node) {
// 确定用户名
var curNode = node.previousSibling;
var lastValidNode = null;
var username = null;
while (curNode) {
var n = findUserName(curNode);
if (n !== null) {
if (username === null) {
username = n;
}
if (n.toLowerCase() === username.toLowerCase()) {
lastValidNode = curNode;
}
}
curNode = curNode.previousSibling;
}
// 判断是否为黑名单名字
if ((useBlacklist && blacklist.indexOf(username.toLowerCase()) === -1) || lastValidNode === null) {
return;
}
if (isIP(username)) {
return;
}
// 格杀勿论
var nodes = [];
curNode = lastValidNode;
while (curNode !== node) {
nodes.push(curNode);
curNode = curNode.nextSibling;
}
for (var i = 0; i<nodes.length; i++) {
nodes[i].remove();
}
// 换成干净的链接
var x = document.createElement('a');
var y = document.createElement('a');
x.text = username;
y.text = '留言';
x.className = 'userlink';
y.className = 'userlink';
x.href = '/wiki/User:' + username;
y.href = '/wiki/User_talk:' + username;
if (isNew['User:' + username]) {
x.className += ' new';
}
if (isNew['User talk:' + username]) {
y.className += ' new';
}
node.parentNode.insertBefore(x, node);
node.parentNode.insertBefore(document.createTextNode('('), node);
node.parentNode.insertBefore(y, node);
node.parentNode.insertBefore(document.createTextNode(')'), node);
if (node.textContent.indexOf(')') === 0) {
node.textContent = node.textContent.substr(1);
}
if (node.textContent.indexOf(')') === 0) {
node.textContent = node.textContent.substr(1);
}
};
var replace_text = function (node, search, callback) {
if (node.nodeType === 3)
{
value = node.nodeValue;
matches = value.match(search);
if (matches !== null) {
callback(node);
}
} else {
var children = [], child = node.firstChild;
while (child) {
children[children.length] = child;
child = child.nextSibling;
}
for (var i = 0; i < children.length; i++) {
replace_text(children[i], search, callback);
}
}
};
replace_text(document.getElementById('bodyContent'), /(\d{4})年(\d{1,2})月(\d{1,2})日 \([一二三四五六日]\) (\d\d):(\d\d) \(UTC\)/g, killSigns);
});