This is an old revision of this page, as edited by Lenore(talk | contribs) at 19:25, 19 December 2009(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 >­. & to &). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.Revision as of 19:25, 19 December 2009 by Lenore(talk | contribs)(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 >­. & to &)
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.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Autolink [[wikilinks]], [external links] and {{templates}} (especially useful for monobook.js // and similar pages); adapted from [[Wikipedia:WikiProject User scripts/Scripts/Autolink]]/* ---------------------------------------------------------------------------------------------- */addOnloadHook(function(){varautolinkCronoSpecial=wgCanonicalSpecialPageName=="Recentchanges"||/* */wgCanonicalSpecialPageName=="Watchlist"||/* */wgCanonicalSpecialPageName=="Recentchangeslinked"||/* */wgCanonicalSpecialPageName=="Newpages"||/* */wgCanonicalSpecialPageName=="Log"||/* */wgCanonicalSpecialPageName=="Contributions";// Special crono pages where this script is enabledif((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}/* ------------------------------------------ Skins --------------------------------------------- */varautolinkBody;if(document.getElementById('bodyContent'))autolinkBody=document.getElementById('bodyContent');// MonoBook-like skinselse{if(document.getElementById('mw_content'))autolinkBody=document.getElementById('mw_content');// Modern skinelseautolinkBody=document.getElementById('content');// Older skins}/* ---------------------------------------- Variables ------------------------------------------- */varautolinkCrono=___location.href.indexOf('&action=history')!=-1||autolinkCronoSpecial;// It says if I'm in a history pagevarautolinkDiff=___location.href.indexOf('&diff=')!=-1;// It says if I'm in a diff pagevarautolinkEdit=___location.href.indexOf('&action=edit')!=-1||___location.href.indexOf('&action=submit')!=-1;// It says if I'm in an edit or submit pagevarautolinkTargetDiv=[];if(autolinkDiff){autolinkTargetDiv[0]=autolinkBody.getElementsByClassName('diff');autolinkTargetDiv[1]=autolinkBody.getElementsByClassName('firstrevisionheader');}elseif(!autolinkCrono){if(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');}}elseautolinkTargetDiv[0]=autolinkBody;/* ------------------------------------------ Regex --------------------------------------------- */varautolinkColor;varautolinkRegexURLinWcodeWoLabel;varautolinkRegexURLinWcodeWithLabel;varautolinkRegexURL;varautolinkRegexWlink;varautolinkRegexOtherPages;varautolinkRegexTl;varautolinkRegexTemplate;varautolinkRegexInternalURL;varautolinkRegexImportScript;varautolinkRegexCorrectNs;if(autolinkCrono||autolinkEdit){if(autolinkCrono){varautolinkInputs=document.getElementsByTagName('input');varautolinkInputValues=[];for(i=0;i<autolinkInputs.length;i++)if(autolinkInputs[i].type=="text"){autolinkInputValues[i]=autolinkInputs[i].value;autolinkInputs[i].setAttribute('value',"");// Svuota i campi testuali}}autolinkColor='';// Link color in history pagesautolinkRegexURLinWcodeWoLabel=/()\[{1}((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&%~]+)\s*\]{1}()/g;// External links in history pages, wikicode without labelautolinkRegexURLinWcodeWithLabel=/()\[{1}((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&%~]+)()\s+([^\]\n]+)\]{1}()/g;// External links in history pages, wikicode with label (the URL will not be visible)}else{autolinkColor='inherit';// Link color in other pagesautolinkRegexURLinWcodeWoLabel=/(\[{1})((?:https?|ftps?):\/\/[\w-\/\.\:\?\=&%~]+)(\s*\]{1})/g;// External links in other pages, wikicode without labelautolinkRegexURLinWcodeWithLabel=/(\[{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;// WikilinksautolinkRegexOtherPages=/(\<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 includedautolinkRegexTl=/({{[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;// TemplatesautolinkRegexInternalURL=/([^\=])('|"|\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;// ImportScriptautolinkRegexCorrectNs=/(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++){varautolinkContent=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 linkif(autolinkParseLink==true||!autolinkDiff){// Qui vanno tutte le sostituzioni fatte anche dal wikEd nelle diffautolinkContent=autolinkContent.replace(autolinkRegexURLinWcodeWoLabel,'$1<a class="external autolink" style="color:'+autolinkColor+'" href="$2">$2</a>$3');// Make external links in wikicode without label into linksautolinkContent=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 linksautolinkContent=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 linksautolinkContent=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 linksautolinkContent=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 ImportScriptautolinkContent=autolinkContent.replace(autolinkRegexCorrectNs,'$1Help$2');// Correct link to Template:NamespaceautolinkTargetDiv[i][j].innerHTML=autolinkContent;// Write it back}}if(autolinkCrono){for(i=0;i<autolinkInputs.length;i++)if(autolinkInputs[i].type=="text"){autolinkInputs[i].setAttribute('value',autolinkInputValues[i]);// Ripristina il contenuto dei campi testuali}}});// [[Category:Wikipedia scripts|Autolink]]