User:Lenore/autolink.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Lenore/autolink. |
// Autolink [[wikilinks]], [external links] and {{templates}} (especially useful for monobook.js
// and similar pages); adapted from [[Wikipedia:WikiProject User scripts/Scripts/Autolink]]
/* ---------------------------------------------------------------------------------------------- */
addOnloadHook (function () {
var autolinkCronoSpecial = wgCanonicalSpecialPageName == "Recentchanges" || /*
*/ wgCanonicalSpecialPageName == "Watchlist" || /*
*/ wgCanonicalSpecialPageName == "Recentchangeslinked" || /*
*/ wgCanonicalSpecialPageName == "Newpages" || /*
*/ wgCanonicalSpecialPageName == "Log" || /*
*/ wgCanonicalSpecialPageName == "Contributions";
// Special crono pages where this script is enabled
if ((typeof(autolinkMySpecialPage) != "undefined"))
autolinkCronoSpecial = autolinkCronoSpecial || wgPageName == autolinkMySpecialPage;
if (wgCanonicalNamespace == "Special" && !autolinkCronoSpecial) {
return; // Disabled in the other special pages
}
if (typeof(autolinkParseLink) == 'undefined') {
autolinkParseLink = true; // Parse external links in all cases
}
/* ---------------------------------------- Variables ------------------------------------------- */
var autolinkCrono = location.href.indexOf('&action=history') != -1 || autolinkCronoSpecial;
// It says if I'm in a history page
var autolinkDiff = location.href.indexOf('&diff=') != -1;
// It says if I'm in a diff page
var autolinkEdit = location.href.indexOf('&action=edit') != -1 || location.href.indexOf('&action=submit') != -1;
// It says if I'm in an edit or submit page
var autolinkTargetDiv = [];
if (autolinkDiff) {
autolinkTargetDiv[0] = document.getElementsByClassName('diff');
autolinkTargetDiv[1] = document.getElementsByClassName('firstrevisionheader');
}
else if (autolinkCrono || autolinkEdit)
autolinkTargetDiv[0] = document.getElementsByClassName('comment');
else {
autolinkTargetDiv[0] = document.getElementsByTagName('pre');
autolinkTargetDiv[1] = document.getElementsByTagName('tt');
autolinkTargetDiv[2] = document.getElementsByTagName('code');
autolinkTargetDiv[3] = document.getElementsByTagName('source');
}
/* ------------------------------------------ Regex --------------------------------------------- */
var autolinkColor;
var autolinkRegexURLinWcodeWoLabel;
var autolinkRegexURLinWcodeWithLabel;
var autolinkRegexURL;
var autolinkRegexWlink;
var autolinkRegexOtherPages;
var autolinkRegexTl;
var autolinkRegexTemplate;
var autolinkRegexInternalURL;
var autolinkRegexImportScript;
var autolinkRegexCorrectNs;
autolinkRegexTl = /([^{]{{2}\s*[Tt]l\|)([^[\]{}|<>\n]+)/g;
// For {{tl}}
autolinkRegexTemplate = /((?:[^{]|^)\{{2}\s*(?:<\/span>)?\s*(?:<span class="diffchange">)?\s*(?:[Ss][Uu][Bb][Ss][Tt]:|[Mm][Ss][Gg]:|[Mm][Ss][Gg][Nn][Ww]:)?\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';
// Templates
autolinkRegexURL = /((?:[^"\[]|[^=]")(?:<span class="diffchange">)?)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+?)(?=(?:<\/span>)?[\s|)\]}'"])/g;
// External links (no wikicode)
if (autolinkCrono || autolinkEdit) {
autolinkColor = ''; // Link color in history pages
autolinkRegexURLinWcodeWoLabel = /([^\[])\[{1}\s*((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)\s*\]{1}/g;
autolinkRegexSubstinWcodeWoLabel = '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>';
// External links in history pages, wikicode without label
autolinkRegexURLinWcodeWithLabel = /([^\[])\[{1}\s*((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)\s+([^\]\n]+)\]{1}/g;
autolinkRegexSubstinWcodeWithLabel = '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$3</a>';
// External links in history pages, wikicode with label (the URL will not be visible)
autolinkRegexTemplate = /((?:[^{]|^)\{{2}\s*(?:[Ss][Uu][Bb][Ss][Tt]:|[Mm][Ss][Gg]:|[Mm][Ss][Gg][Nn][Ww]:)?\s*)([^[\]{}|<>\n]+)(\||}{2})/gm;
autolinkRegexSubstTemplate = '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2">$2</a>$3';
// Templates in history pages
} else {
autolinkColor = 'inherit'; // Link color in other pages
autolinkRegexURLinWcodeWoLabel = /(\[{1}\s*(?:<\/span>)?\s*(?:<span class="diffchange">)?\s*)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)((?:<\/span>)?(?:<span class="diffchange">)?)([\w._\/\\~%\-+&#?!=()@:]*)((?:<\/span>)?(?:<span class="diffchange">)?)([\w._\/\\~%\-+&#?!=()@:]*)([^\]\n]*\]{1})/g;
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';
// External links in other pages, wikicode without label
autolinkRegexURLinWcodeWithLabel = autolinkRegexURLinWcodeWoLabel;
autolinkRegexSubstinWcodeWithLabel = autolinkRegexSubstinWcodeWoLabel;
// External links in other pages, wikicode with label
}
autolinkRegexWlink = /(\[{2}\s*(?:<\/span>)?\s*(?:<span class="diffchange">)?\s*)([^[\]{}|<>\n]+)((?:<\/span>)?(?:<span class="diffchange">)?)([^[\]{}|<>\n]?)((?:<\/span>)?(?:<span class="diffchange">)?)([^[\]{}|<>\n]?)((?:[^\]\n]|\][^\]])*\]{2})/g;
autolinkRegexSubstWlink = '$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';
// Wikilinks
autolinkRegexInternalURL = /([^=])('|"|\s)((?:\/?w\/index\.php\?|\/?wiki\/)[\w._\/\\~%\-+&#?!=()@:]+)\2/g;
// External links (no wikicode, under wgServer)
autolinkRegexImportScript = /([Ii]mport[Ss]cript(?:<span class="br0">)?\((?:<\/span><span class="st0">)?)('|")([^[\]{}|<>\n]+?)(\2(?:<\/span><span class="br0">)?\)(?:<\/span>)?)/g;
// ImportScript
autolinkRegexCorrectNs = /(href="\/wiki\/)Template(:(?:[Dd]iscussion[ei] ?)?(?:[Ss]peciale?|[Ww](?:ikipedia|P)|[Tt]emplate|[Uu](?:ser|tente)|[Pp]ortale?|[Pp]rogetto)?(?: ?[Tt]alk)?")/g;
// Correct link to Template:Namespace
/* ---------------------------------------------------------------------------------------------- */
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, '­>­');
// < to ­<­ and > to ­>­ (­ is needed to not activate HTML)
autolinkContent = autolinkContent.replace(/&/g, '&');
// & to &
autolinkContent = autolinkContent.replace(autolinkRegexTl,
'$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2">$2</a>');
// For {{tl}}: make his argument into link
if (autolinkParseLink == true || !autolinkDiff) {
// Qui vanno tutte le sostituzioni fatte anche dal wikEd nelle diff
autolinkContent = autolinkContent.replace(autolinkRegexURLinWcodeWoLabel,
autolinkRegexSubstinWcodeWoLabel);
// Make external links in wikicode without label into links
autolinkContent = autolinkContent.replace(autolinkRegexURLinWcodeWithLabel,
autolinkRegexSubstinWcodeWithLabel);
// Make external links in wikicode with label into links
autolinkContent = autolinkContent.replace(autolinkRegexURL,
'$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>');
// Parse inactive external links (no wikicode)
autolinkContent = autolinkContent.replace(autolinkRegexWlink,
autolinkRegexSubstWlink);
// Make wikilink code into links
autolinkContent = autolinkContent.replace(autolinkRegexTemplate,
autolinkRegexSubstTemplate);
autolinkContent = autolinkContent.replace(/href\="\/wiki\/Template\:#/g, 'href="/wiki/Help:');
// Make template code into links
}
autolinkContent = autolinkContent.replace(autolinkRegexInternalURL,
'$1$2<a class="external autolink" style="color:' + autolinkColor + '" href="' + wgServer + '$3">$3</a>$2');
// Parse inactive external links (no wikicode, under wgServer)
autolinkContent = autolinkContent.replace(autolinkRegexImportScript,
'$1$2<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$3">$3</a>$4');
// Parse ImportScript
autolinkContent = autolinkContent.replace(autolinkRegexCorrectNs,
'$1Help$2');
// Correct link to Template:Namespace
autolinkTargetDiv[i][j].innerHTML = autolinkContent; // Write it back
}
}
});
// [[Category:Wikipedia scripts|Autolink]]