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:36, 18 June 2009 (Created page with '// Autolink wikilinks, [external links] and {{templates}} (especially useful for monobook.js // and similar pages); adapted from [[Wikipedia:WikiProject User s...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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 () {

    if (location.href.indexOf('&action=edit') != -1
     || location.href.indexOf('&action=submit') != -1
     || location.href.indexOf('Special%3A') != -1
     || location.href.indexOf('Speciale%3A') != -1) {

       return; // Disabled in edit, submit and some critic pages

    }

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

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

    }

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

    if (document.getElementById('bodyContent'))
       targetdiv = document.getElementById('bodyContent');
    // MonoBook-like skins

    else {

    if (document.getElementById('mw_content'))
       targetdiv = document.getElementById('mw_content');
    // Modern skin

    else targetdiv = document.getElementById('content');
    // Older skins

    }

/* ---------------------------------------- Variables ------------------------------------------- */

    autolinkCrono = location.href.indexOf('&action=history') != -1 || /*
    */location.href.indexOf('Speciale:UltimeModifiche') != -1 || /*
    */location.href.indexOf('Special:RecentChanges') != -1 || /*
    */location.href.indexOf('Speciale:OsservatiSpeciali') != -1 || /*
    */location.href.indexOf('Special:Watchlist') != -1 || /*
    */location.href.indexOf('Speciale:Contributi') != -1 || /*
    */location.href.indexOf('Special:Contributions') != -1;
    // It says if I'm in a history page

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

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

    if (autolinkCrono) {

       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+([^\]]+)\]{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+[^\]]+)(\]{1})/g;
       // External links in other pages, wikicode with label

    }

    autolinkRegexURL = /([^>"'\[]|\s["'])((?:https?|ftps?):\/\/[^\s\]\)\}<>'"]+)/g;
    // External links (no wikicode)

    autolinkRegexWlink = /\[{2}([^\|\]<>\n]*)([^\n]*?)\]{2}/g;
    // Wikilinks

    autolinkRegexTemplate = /\{{2}((?:[Ss][Uu][Bb][Ss][Tt]\:|[Mm][Ss][Gg]\:|[Mm][Ss][Gg][Nn][Ww]\:)?)([^<>\n]*?)(\s*(?:(?:\:|\|)[\s\S]*?|))\}{2}/g;
    // Templates

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

    content = targetdiv.innerHTML;

    if (autolinkParseLink == true || !autolinkDiff) { 

       content = content.replace(autolinkRegexURLinWcodeWoLabel,
       '$1<a class="autolink" style="color:' + autolinkColor + '" href="$2">$2</a>$3');
       // Make external links in wikicode without label into links

       content = content.replace(autolinkRegexURLinWcodeWithLabel,
       '$1<a class="autolink" style="color:' + autolinkColor + '" href="$2">$3$4</a>$5');
       // Make external links in wikicode with label into links

       content = content.replace(autolinkRegexURL,
       '$1<a class="autolink" style="color:' + autolinkColor + '" href="$2">$2</a>'); 
       // Parse inactive external links (no wikicode)

    }

    content = content.replace(autolinkRegexWlink,
    '[[<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/$1">$1</a>$2]]');
    // Make wikilink code into links

    content = content.replace(autolinkRegexTemplate,
    '{{$1<a class="autolink" style="color:' + autolinkColor + '" href="/wiki/Template:$2">$2</a>$3}}');
    // Make template code into links

    targetdiv.innerHTML = content; // Write it back

});

// [[Category:Wikipedia scripts|Autolink]]