User:Vesekskiy/CharMark.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
mw.loader.using(['mediawiki.util']).then(function () {
let enabled = false;
const markRules = [
{ text: "......", bg: "red", title: "六个半角句号" },
{ text: "...", bg: "orange", title: "三个半角句号" },
{ text: "……", bg: "lightgreen", title: "省略号" },
{ text: "——", bg: "lightgreen", title: "破折号" },
//↑多个字符
//↓单个字符
{ text: ",", bg: "lightblue", title: "U+002C\nComma\n半角逗号" },
{ text: "﹐", bg: "red", title: "U+FE50\nSmall Comma\n小型逗号" },
{ text: "。", bg: "red", title: "U+FF61\nHalfwidth Ideographic Full Stop\n半角表意文字句号" },
{ text: ".", bg: "lightblue", title: "U+002E\nFull Stop\n半角句号" },
{ text: "﹒", bg: "red", title: "U+FE52\nSmall Full Stop\n小型句号" },
{ text: ".", bg: "red", title: "U+FF0E\nFullwidth Full Stop\n全角句号" },
{ text: "․", bg: "red", title: "U+2024\nOne Dot Leader" },
{ text: "!", bg: "orange", title: "U+0021\nExclamation Mark\n半角感叹号" },
{ text: "﹗", bg: "red", title: "U+FE57\nSmall Exclamation Mark\n小型感叹号" },
{ text: "?", bg: "orange", title: "U+003F\nQuestion Mark\n半角问号" },
{ text: "﹖", bg: "red", title: "U+FE56\nSmall Question Mark\n小型问号" },
{ text: ";", bg: "orange", title: "U+003B\nSemicolon\n半角分号" },
{ text: "﹔", bg: "red", title: "U+FE54\nSmall Semicolon\n小型分号" },
{ text: ":", bg: "orange", title: "U+003A\nColon\n半角冒号" },
{ text: "﹕", bg: "red", title: "U+FE55\nSmall Colon\n小型冒号" },
{ text: "∶", bg: "lightblue", title: "U+2236\nRatio\n比" },
{ text: "⁚", bg: "red", title: "U+205A\nTwo Dot Punctuation" },
{ text: "'", bg: "orange", title: "U+0027\nApostrophe\n撇号" },
{ text: "'", bg: "red", title: "U+FF07\nFullwidth Apostrophe\n全角撇号" },
{ text: "′", bg: "lightblue", title: "U+2032\nPrime" },
{ text: "\"", bg: "orange", title: "U+0022\nQuotation Mark\n引号" },
{ text: "″", bg: "lightblue", title: "U+2033\nDouble Prime" },
{ text: "(", bg: "orange", title: "U+0028\nLeft Parenthesis\n左圆括号" },
{ text: ")", bg: "orange", title: "U+0029\nRight Parenthesis\n右圆括号" },
{ text: "﹙", bg: "red", title: "U+FE59\nSmall Left Parenthesis\n小型左圆括号" },
{ text: "﹚", bg: "red", title: "U+FE5A\nSmall Right Parenthesis\n小型右圆括号" },
{ text: "❨", bg: "red", title: "U+2768\nMedium Left Parenthesis Ornament" },
{ text: "❩", bg: "red", title: "U+2769\nMedium Right Parenthesis Ornament" },
{ text: "〈", bg: "orchid", title: "U+3008\nLeft Angle Bracket" },
{ text: "〉", bg: "orchid", title: "U+3009\nRight Angle Bracket" },
{ text: "❬", bg: "red", title: "U+276C\nMedium Left-Pointing Angle Bracket Ornament" },
{ text: "❭", bg: "red", title: "U+276D\nMedium Right-Pointing Angle Bracket Ornament" },
{ text: "⟨", bg: "red", title: "U+27E8\nMathematical Left Angle Bracket" },
{ text: "⟩", bg: "red", title: "U+27E9\nMathematical Right Angle Bracket" },
{ text: "⟪", bg: "red", title: "U+27EA\nMathematical Left Double Angle Bracket" },
{ text: "⟫", bg: "red", title: "U+27EB\nMathematical Right Double Angle Bracket" },
{ text: "╱", bg: "red", title: "U+2571\nBox Drawings Light Diagonal Upper Right To Lower Left\n方框绘制字符细斜线右上至左下" },
{ text: "丶", bg: "red", title: "U+4E36\nCJK Unified Ideograph-4E36\n中日韩统一表意文字-4E36" },
{ text: "﹑", bg: "red", title: "U+FE51\nSmall Ideographic Comma\n小型顿号" },
{ text: "、", bg: "red", title: "U+FF64\nHalfwidth Ideographic Comma\n半角顿号" },
{ text: "·", bg: "lightgreen", title: "U+00B7\nMiddle Dot\n中点" },
{ text: "•", bg: "red", title: "U+2022\nBullet\n项目符号" },
{ text: "‧", bg: "red", title: "U+2027\nHyphenation Point\n连字点" },
{ text: "・", bg: "orange", title: "U+30FB\nKatakana Middle Dot\n片假名中点" },
{ text: "・", bg: "red", title: "U+FF65\nHalfwidth Katakana Middle Dot\n半角片假名中点" },
{ text: "∙", bg: "red", title: "U+2219\nBullet Operator" },
{ text: "⋅", bg: "lightblue", title: "U+22C5\nDot Operator" },
{ text: "⸱", bg: "red", title: "U+2E31\nWord Separator Middle Dot" },
{ text: "⸳", bg: "red", title: "U+2E33\nRaised Dot" },
{ text: "ꞏ", bg: "red", title: "U+A78F\nLatin Small Letter Sinological Dot" },
{ text: "~", bg: "orange", title: "U+007E\nTilde\n波浪号" },
{ text: "~", bg: "orange", title: "U+FF5E\nFullwidth Tilde\n全角波浪号" },
{ text: "〜", bg: "orange", title: "U+301C\nWave Dash" },
{ text: "-", bg: "orchid", title: "U+002D\nHyphen-Minus\n连字暨减号" },
{ text: "‐", bg: "orange", title: "U+2010\nHyphen\n连字号" },
{ text: "−", bg: "lightblue", title: "U+2212\nMinus Sign\n减号" },
{ text: "‒", bg: "lightblue", title: "U+2012\nFigure Dash" },
{ text: "–", bg: "orange", title: "U+2013\nEn Dash" },
{ text: "—", bg: "orchid", title: "U+2014\nEm Dash" },
{ text: "―", bg: "red", title: "U+2015\nHorizontal Bar" },
{ text: "⸺", bg: "red", title: "U+2E3A\nTwo-Em Dash" },
{ text: "-", bg: "red", title: "U+FF0D\nFullwidth Hyphen-Minus\n全角连字暨减号" },
{ text: "﹘", bg: "red", title: "U+FE58\nSmall Em Dash\n小型Em Dash" },
{ text: "﹣", bg: "red", title: "U+FE63\nSmall Hyphen-Minus\n小型连字暨减号" },
{ text: "─", bg: "red", title: "U+2500\nBox Drawings Light Horizontal\n方框绘制字符细横线" },
{ text: "ー", bg: "orange", title: "U+30FC\nKatakana-Hiragana Prolonged Sound Mark\n日语长音符" },
{ text: "ー", bg: "red", title: "U+FF70\nHalfwidth Katakana-Hiragana Prolonged Sound Mark\n半角日语长音符" },
{ text: "…", bg: "orange", title: "U+2026\nHorizontal Ellipsis\n水平省略号" },
{ text: "⋯", bg: "red", title: "U+22EF\nMidline Horizontal Ellipsis\n中线水平省略号" },
/*
{ text: "", bg: "", title: "U+\n\n" },
*/
];
for (let cp = 0x2E80; cp <= 0x2EF3; cp++) {
if (cp === 0x2E9A) continue;
markRules.push({
text: String.fromCodePoint(cp),
bg: "red",
title: "中日韩汉字部首补充 U+" + cp.toString(16).toUpperCase().padStart(4, "0")
});
}
for (let cp = 0x2F00; cp <= 0x2FD5; cp++) {
markRules.push({
text: String.fromCodePoint(cp),
bg: "red",
title: "康熙部首 U+" + cp.toString(16).toUpperCase().padStart(4, "0")
});
}
markRules.sort(function (a, b) {
return b.text.length - a.text.length;
});
function markSpecialChars(root) {
let walker = document.createTreeWalker(
root,
NodeFilter.SHOW_TEXT,
{
acceptNode(node) {
if (node.parentNode.closest(".marked-special-char")) {
return NodeFilter.FILTER_REJECT;
}
return NodeFilter.FILTER_ACCEPT;
}
}
);
let nodes = [];
while (walker.nextNode()) {
nodes.push(walker.currentNode);
}
nodes.forEach(function (node) {
let text = node.nodeValue;
let frag = document.createDocumentFragment();
let i = 0;
let changed = false;
while (i < text.length) {
let matchedRule = null;
for (let rule of markRules) {
if (text.substr(i, rule.text.length) === rule.text) {
matchedRule = rule;
break;
}
}
if (matchedRule) {
let span = document.createElement("span");
span.textContent = matchedRule.text;
span.style.backgroundColor = matchedRule.bg;
span.title = matchedRule.title;
span.className = "marked-special-char";
frag.appendChild(span);
i += matchedRule.text.length;
changed = true;
} else {
frag.appendChild(document.createTextNode(text[i]));
i++;
}
}
if (changed) {
node.parentNode.replaceChild(frag, node);
}
});
}
function unmarkSpecialChars() {
document.querySelectorAll(".marked-special-char").forEach(function (span) {
span.replaceWith(document.createTextNode(span.textContent));
});
}
function toggle() {
let root = document.getElementById("mw-content-text");
if (!enabled) {
markSpecialChars(root);
enabled = true;
} else {
unmarkSpecialChars();
enabled = false;
}
}
$(function () {
let link = mw.util.addPortletLink(
"p-cactions",
"#",
"标记特殊符号",
"ca-char-mark",
"标记相似符号并显示Unicode码位和字符名称"
);
$(link).on("click", function (e) {
e.preventDefault();
toggle();
});
});
});