User:Lenore/autolink.js: Difference between revisions

Content deleted Content added
m -no more useful code
m Bugfix: Allow for &diff or ?diff; Better ways to do this would involve more editing
 
(17 intermediate revisions by 3 users not shown)
Line 1:
// 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 = 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";
// Special crono pages where this script is enabled
 
if ((typeof(autolinkMySpecialPage) != "undefined"))
autolinkCronoSpecial = autolinkCronoSpecial || mw.config.get('wgPageName') == autolinkMySpecialPage;
 
if (mw.config.get('wgCanonicalNamespace') == "Special" && !autolinkCronoSpecial) {
 
return; // Disabled in the other special pages
Line 25:
if (typeof(autolinkParseLink) == 'undefined') {
 
autolinkParseLink = true; // Parse external links in all cases, even in diffs. To be disabled when wikEd is active
 
}
 
/* ------------------------------------------ SkinsVariables --------------------------------------------- */
 
var autolinkDiff = ___location.href.indexOf('diff=') != -1;
var autolinkBody;
// It says if I'm in a diff page
 
if (document.getElementById('bodyContent'))
autolinkBody = document.getElementById('bodyContent');
// MonoBook-like skins
 
else {
 
if (document.getElementById('mw_content'))
autolinkBody = document.getElementById('mw_content');
// Modern skin
 
else autolinkBody = document.getElementById('content');
// Older skins
 
}
 
/* ---------------------------------------- Variables ------------------------------------------- */
 
var autolinkCrono = ___location(mw.hrefconfig.indexOfget('&action=historywgAction') !== -1'history' || autolinkCronoSpecial);
// It says if I'm in a history page
 
var autolinkDiffautolinkEdit = ___location($.hrefinArray(mw.indexOfconfig.get('&diff=wgAction'), ['edit' , 'submit']) !== -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 autolinkColor; // links color
 
var autolinkTargetDiv = [];
 
if (autolinkDiff) { // in diff pages
if (autolinkParseLink) { // without wikEd
 
autolinkTargetDiv[0] = autolinkBody.getElementsByClassName$('.diff');
autolinkTargetDiv[1] = autolinkBody.getElementsByClassName('firstrevisionheader');
 
}
else if (autolinkCrono || autolinkEdit)
autolinkTargetDiv[0] = autolinkBody.getElementsByClassName('comment');
 
else {
 
autolinkTargetDiv[0] = autolinkBody.getElementsByTagName('pre');
autolinkTargetDiv[1] = autolinkBody.getElementsByTagName('tt');
autolinkTargetDiv[2] = autolinkBody.getElementsByTagName('code');
autolinkTargetDiv[3] = autolinkBody.getElementsByTagName('source');
 
autolinkTargetDiv[1] = $('.firstrevisionheader');
autolinkColor = 'inherit'; // not coloured links
} else { // with wikEd
autolinkTargetDiv[0] = $('.comment');
autolinkColor = ''; // coloured links
}
} else if (autolinkCrono || autolinkEdit) { // in comments
autolinkTargetDiv[0] = $('.comment');
autolinkColor = ''; // coloured links
} else { // in code sections
autolinkTargetDiv[0] = $('source');
autolinkTargetDiv[1] = $('.javascript');
autolinkTargetDiv[2] = $('.source-javascript');
autolinkTargetDiv[3] = $('.css');
autolinkTargetDiv[4] = $('.source-css');
autolinkColor = ''; // coloured links
}
 
/* ------------------------------------------ Regex --------------------------------------------- */
 
var autolinkColorautolinkRegexTl;
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 autolinkRegexURLinWcodeWoLabelautolinkActivateHTML = false;
var autolinkRegexURLinWcodeWithLabel;
 
/* ------------------------------------------------------------------------------------------ */
var autolinkRegexURL;
var autolinkRegexWlink;
var autolinkRegexOtherPages;
var autolinkRegexTl;
var autolinkRegexTemplate;
var autolinkRegexInternalURL;
 
autolinkRegexTl = /([^{]{{2}\s*[Tt]l\|)([^[\]{}|:<>\n]+)/g;
var autolinkRegexImportScript;
// For {{tl}}
autolinkRegexURL = /((?:[^"\[]|[^=]")(?:<span class="diffchange">)?)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+?)(?=(?:<\/span>)?[\s|)\]}'"])/g;
// External links (no wikicode)
 
// Regex for diffs
var autolinkRegexCorrectNs;
 
autolinkRegexURLinWcodeWoLabel = /((?:[^\[]|^)\[\s*(?:<\/span>)?\s*(?:<span class="diffchange">)?\s*)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)((?:<\/span>)?(?:<span class="diffchange">)?)([\w._\/\\~%\-+&#?!=()@:]*)((?:<\/span>)?(?:<span class="diffchange">)?)([\w._\/\\~%\-+&#?!=()@:]*)([^\]\n]*\])/gm;
if (autolinkCrono || autolinkEdit) {
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 diff pages, wikicode without label
 
autolinkRegexURLinWcodeWithLabel = autolinkRegexURLinWcodeWoLabel;
autolinkColor = ''; // Link color in history pages
autolinkRegexSubstinWcodeWithLabel = autolinkRegexSubstinWcodeWoLabel;
// External links in diff pages, wikicode with label
autolinkRegexOtherPages = /((?:[^{]|^)\{{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">)?\s*)((?:[Dd]iscussion[ei] ?)?\s*(?:[Ss]peciale?|[Ww](?:ikipedia|P)|[Tt]emplate|[Uu](?:ser|tente)|[Pp]ortale?|[Pp]rogetto)?\s*(?: ?[Tt]alk)?\s*:[^[\]{}|:<>\n]+)((?:<\/span>)?(?:<span class="diffchange">)?)([^[\]{}|:<>\n]*)((?:<\/span>)?(?:<span class="diffchange">)?)([^[\]{}|:<>\n]*)(\||}{2})/gm;
autolinkRegexSubstOtherPages = '$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';
// Other pages included in diff pages
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';
// Templates in diff pages
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';
// Wikilinks in diff pages
autolinkRegexWlink2 = autolinkRegexWlink1;
autolinkRegexSubstWlink2 = autolinkRegexSubstWlink1;
 
// Regex for comments or code sections
autolinkRegexURLinWcodeWoLabel = /()\[{1}((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&%~]+)\s*\]{1}()/g;
// External links in history pages, wikicode without label
 
if (!autolinkDiff || (autolinkDiff && !autolinkParseLink)) {
autolinkRegexURLinWcodeWithLabel = /()\[{1}((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&%~]+)()\s+([^\]\n]+)\]{1}()/g;
// External links in history pages, wikicode with label (the URL will not be visible)
 
autolinkRegexURLinWcodeWoLabel = /([^\[]|^)\[\s*((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)\s*\]/gm;
} else {
autolinkRegexSubstinWcodeWoLabel = '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>';
// External links in comments or code sections, wikicode without label
 
autolinkRegexURLinWcodeWithLabel = /([^\[]|^)\[\s*((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)\s+([^\n]+?)\s*\]/gm;
autolinkColor = 'inherit'; // Link color in other pages
autolinkRegexSubstinWcodeWithLabel = '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$3</a>';
// External links in comments or code sections, wikicode with label (the URL will not be visible)
autolinkRegexOtherPages = /((?:[^{]|^)\{{2}\s*(?:(?:[Ss][Uu][Bb][Ss][Tt]|[Mm][Ss][Gg]|[Mm][Ss][Gg][Nn][Ww])\s*:)?\s*)((?:[Dd]iscussion[ei] ?)?\s*(?:[Ss]peciale?|[Ww](?:ikipedia|P)|[Tt]emplate|[Uu](?:ser|tente)|[Pp]ortale?|[Pp]rogetto)?\s*(?: ?[Tt]alk)?\s*:[^[\]{}|:<>\n]+)(\||}{2})/gm;
autolinkRegexSubstOtherPages = '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$2">$2</a>$3';
// Other pages included in comments or code sections
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';
// Templates in comments or code sections
 
autolinkRegexURLinWcodeWoLabelautolinkRegexWlink1 = /(\[{12})\s*((?:https?[^[\]{}|ftps<>\n]+?):\/s*\/[\w-\/|\s*(.\:\+?\=&%~]+)(\s*\]{12})/g;
autolinkRegexSubstWlink1 = '<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$1">$2</a>';
// External links in other pages, wikicode without label
// Wikilinks in code sections, with label
 
autolinkRegexURLinWcodeWithLabel = /(\[{1})(((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&%~]+))(\s+[^\]\n]+)(\]{1})/g;
autolinkRegexWlink2 = /\[{2}\s*([^[\]{}|<>\n]+?)\s*\|?\s*\]{2}/g;
// External links in other pages, wikicode with label
autolinkRegexSubstWlink2 = '<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$1">$1</a>';
// Wikilinks in code sections, without label
autolinkActivateHTML = true;
// Activate some HTML (inline) and wikicode for bold and italic
 
}
 
autolinkRegexURLautolinkRegexInternalURL = /((?:[^"\[]|[^\=]")(?:\<span class\="diffchange'|"|\>|)s)((?:https?|ftps?):\/\/[\?w-\/index\.\:php\?|\=&%~]+)((/?:\<wiki\/span\>|)[\sw._\]/\)\|~%\}'"-+&#?!=()@:]+)\2/g;
// External links (no wikicode)
 
autolinkRegexWlink = /(\<span class\="diffchange"\>|)\[{2}(\<\/span\>|)([^\[\]\{\}<>\n]*?)(\<span class\="diffchange"\>|)([^\[\]\{\}<>\n]*?)(\<\/span\>|)([^\[\]\{\}<>\n]*?)(|\|[^\n]*?)(\<span class\="diffchange"\>|)\]{2}(\<\/span\>|)/g;
// Wikilinks
 
autolinkRegexOtherPages = /(\<span class\="diffchange"\>|)([^\{]|\{{2}|^)(\<\/span\>|)(\<span class\="diffchange"\>|)\{{2}(\<\/span\>|)(\<span class\="diffchange"\>|)([Ss][Uu][Bb][Ss][Tt]\:|[Mm][Ss][Gg]\:|[Mm][Ss][Gg][Nn][Ww]\:|)(\<\/span\>|)(\<span class\="diffchange"\>|)((?:[Dd]iscussion[ei] ?|)(?:[Ss]peciale?|[Ww](?:ikipedia|P)|[Tt]emplate|[Uu](?:ser|tente)|[Pp]ortale?|[Pp]rogetto|)(?: ?[Tt]alk|)\:[^\{\}\:\&\|<>\n]*?)(\<\/span\>|)([^\{\}\:\&\|<>\n]*?)(\<span class\="diffchange"\>|)([^\{\}\:\&\|<>\n]*?)(\<\/span\>|)([^\{\}\:\&\|<>\n]*?)(?=\s*(?:(?:\:|\|)|\}{2}))/gm;
// Not-template pages included
 
autolinkRegexTl = /({{[Tt]l\|)([^\{\}\|<>]+)/g;
// For {{tl}}
 
autolinkRegexTemplate = /(\<span class\="diffchange"\>|)([^\{]|\{{2}|^)(\<\/span\>|)(\<span class\="diffchange"\>|)\{{2}(\<\/span\>|)(\<span class\="diffchange"\>|)([Ss][Uu][Bb][Ss][Tt]\:|[Mm][Ss][Gg]\:|[Mm][Ss][Gg][Nn][Ww]\:|)(\<\/span\>|)([^\{\}\:\&\|<>\n]*?)(\<span class\="diffchange"\>|)([^\{\}\:\&\|<>\n]*?)(\<\/span\>|)([^\{\}\:\&\|<>\n]*?)(?=\s*(?:(?:\:|\|)|\}{2}))/gm;
// Templates
 
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++) {
Line 150 ⟶ 167:
for (j=0; j<autolinkTargetDiv[i].length; j++) {
 
var autolinkContent = autolinkTargetDiv[i][j].innerHTML;
 
autolinkContent = autolinkContent.replace(/&lt;/g, '&shy;<&shy;');
autolinkContent = autolinkContent.replace(/&gt;/g, '&shy;>&shy;');
// &amp;lt; to &amp;shy;<&amp;shy; and &amp;gt; to &amp;shy;>&amp;shy; (&amp;shy; is needed to not activatea HTMLmarker)
 
autolinkContent = autolinkContent.replace(/&amp;/g, '&');
// &amp;amp; to &
/* ----------------------------------------------------------------------------------- */
 
autolinkContent = autolinkContent.replace(autolinkRegexTl, '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2">$2</a>');
autolinkContent = autolinkContent.replace(autolinkRegexTl,
// For {{tl}}: make his argument into link
'$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2">$2</a>');
// For {{tl}}: make his argument into link
 
autolinkContent = autolinkContent.replace(autolinkRegexURL, '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>');
if (autolinkParseLink == true || !autolinkDiff) {
// Parse inactive external links (no wikicode)
// 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
'$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>$3');
// Make external links in wikicode without label into links
 
autolinkContent = autolinkContent.replace(autolinkRegexURLinWcodeWithLabel,
'$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$3$4</a>$5');
// Make external links in wikicode with label into links
 
autolinkContent = autolinkContent.replace(autolinkRegexURLautolinkRegexURLinWcodeWithLabel, autolinkRegexSubstinWcodeWithLabel);
// Make external links in wikicode with label into links
'$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>$3');
// Parse inactive external links (no wikicode)
autolinkContent = autolinkContent.replace(autolinkRegexOtherPages, autolinkRegexSubstOtherPages);
// Make other pages included code into links
 
autolinkContent = autolinkContent.replace(autolinkRegexWlinkautolinkRegexTemplate, autolinkRegexSubstTemplate);
autolinkContent = autolinkContent.replace(/href\="\/wiki\/Template\:#/g, 'href="/wiki/Help:');
'$1[[$2<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$3$5$7">$3$4$5$6$7$8</a>$9]]$10');
// Make wikilinktemplate code into links
 
autolinkContent = autolinkContent.replace(autolinkRegexOtherPagesautolinkRegexWlink1, autolinkRegexSubstWlink1);
autolinkContent = autolinkContent.replace(autolinkRegexWlink2, autolinkRegexSubstWlink2);
'$1$2$3$4{{$5$6<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Help:$7">$7</a>$8<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$10$12$14$16">$9$10$11$12$13$14$15$16</a>');
// Make not-template pages includedwikilink code into links
 
autolinkContent = autolinkContent.replace(autolinkRegexInternalURL, '$1$2<a class="external autolink" style="color:' + autolinkColor + '" href="' + mw.config.get('wgServer') + '$3">$3</a>$2');
autolinkContent = autolinkContent.replace(autolinkRegexTemplate,
// Parse inactive external links (no wikicode, under wgServer)
'$1$2$3$4{{$5$6<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Help:$7">$7</a>$8<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$9$11$13">$9$10$11$12$13</a>');
autolinkContent = autolinkContent.replace(/href\="\/wiki\/Template\:#/g, 'href="/wiki/Help:');
// Make template code into links
 
autolinkContent = autolinkContent.replace(autolinkRegexImportScript, '$1$2<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$3">$3</a>$4');
}
// Parse ImportScript
 
/* ----------------------------------------------------------------------------------- */
autolinkContent = autolinkContent.replace(autolinkRegexInternalURL,
'$1$2<a class="external autolink" style="color:' + autolinkColor + '" href="' + wgServer + '$3">$3</a>$2');
autolinkContent = autolinkContent.replace(/&shy;<&shy;/g, '&lt;');
// Parse inactive external links (no wikicode, under wgServer)
autolinkContent = autolinkContent.replace(/&shy;>&shy;/g, '&gt;');
// &amp;shy;<&amp;shy; to &amp;lt; and &amp;shy;>&amp;shy; to &amp;gt; (revert)
if (autolinkActivateHTML) {
autolinkContent = autolinkContent.replace(/&lt;(span|b|i|strong|small|tt|del|s|u|sub|sup)&gt;(.*?)&lt;\/(\1)&gt;/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>');
// I'm in a comment field (italic)
 
}
autolinkContent = autolinkContent.replace(autolinkRegexImportScript,
'$1$2<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$3">$3</a>$4');
// Parse ImportScript
 
if (autolinkDiff && autolinkParseLink) {
autolinkContent = autolinkContent.replace(autolinkRegexCorrectNs,
autolinkContent = autolinkContent.replace(/<a [^>]+><\/a>/g, ''); // Clean
'$1Help$2');
autolinkContent = autolinkContent.replace(/([^\[]|^)\[\s*(<a [^>]+>)(?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+(<\/a>)\s+([^\]\n]+)\]/gm, '$1$2$4$3');
// Correct link to Template:Namespace
autolinkContent = autolinkContent.replace(/([^\[]|^)\[\s*(<a [^>]+>)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)(<\/a>)\s*\]/gm, '$1$2$3$4');
}
 
autolinkTargetDiv[i][j].innerHTML = autolinkContent; // Write it back
 
}