User:Lenore/autolink.js: Difference between revisions

Content deleted Content added
mNo edit summary
m Bugfix: Allow for &diff or ?diff; Better ways to do this would involve more editing
 
(45 intermediate revisions by 4 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" || /*
if (___location.href.indexOf('&action=edit') != -1
*/ mw.config.get('wgCanonicalSpecialPageName') == "Watchlist" || /*
|| ___location.href.indexOf('&action=submit') != -1
*/ mw.config.get('wgCanonicalSpecialPageName') == "Recentchangeslinked" || /*
|| ___location.href.indexOf('Special%3A') != -1
*/ mw.config.get('wgCanonicalSpecialPageName') == "Newpages" || /*
|| ___location.href.indexOf('Speciale%3A') != -1) {
*/ mw.config.get('wgCanonicalSpecialPageName') == "Log" || /*
*/ mw.config.get('wgCanonicalSpecialPageName') == "Contributions";
// Special crono pages where this script is enabled
 
if ((typeof(autolinkMySpecialPage) != "undefined"))
return; // Disabled in edit, submit and some critic pages
autolinkCronoSpecial = autolinkCronoSpecial || mw.config.get('wgPageName') == autolinkMySpecialPage;
 
if (mw.config.get('wgCanonicalNamespace') == "Special" && !autolinkCronoSpecial) {
 
return; // Disabled in the other special pages
 
}
Line 17 ⟶ 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;
if (document.getElementById('bodyContent'))
// It says if I'm in a diff page
targetdiv = document.getElementById('bodyContent');
// MonoBook-like skins
 
var autolinkCrono = (mw.config.get('wgAction') == 'history' || autolinkCronoSpecial);
else {
// It says if I'm in a history page
 
var autolinkEdit = ($.inArray(mw.config.get('wgAction'), ['edit' , 'submit']) !== -1);
if (document.getElementById('mw_content'))
// It says if I'm in an edit or submit page
targetdiv = document.getElementById('mw_content');
// Modern skin
var autolinkColor; // links color
 
var autolinkTargetDiv = [];
else targetdiv = document.getElementById('content');
// Older skins
 
if (autolinkDiff) { // in diff pages
if (autolinkParseLink) { // without wikEd
autolinkTargetDiv[0] = $('.diff');
 
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
}
 
/* ------------------------------------------ VariablesRegex --------------------------------------------- */
 
var autolinkRegexTl;
autolinkCrono = ___location.href.indexOf('&action=history') != -1 || /*
var autolinkRegexURL;
*/___location.href.indexOf('Speciale:UltimeModifiche') != -1 || /*
*/___location.href.indexOf('Special:RecentChanges') != -1 || /*
var autolinkRegexURLinWcodeWoLabel;
*/___location.href.indexOf('Speciale:OsservatiSpeciali') != -1 || /*
var autolinkRegexSubstinWcodeWoLabel;
*/___location.href.indexOf('Special:Watchlist') != -1 || /*
var autolinkRegexURLinWcodeWithLabel;
*/___location.href.indexOf('Speciale:Contributi') != -1 || /*
var autolinkRegexSubstinWcodeWithLabel;
*/___location.href.indexOf('Special:Contributions') != -1;
var autolinkRegexOtherPages;
// It says if I'm in a history page
var autolinkRegexSubstOtherPages;
var autolinkRegexTemplate;
var autolinkRegexSubstTemplate;
var autolinkRegexWlink1;
var autolinkRegexSubstWlink1;
var autolinkRegexWlink2;
var autolinkRegexSubstWlink2;
var autolinkRegexInternalURL;
var autolinkRegexImportScript;
 
var autolinkActivateHTML = false;
autolinkDiff = ___location.href.indexOf('&diff=') != -1;
// It says if I'm in a diff page
 
/* ------------------------------------------ Regex ------------------------------------------------ */
 
autolinkRegexTl = /([^{]{{2}\s*[Tt]l\|)([^[\]{}|:<>\n]+)/g;
if (autolinkCrono) {
// For {{tl}}
autolinkRegexURL = /((?:[^"\[]|[^=]")(?:<span class="diffchange">)?)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+?)(?=(?:<\/span>)?[\s|)\]}'"])/g;
// External links (no wikicode)
 
// Regex for diffs
var autolinkColor = ''; // link color in history pages
 
autolinkRegexURLinWcodeWoLabel = /((?:[^\[]|^)\[{1}\s*(?:<\/span>)?\s*(?:<span class="diffchange">)?\s*)((?:https?|ftps?):\/\/[^\sw._\/\\~%\-+&#?!=()@:]+)((?:<\/span>)?(?:<span class="diffchange">)?)([\sw._\/\\~%\-+&#?!=()@:]*)((?:<\/span>)?(?:<span class="diffchange">)?)([\w._\/\\~%\-+&#?!=()@:]{1}*)([^\]\n]*\])/ggm;
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 history pages, wikicode without label
// External links in diff pages, wikicode without label
 
autolinkRegexURLinWcodeWithLabel = /()\[{1}((?:https?|ftps?):\/\/[^\s]+)()\s+([^\]\n]+)\]{1}()/gautolinkRegexURLinWcodeWoLabel;
autolinkRegexSubstinWcodeWithLabel = autolinkRegexSubstinWcodeWoLabel;
// External links in history pages, wikicode with label (the URL will not be visible)
// 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
} else {
 
if (!autolinkDiff || (autolinkDiff && !autolinkParseLink)) {
var autolinkColor = 'inherit'; // link color in other pages
 
autolinkRegexURLinWcodeWoLabel = /([^\[{1}]|^)\[\s*((?:https?|ftps?):\/\/[^\s]w._\/\\~%\-+&#?)!=()@:]+)\s*\]{1})/ggm;
autolinkRegexSubstinWcodeWoLabel = '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$2</a>';
// External links in other pages, wikicode without label
// External links in comments or code sections, wikicode without label
 
autolinkRegexURLinWcodeWithLabel = /([^\[{1}]|^)(\[\s*((?:https?|ftps?):\/\/[^\sw._\/\\~%\-+&#?!=()@:]+))(\s+([^\]\n]+?)(\s*\]{1})/ggm;
autolinkRegexSubstinWcodeWithLabel = '$1<a class="external autolink" style="color:' + autolinkColor + '" href="$2">$3</a>';
// External links in other pages, wikicode with label
// 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
 
autolinkRegexWlink1 = /\[{2}\s*([^[\]{}|<>\n]+?)\s*\|\s*(.+?)\s*\]{2}/g;
autolinkRegexSubstWlink1 = '<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$1">$2</a>';
// Wikilinks in code sections, with label
autolinkRegexWlink2 = /\[{2}\s*([^[\]{}|<>\n]+?)\s*\|?\s*\]{2}/g;
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 = /([^>"'\[=])('|"|\s["'])((?:https\/?|ftpsw\/index\.php\?):|\/?wiki\/)[^\sw._\]/\)\}<>'"~%\-+&#?!=()@:]+)\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;
autolinkRegexWlink = /\[{2}([^\[][^\|\]<>\n]*)([^\n]*?)\]{2}/g;
// WikilinksImportScript
 
/* ---------------------------------------------------------------------------------------------- */
autolinkRegexTemplate = /\{{2}([Ss][Uu][Bb][Ss][Tt]\:|[Mm][Ss][Gg]\:|[Mm][Ss][Gg][Nn][Ww]\:|)([^\{][^\:\|\}<>\n]*)/g;
// Templates
 
for (i=0; i<autolinkTargetDiv.length; i++) {
/* ---------------------------------------------------------------------------------------------- */
 
for (j=0; j<autolinkTargetDiv[i].length; j++) {
content = targetdiv.innerHTML;
 
var autolinkContent = autolinkTargetDiv[i][j].innerHTML;
if (autolinkParseLink == true || !autolinkDiff) {
 
autolinkContent = autolinkContent.replace(/&lt;/g, '&shy;<&shy;');
content = content.replace(autolinkRegexURLinWcodeWoLabel,
autolinkContent = autolinkContent.replace(/&gt;/g, '&shy;>&shy;');
'$1<a class="autolink" style="color:' + autolinkColor + '" href="$2">$2</a>$3');
// &amp;lt; to &amp;shy;<&amp;shy; and &amp;gt; to &amp;shy;>&amp;shy; (&amp;shy; is a marker)
// Make external links in wikicode without label into links
 
autolinkContent = autolinkContent.replace(/&amp;/g, '&');
content = content.replace(autolinkRegexURLinWcodeWithLabel,
// &amp;amp; to &
'$1<a class="autolink" style="color:' + autolinkColor + '" href="$2">$3$4</a>$5');
// Make external links in wikicode with label into links
/* ----------------------------------------------------------------------------------- */
 
autolinkContent = autolinkContent.replace(autolinkRegexTl, '$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2">$2</a>');
content = content.replace(autolinkRegexURL,
// For {{tl}}: make his argument into link
'$1<a class="autolink" style="color:' + autolinkColor + '" href="$2">$2</a>');
// Parse inactive external links (no wikicode)
 
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(autolinkRegexURLinWcodeWoLabel, autolinkRegexSubstinWcodeWoLabel);
content = content.replace(autolinkRegexWlink,
// Make external links in wikicode without label into links
'[[<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$1">$1</a>$2]]');
// Make wikilink code into links
 
content = content.replace(autolinkRegexTemplate,
'{{<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Help:$1">$1</a><a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2">$2</a>');
// Make template code into links
 
autolinkContent = autolinkContent.replace(autolinkRegexURLinWcodeWithLabel, autolinkRegexSubstinWcodeWithLabel);
targetdiv.innerHTML = content; // Write it back
// Make external links in wikicode with label into links
autolinkContent = autolinkContent.replace(autolinkRegexOtherPages, autolinkRegexSubstOtherPages);
// Make other pages included 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(autolinkRegexWlink1, autolinkRegexSubstWlink1);
autolinkContent = autolinkContent.replace(autolinkRegexWlink2, autolinkRegexSubstWlink2);
// Make wikilink 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');
// 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(/&shy;<&shy;/g, '&lt;');
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)
 
}
 
if (autolinkDiff && autolinkParseLink) {
autolinkContent = autolinkContent.replace(/<a [^>]+><\/a>/g, ''); // Clean
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
 
}
 
}
 
});