User:Neo-Jay/monobook.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.
//<div><pre><nowiki>
function MyaddLoadEvent(func)
{
    if (window.addEventListener)
        window.addEventListener("load", func, false);
    else if (window.attachEvent)
        window.attachEvent("onload", func);
}

function checkpath(url) {
    var reg=/^\/w(\/|iki\/).*/;
    if(reg.test(url))return true;

}

function getpathfromurl(url) {
    var ret=new String(url);
    var path3=/^([A-Za-z0-9_]+):\/\/([^\/]*)\/(.*)/;
    return ret.replace(path3,"$3");
}

function getpath(olink){
    if(is_gecko){
        return olink.pathname;
    }
    var reg=/^.*href="([^"]+)".*$/;
    return getpathfromurl(olink.outerHTML.replace(reg,"$1"));
}

function getpath1(url) {
    var ret=new String(url);
    var path1=/^([A-Za-z0-9_]+):\/\/([^\/]*)\/([^\/]*)\/(.*)/;
    var tmp=new String(url);
    ret=ret.replace(path1,"$3");
    if(ret==tmp) {
        return ret.replace(/^([A-Za-z0-9_]+):\/\/([^\/]*)\/(.*)/,"$3");
    }
    return ret;
}

function getpath2(url) {
    var ret=new String(url);
    var path2=/^([A-Za-z0-9_]+):\/\/([^\/]*)\/([^\/]*)\/([^\/]*)\/(.*)/;
    var tmp=new String(url);
    ret=ret.replace(path2,"$4");
    if(ret==tmp){
        return ret.replace(/^([A-Za-z0-9_]+):\/\/([^\/]*)\/([^\/]*)\/(.*)/,"$4");
    };
    return ret;
}

function update(url) {
    var ret=new String(url);
    var ud=/^http:\/\/([^\.\/]*).(wikimedia|wikipedia|wiktionary|wikibooks|wikiquote|wikisource|wikinews).org\/(.*)/;
    return ret.replace(ud,"https://secure.wikimedia.org/$2/$1/$3");
}

function updateurl(olink) {
    if(is_gecko) {
        return update(olink.href);
    }
    var reg=/^.*href="([^"]+)".*$/;
    return update(olink.outerHTML.replace(reg,"$1"));
}

function fixup(obj) {
    var path=getpath(obj);
    if(obj.protocol=="https:" && checkpath(path))
        obj.href="/"+getpath1(location)+"/"+getpath2(location)+path;
    else 
        obj.href=updateurl(obj);
    obj.onmouseover=obj.oldfun;
}
 
function checklink(olink) {
    if(olink.hostname=="upload.wikimedia.org")return;
    if(olink.hostname=="mail.wikipedia.org")return;
    if(olink.protocol=="https:") {
        if(olink.hostname=="secure.wikimedia.org") {
            if(!checkpath(olink.pathname))return;
        }
    }
    olink.oldfun=olink.onmouseover;
    if(olink.oldfun) {
        olink.onmouseover=function(){this.oldfun();fixup(this);}
    }
    else olink.onmouseover=function(){fixup(this);}
}

function updatehook() {
    for(i=0;i+document.links.length;++i) {
        checklink(document.links[i]);
    }
}

if(location.protocol=="https:") {
     MyaddLoadEvent(updatehook);
}//</nowiki></pre></div>