User:Omegatron/monobook.js/autolinker.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
/* <pre><nowiki> */
 
/* Autolink [[wikilinks]] and {{templates}} (especially useful for monobook.js and similar pages) */
/* See [[Wikipedia:WikiProject User scripts/Scripts/Autolink]] */
/* Shouldn't affect things *inside* HTML tags, either */
 
addOnloadHook(function () {
    // Get the HTML of just the main body of the page, not including textareas hopefully
 
    if (document.getElementById('wikiDiff')) {
        targetdiv = document.getElementById('wikiDiff');   // wikiDiff if it's there
    } else {
 
    if (document.getElementById('wikiPreview')) {
        targetdiv = document.getElementById('wikiPreview');  // wikiPreview if it's there
    } else
 
    if (document.getElementById('bodyContent')) {
        targetdiv = document.getElementById('bodyContent');  // bodyContent div for most pages
    } else 
        return;
    }
    var content = targetdiv.innerHTML;
    content = content.replace(/([^\[])\[{2}([^\[\]\|\<\>\n]*)([^\[\]\<\>\n]*?)?\]{2}([^\]])/g, '$1<a class="autolink" href="/wiki/$2">[' + '[$2$3]' + ']</a>$4'); // Make wikilink code into links
    content = content.replace(/([^\{])\{{2}(subst\:|msg\:)?([^\{\}\|\<\>\n]*)([^\{\}\<\>\n]*?)?\}{2}([^\}])/g, '$1<a class="autolink" href="/wiki/Template:$3">{' + '{$2$3$4}' + '}</a>$5'); // Make template code into links
    targetdiv.innerHTML = content; // Write it back
});
 
/* </nowiki></pre> */