User:Cacycle/myskinify.js

From Wikipedia, the free encyclopedia
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.
// replace [[Special:MySkin.js]] links with [[Special:MyPage/SKIN.js]] links (also for SKIN.css)
function MySkinify() {

// get content container for all skins but modern
    var content = document.getElementById('content');

// modern skin
    if (content == null) {
        content = document.getElementById('mw-content');
    }

// cycle through links
    var links = content.getElementsByTagName('a');
    for (var i = 0; i < links.length; i ++) {
        var link = links[i];

// quick test
        if (link.href.toLowerCase().indexOf('special:myskin.') > -1) {

// regexp replace and detection
            var href = link.href.replace(/(\/|=)special:myskin.(js|css)($|\?|&)/i, '$1Special:MyPage/' + skin + '.$2$3'.toLowerCase());
            if (link.href != href) {
                link.href = href;

// fix redlink to blue
                if (link.className == 'new') {
                    link.className = '';
                }

// fix title
                if (link.title != '') {
                    var title = 'User:' + mw.config.get('wgUserName') + '/' + skin;
                    if (href.indexOf('Special:MyPage/' + skin + '.css') > -1) {
                        link.title = title + '.css'; 
                    }
                    else {
                        link.title = title + '.js'; 
                    }
                }
            }
        }
    }
}

// execute after page load
if (mw.config.get('wgUserName') != null) {
    addOnloadHook(MySkinify);
}