User:Lenore/autolink.js: Difference between revisions

Content deleted Content added
m fix in parsing URL
fix: now grabs all classes 'pre', 'tt', 'code', 'source' (not only 'pre'). Some regex fixes. Adding some ns. {{tl}} parsing. Correct link to Template:Namespace. < to ­<­ and > to ­>&shy. & to &
Line 6:
addOnloadHook (function () {
 
var autolinkCronoSpecial = wgCanonicalSpecialPageName == "Recentchanges" || /*
*/ wgCanonicalSpecialPageName == "Watchlist" || /*
*/ wgCanonicalSpecialPageName == "Recentchangeslinked" || /*
Line 30:
 
/* ------------------------------------------ Skins --------------------------------------------- */
 
autolinkTargetDiv[0] =var autolinkBody;
 
if (document.getElementById('bodyContent'))
Line 48 ⟶ 50:
/* ---------------------------------------- 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 = new Array()[];
 
autolinkTargetDiv[0] = autolinkBody;
 
if (autolinkDiff) {
 
autolinkTargetDiv[0] = autolinkBody.getElementsByClassName('diff', 'firstrevisionheader');
else autolinkTargetDiv[1] = autolinkBody.getElementsByTagNamegetElementsByClassName('pre', 'tt', 'code', 'sourcefirstrevisionheader');
 
} else if (!autolinkCrono) {
 
if (autolinkEdit)
autolinkTargetDiv[0] = autolinkBody.getElementsByClassName('comment');
 
else {
else autolinkTargetDiv = autolinkBody.getElementsByTagName('pre', 'tt', 'code', 'source');
 
autolinkTargetDiv[0] = autolinkBody.getElementsByTagName('pre');
}
autolinkTargetDiv[1] = autolinkBody.getElementsByTagName('tt');
autolinkTargetDiv[2] = autolinkBody.getElementsByTagName('code');
autolinkTargetDiv[3] = autolinkBody.getElementsByTagName('source');
 
}
 
} else autolinkTargetDiv[0] = autolinkBody;
 
/* ------------------------------------------ Regex --------------------------------------------- */
 
var autolinkColor;
 
var autolinkRegexURLinWcodeWoLabel;
var autolinkRegexURLinWcodeWithLabel;
 
var autolinkRegexURL;
var autolinkRegexWlink;
var autolinkRegexOtherPages;
var autolinkRegexTl;
var autolinkRegexTemplate;
var autolinkRegexInternalURL;
 
var autolinkRegexImportScript;
 
var autolinkRegexCorrectNs;
 
if (autolinkCrono || autolinkEdit) {
Line 81 ⟶ 105:
 
var autolinkInputs = document.getElementsByTagName('input');
var autolinkInputValues = new Array()[];
 
for (i=0; i<autolinkInputs.length; i++)
Line 94 ⟶ 118:
}
 
var autolinkColor = ''; // Link color in history pages
 
autolinkRegexURLinWcodeWoLabel = /()\[{1}((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&;%~]+)\s*\]{1}()/g;
// External links in history pages, wikicode without label
 
autolinkRegexURLinWcodeWithLabel = /()\[{1}((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&;%~]+)()\s+([^\]\n]+)\]{1}()/g;
// External links in history pages, wikicode with label (the URL will not be visible)
 
} else {
 
var autolinkColor = 'inherit'; // Link color in other pages
 
autolinkRegexURLinWcodeWoLabel = /(\[{1})((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&;%~]+)(\s*\]{1})/g;
// External links in other pages, wikicode without label
 
autolinkRegexURLinWcodeWithLabel = /(\[{1})(((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&;%~]+))(\s+[^\]\n]+)(\]{1})/g;
// External links in other pages, wikicode with label
 
}
 
autolinkRegexURL = /((?:[^>"\[]|[^\=]")(?:\<span class\="diffchange"\>|))((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&;%~]+)((?:\<\/span\>|)[\s\]\)\|\}'"])/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;
Line 131 ⟶ 158:
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
 
/* ---------------------------------------------------------------------------------------------- */
Line 136 ⟶ 166:
for (i=0; i<autolinkTargetDiv.length; i++) {
 
autolinkContent for (j=0; j<autolinkTargetDiv[i].innerHTMLlength; j++) {
 
var autolinkContent = autolinkTargetDiv[i][j].innerHTML;
 
autolinkContent = autolinkContent.replace(/&lt;/g, '&shy;<&shy;');
autolinkContent = autolinkContent.replace(/&gt;/g, '&shy;>&shy;');
// &lt; to &shy;<&shy; and &gt; to &shy;>&shy; (&shy; is needed to not activate HTML)
 
autolinkContent = autolinkContent.replace(/&amp;/g, '&');
// &amp; 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,
'$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(autolinkRegexURL,
'$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>$3');
// Parse inactive external links (no wikicode)
 
autolinkContent = autolinkContent.replace(autolinkRegexWlink,
'$1[[$2<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$3$5$7">$3$4$5$6$7$8</a>$9]]$10');
// Make wikilink code into links
 
autolinkContent = autolinkContent.replace(autolinkRegexOtherPages,
'$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 included code into links
 
autolinkContent = autolinkContent.replace(autolinkRegexTemplate,
'$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(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,
autolinkTargetDiv[i].innerHTML = autolinkContent; // Write it back
'$1Help$2');
// Correct link to Template:Namespace
 
autolinkTargetDiv[i][j].innerHTML = autolinkContent; // Write it back
 
}
 
}