Jump to content

User:Lenore/autolink.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lenore (talk | contribs) at 19:31, 20 August 2009 (Revert to revision 309118801 dated 2009-08-20 19:27:10 by Lenore using popups). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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 () {

    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 (location.href.indexOf('&action=edit') != -1 || /*
    */ location.href.indexOf('&action=submit') != -1 || /*
    */ (wgCanonicalNamespace == "Special" && !autolinkCronoSpecial) /*
    */) {

       return; // Disabled in edit, submit and in the other special pages

    }

    if (typeof(autolinkParseLink) == 'undefined') { 

       autolinkParseLink = true; // Parse external links in all cases

    }

/* ------------------------------------------ Skins --------------------------------------------- */

    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 ------------------------------------------- */

    autolinkCrono = location.href.indexOf('&action=history') != -1 || autolinkCronoSpecial;
    // It says if I'm in a history page

    autolinkDiff = location.href.indexOf('&diff=') != -1;
    // It says if I'm in a diff page

    autolinkTargetDiv = new Array();

    if (autolinkDiff) {

       autolinkTargetDiv = autolinkBody.getElementsByClassName('diff', 'firstrevisionheader');

    } else {

       if (!autolinkCrono)

          autolinkTargetDiv = autolinkBody.getElementsByTagName('pre','tt','code','source');

       else

          autolinkTargetDiv[0] = autolinkBody;

    }

/* ------------------------------------------ Regex --------------------------------------------- */

    if (autolinkCrono) {

       var autolinkInputs = document.getElementsByTagName('input');
       autolinkInputValues = new Array();

       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

          }

       var autolinkColor = ''; // Link color in history pages

       autolinkRegexURLinWcodeWoLabel = /()\[{1}((?:https?|ftps?):\/\/[^\{\}\s]+?)\s*\]{1}()/g;
       // External links in history pages, wikicode without label

       autolinkRegexURLinWcodeWithLabel = /()\[{1}((?:https?|ftps?):\/\/[^\{\}\s]+)()\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?):\/\/[^\{\}\s]+?)(\s*\]{1})/g;
       // External links in other pages, wikicode without label

       autolinkRegexURLinWcodeWithLabel = /(\[{1})(((?:https?|ftps?):\/\/[^\{\}\s]+))(\s+[^\]\n]+)(\]{1})/g;
       // External links in other pages, wikicode with label

    }

    autolinkRegexURL = /((?:[^>"\[]|[^\=]")(?:\<span class\="diffchange"\>|))((?:https?|ftps?):\/\/[^\{\}]+?)((?:\<\/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"\>|)((?:[Ss]peciale?|[Ww]ikipedia|[Tt]emplate|[Pp]ortale?|[Pp]rogetto|)\:[^\{\}\:\&\|<>\n]*?)(\<\/span\>|)(\<span class\="diffchange"\>|)([^\{\}\:\&\|<>\n]*?)(\<\/span\>|)([^\{\}\:\&\|<>\n]*?)(?=\s*(?:(?:\:|\|)|\}{2}))/gm;
    // Not-template pages included

    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\/)[^\s\{\}\|]+?)\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

/* ---------------------------------------------------------------------------------------------- */

    for (i=0; i<autolinkTargetDiv.length; i++) {

    autolinkContent = autolinkTargetDiv[i].innerHTML;

    if (autolinkParseLink == true || !autolinkDiff) { 
    // Qui vanno tutte le sostituzioni fatte anche dal wikEd nelle diff

       autolinkContent = autolinkContent.replace(autolinkRegexURLinWcodeWoLabel,
       '$1<a class="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="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="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$13$15">$9$10$11$12$13$14$15</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(/Template\:#/g, 'Help:');
       // Make template code into links

    }

    autolinkContent = autolinkContent.replace(autolinkRegexInternalURL,
    '$1$2<a class="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

    autolinkTargetDiv[i].innerHTML = autolinkContent; // Write it back

    }

    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]]