MediaWiki:Gadget-autolink.js
外观
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
// 自动链接 [[wikilinks]], [external links] 和 {templates} (特别适用于 monobook.js
// 和类似页面); 改编自 [[Wikipedia:WikiProject User scripts/Scripts/Autolink]]
/* ---------------------------------------------------------------------------------------------- */
$(function () {
var autolinkCronoSpecial = mw.config.get('wgCanonicalSpecialPageName') == "Recentchanges" || /*
*/ mw.config.get('wgCanonicalSpecialPageName') == "Watchlist" || /*
*/ mw.config.get('wgCanonicalSpecialPageName') == "Recentchangeslinked" || /*
*/ mw.config.get('wgCanonicalSpecialPageName') == "Newpages" || /*
*/ mw.config.get('wgCanonicalSpecialPageName') == "Log" || /*
*/ mw.config.get('wgCanonicalSpecialPageName') == "Contributions";
// 启用此脚本的特殊时间线页面
if ((typeof(autolinkMySpecialPage) != "undefined"))
autolinkCronoSpecial = autolinkCronoSpecial || mw.config.get('wgPageName') == autolinkMySpecialPage;
if (mw.config.get('wgCanonicalNamespace') == "Special" && !autolinkCronoSpecial) {
return; // 在其他特殊页面中禁用
}
if (typeof(autolinkParseLink) == 'undefined') {
autolinkParseLink = true; // 在所有情况下解析链接,即使在差异页面中。当wikEd激活时禁用
}
/* ---------------------------------------- Variables ------------------------------------------- */
var autolinkDiff = location.href.indexOf('diff=') != -1;
// 判断是否在差异页面
var autolinkCrono = (mw.config.get('wgAction') == 'history' || autolinkCronoSpecial);
// 判断是否在历史页面
var autolinkEdit = ($.inArray(mw.config.get('wgAction'), ['edit' , 'submit']) !== -1);
// 判断是否在编辑或提交页面
var autolinkColor; // links color
var autolinkTargetDiv = [];
if (autolinkDiff) { // 在差异页面中
if (autolinkParseLink) { // 没有wikEd
autolinkTargetDiv[0] = $('.diff');
autolinkTargetDiv[1] = $('.firstrevisionheader');
autolinkColor = 'inherit'; // 无颜色链接
} else { // 有wikEd
autolinkTargetDiv[0] = $('.comment');
autolinkColor = ''; // 有颜色链接
}
} else if (autolinkCrono || autolinkEdit) { // 在评论中
autolinkTargetDiv[0] = $('.comment');
autolinkColor = ''; // 有颜色链接
} else { // 在代码区域中
autolinkTargetDiv[0] = $('source');
autolinkTargetDiv[1] = $('.javascript');
autolinkTargetDiv[2] = $('.source-javascript');
autolinkTargetDiv[3] = $('.css');
autolinkTargetDiv[4] = $('.source-css');
autolinkColor = ''; // coloured links
}
/* ------------------------------------------ Regex --------------------------------------------- */
var autolinkRegexTl;
var autolinkRegexURL;
var autolinkRegexURLinWcodeWoLabel;
var autolinkRegexSubstinWcodeWoLabel;
var autolinkRegexURLinWcodeWithLabel;
var autolinkRegexSubstinWcodeWithLabel;
var autolinkRegexOtherPages;
var autolinkRegexSubstOtherPages;
var autolinkRegexTemplate;
var autolinkRegexSubstTemplate;
var autolinkRegexWlink1;
var autolinkRegexSubstWlink1;
var autolinkRegexWlink2;
var autolinkRegexSubstWlink2;
var autolinkRegexInternalURL;
var autolinkRegexImportScript;
var autolinkActivateHTML = false;
/* ------------------------------------------------------------------------------------------ */
autolinkRegexTl = /([^{]{{2}\s*[Tt]l\|)([^[\]{}|:<>\n]+)/g;
// 用于 {{tl}}
autolinkRegexURL = /((?:[^"\[]|[^=]")(?:<span class="diffchange">)?)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)((?:<\/span>)?(?:<span class="diffchange">)?)([\w._\/\\~%\-+&#?!=()@:]*)((?:<\/span>)?(?:<span class="diffchange">)?)([\w._\/\\~%\-+&#?!=()@:]*)([^\]\n]*\])/gm;
autolinkRegexSubstinWcodeWoLabel = '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2$4$6">$2</a>$3<a class="external autolink" style="color:' + autolinkColor + '" href="$2$4$6">$4</a>$5<a class="external autolink" style="color:' + autolinkColor + '" href="$2$4$6">$6</a>$7';
// 差异页面中的外部链接,wikicode无标签
autolinkRegexURLinWcodeWithLabel = autolinkRegexURLinWcodeWoLabel;
autolinkRegexSubstinWcodeWithLabel = autolinkRegexSubstinWcodeWoLabel;
// 差异页面中的外部链接,wikicode有标签
autolinkRegexOtherPages = /((?:[^{]|^)\{{2}\s*(?:(?:[Ss][Uu][Bb][Ss][Tt]|[Mm][Ss][Gg]|[Mm][Ss][Gg][Nn][Ww])\s*:)?\s*)((?: ?[Tt]alk)?\s*(?:[Ss]pecial|[Ww](?:wikipedia|P)|[Tt]emplate|[Uu]ser|[Pp]roject|[Hh]elp|[Cc]ategory|[Ff]ile|[Mm]edia|[Mm]ediaWiki|[Mm]odule)?\s*:[^[\]{}|:<\n]+)(\||}{2})/gm;
autolinkRegexSubstOtherPages = '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$2">$2</a>$3<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$2$4$6">$4</a>$5<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$2$4$6">$6</a>$7';
// 差异页面中包含的其他页面
autolinkRegexTemplate = /((?:[^{]|^)\{{2}\s*(?:<\/span>)?\s*(?:<span class="diffchange">)?\s*(?:(?:[Ss][Uu][Bb][Ss][Tt]|[Mm][Ss][Gg]|[Mm][Ss][Gg][Nn][Ww])\s*:)?\s*(?:<\/span>)?\s*(?:<span class="diffchange">)?)([^[\]{}|:<>\n]+)((?:<\/span>)?(?:<span class="diffchange">)?)([^[\]{}|:<>\n]*)((?:<\/span>)?(?:<span class="diffchange">)?)([^[\]{}|:<>\n]*)(\||}{2}|:)/gm;
autolinkRegexSubstTemplate = '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2$4$6">$2</a>$3<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2$4$6">$4</a>$5<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2$4$6">$6</a>$7';
// 差异页面中的模板
autolinkRegexWlink1 = /(\[{2}\s*(?:<\/span>)?\s*(?:<span class="diffchange">)?\s*)([^[\]{}|<>\n]+)((?:<\/span>)?(?:<span class="diffchange">)?)([^[\]{}|<>\n]*)((?:<\/span>)?(?:<span class="diffchange">)?)([^[\]{}|<>\n]*)((?:[^\]\n]|\][^\]])*\]{2})/g;
autolinkRegexSubstWlink1 = '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$2$4$6">$2</a>$3<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$2$4$6">$4</a>$5<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$2$4$6">$6</a>$7';
// 差异页面中的Wiki链接
autolinkRegexWlink2 = autolinkRegexWlink1;
autolinkRegexSubstWlink2 = autolinkRegexSubstWlink1;
// 用于评论或代码区域的正则表达式
if (!autolinkDiff || (autolinkDiff && !autolinkParseLink)) {
autolinkRegexURLinWcodeWoLabel = /([^\[]|^)\[\s*((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)\s*\]/gm;
autolinkRegexSubstinWcodeWoLabel = '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>';
// 评论或代码区域中的外部链接,wikicode无标签
autolinkRegexURLinWcodeWithLabel = /([^\[]|^)\[\s*((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)\s+([^\n]+?)\s*\]/gm;
autolinkRegexSubstinWcodeWithLabel = '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$3</a>';
// 评论或代码区域中的外部链接,wikicode有标签 (URL将不可见)
autolinkRegexOtherPages = /((?:[^{]|^)\{{2}\s*(?:(?:[Ss][Uu][Bb][Ss][Tt]|[Mm][Ss][Gg]|[Mm][Ss][Gg][Nn][Ww])\s*:)?\s*)((?: ?[Tt]alk)?\s*(?:[Ss]pecial|[Ww](?:wikipedia|P)|[Tt]emplate|[Uu]ser|[Pp]roject|[Hh]elp|[Cc]ategory|[Ff]ile|[Mm]edia|[Mm]ediaWiki|[Mm]odule)?\s*:[^[\]{}|:<\n]+)(\||}{2})/gm;
autolinkRegexSubstOtherPages = '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$2">$2</a>$3';
// 评论或代码区域中包含的其他页面
autolinkRegexTemplate = /((?:[^{]|^)\{{2}\s*(?:(?:[Ss][Uu][Bb][Ss][Tt]|[Mm][Ss][Gg]|[Mm][Ss][Gg][Nn][Ww])\s*:)?\s*)([^[\]{}|:<>\n]+)(\||}{2}|:)/gm;
autolinkRegexSubstTemplate = '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2">$2</a>$3';
// 评论或代码区域中的模板
autolinkRegexWlink1 = /\[{2}\s*([^[\]{}|<>\n]+?)\s*\|\s*(.+?)\s*\]{2}/g;
autolinkRegexSubstWlink1 = '<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$1">$2</a>';
// 代码区域中的Wiki链接,有标签
autolinkRegexWlink2 = /\[{2}\s*([^[\]{}|<>\n]+?)\s*\|?\s*\]{2}/g;
autolinkRegexSubstWlink2 = '<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$1">$1</a>';
// 代码区域中的Wiki链接,无标签
autolinkActivateHTML = true;
// 激活一些HTML(内联)和wikicode用于粗体和斜体
}
autolinkRegexInternalURL = /([^=])('|"|\s)((?:\/?w\/index\.php\?|\/?wiki\/)[\w._\/\\~%\-+&#?!=()@:]+)\2/g;
// 外部链接 (无wikicode,在wgServer下)
autolinkRegexImportScript = /([Ii]mport[Ss]cript(?:<span class="br0">)?\((?:<\/span><span class="st0">)?)('|")([^[\]{}|<>\n]+?)(\2(?:<\/span><span class="br0">)?\)(?:<\/span>)?)/g;
// ImportScript函数
/* ---------------------------------------------------------------------------------------------- */
for (i=0; i<autolinkTargetDiv.length; i++) {
for (j=0; j<autolinkTargetDiv[i].length; j++) {
var autolinkContent = autolinkTargetDiv[i][j].innerHTML;
autolinkContent = autolinkContent.replace(/</g, '­<­');
autolinkContent = autolinkContent.replace(/>/g, '­>­');
// &lt; 转换为 &shy;<&shy; 和 &gt; 转换为 &shy;>&shy; (&shy; 是一个标记)
autolinkContent = autolinkContent.replace(/&/g, '&');
// &amp; 转换为 &
/* ----------------------------------------------------------------------------------- */
autolinkContent = autolinkContent.replace(autolinkRegexTl, '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2">$2</a>');
// 对于 {{tl}}:将其参数转换为链接
autolinkContent = autolinkContent.replace(autolinkRegexURL, '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>');
// 解析非活跃的外部链接 (无wikicode)
autolinkContent = autolinkContent.replace(autolinkRegexURLinWcodeWoLabel, autolinkRegexSubstinWcodeWoLabel);
// 将wikicode中无标签的外部链接转换为链接
autolinkContent = autolinkContent.replace(autolinkRegexURLinWcodeWithLabel, autolinkRegexSubstinWcodeWithLabel);
// 将wikicode中有标签的外部链接转换为链接
autolinkContent = autolinkContent.replace(autolinkRegexOtherPages, autolinkRegexSubstOtherPages);
// 将包含其他页面的代码转换为链接
autolinkContent = autolinkContent.replace(autolinkRegexTemplate, autolinkRegexSubstTemplate);
autolinkContent = autolinkContent.replace(/href\="\/wiki\/Template\:#/g, 'href="/wiki/Help:');
// 将模板代码转换为链接
autolinkContent = autolinkContent.replace(autolinkRegexWlink1, autolinkRegexSubstWlink1);
autolinkContent = autolinkContent.replace(autolinkRegexWlink2, autolinkRegexSubstWlink2);
// 将wikilink代码转换为链接
autolinkContent = autolinkContent.replace(autolinkRegexInternalURL, '$1$2<a class="external autolink" style="color:' + autolinkColor + '" href="' + mw.config.get('wgServer') + '$3">$3</a>$2');
// 解析非活跃的外部链接 (无wikicode,在wgServer下)
autolinkContent = autolinkContent.replace(autolinkRegexImportScript, '$1$2<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$3">$3</a>$4');
// 解析ImportScript
/* ----------------------------------------------------------------------------------- */
autolinkContent = autolinkContent.replace(/­<­/g, '<');
autolinkContent = autolinkContent.replace(/­>­/g, '>');
// &shy;<&shy; 转换为 &lt; 和 &shy;>&shy; 转换为 &gt; (恢复)
if (autolinkActivateHTML) {
autolinkContent = autolinkContent.replace(/<(span|b|i|strong|small|tt|del|s|u|sub|sup)>(.*?)<\/(\1)>/g, '<$1>$2</$3>');
autolinkContent = autolinkContent.replace(/([^']|^)'{3}(.+?)'{3}([^']|$)/gm, '$1<b>$2</b>$3');
autolinkContent = autolinkContent.replace(/([^']|^)'{2}(.+?)'{2}([^']|$)/gm, '$1<i>$2</i>$3');
if (autolinkCrono || autolinkEdit)
autolinkContent = autolinkContent.replace(/<i>(.*?)<\/i>/g, '<span title="italic" style="border: 1px solid silver; padding: 2px;">$1</span>');
// 我在评论字段中 (斜体)
}
if (autolinkDiff && autolinkParseLink) {
autolinkContent = autolinkContent.replace(/<a [^>]+><\/a>/g, ''); // 清理
autolinkContent = autolinkContent.replace(/([^\[]|^)\[\s*(<a [^>]+>)(?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+(<\/a>)\s+([^\]\n]+)\]/gm, '$1$2$4$3');
autolinkContent = autolinkContent.replace(/([^\[]|^)\[\s*(<a [^>]+>)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)(<\/a>)\s*\]/gm, '$1$2$3$4');
}
autolinkTargetDiv[i][j].innerHTML = autolinkContent; // Write it back
}
}
});
// [[Category:Wikipedia scripts|Autolink]]