User:Subh83/JavaScriptTools/RightclickMenus maintain.js

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.
/****************************************************
* Created by Subhrajit Bhattacharya [[User:Subh83]] *
* Licensed under GNU-GPL v3.0                       *
*****************************************************/
 
if (typeof(RightClickMenuKey)=='undefined') RightClickMenuKey = ''; // 'shift', 'alt', 'ctrl' or ''

function showMaintainMenu(e)
{
    var menudiv = document.getElementById("maintainrightclickmenu");
    if (!menudiv) {
        alert("Unable to locate rightclick-menu!");
        return false;
    }

    var posx = e.clientX; 
    var posy = e.clientY;
 
    var w, h;
    if (document.body && document.body.offsetWidth) {
        w = document.body.offsetWidth;
        h = document.body.offsetHeight;
    }
    if (document.compatMode=='CSS1Compat' &&
               document.documentElement &&
                  document.documentElement.offsetWidth ) {
        w = document.documentElement.offsetWidth;
        h = document.documentElement.offsetHeight;
    }
    if (window.innerWidth && window.innerHeight) {
        w = window.innerWidth;
        h = window.innerHeight;
    }
 
    menudiv.style.position = "fixed";
    if (posx < w/2) {
        menudiv.style.left = posx+"px";
        menudiv.style.pixelLeft = posx+"px";
        menudiv.style.right = "auto";
        menudiv.style.pixelRight = "auto";
    } else {
        menudiv.style.right = (w-posx)+"px";
        menudiv.style.pixelRight = (w-posx)+"px";
        menudiv.style.left = "auto";
        menudiv.style.pixelLeft = "auto";
    }
 
    if (posy < h/2) {
        menudiv.style.top = posy+"px";
        menudiv.style.pixelTop = posy+"px";
        menudiv.style.bottom = "auto";
        menudiv.style.pixelBottom = "auto";
    } else {
        menudiv.style.bottom = (h-posy)+"px";
        menudiv.style.pixelBottom = (h-posy)+"px";
        menudiv.style.top = "auto";
        menudiv.style.pixelTop = "auto";
    }

    if (!e) var e = window.event;
    var rightclick;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
 
    var keyPressed = true;
    if (RightClickMenuKey=='' && (e.shiftKey || e.ctrlKey || e.altKey)) keyPressed = false;
    else if (RightClickMenuKey=='shift' && !e.shiftKey) keyPressed = false;
    else if (RightClickMenuKey=='ctrl' && !e.ctrlKey) keyPressed = false;
    else if (RightClickMenuKey=='alt' && !e.altKey) keyPressed = false;
 
    if (rightclick && keyPressed) {
        menudiv.innerHTML = "<div style='font-size:8pt; background-color:#eee; border:2px outset #ccc; padding:3px; width:250px;'>RightclickMenus is currently under maintenance. Will be back soon! Sorry for the inconvenience.</div>";
        menudiv.style.zIndex = "10000";
        menudiv.style.display = "block";
        return false;
    }
    else {
        menudiv.style.display = "none";
        return true;
    }
}
 
addOnloadHook( function () {
 
    // The right-click menu 
    var theBody = document.getElementsByTagName("body")[0];
    if (theBody) {
        theBody.onmouseup = showMaintainMenu;
        theBody.oncontextmenu = showMaintainMenu;
    }
 
    var maintainrightclickmenu = document.createElement("div");
    maintainrightclickmenu.setAttribute("id", "maintainrightclickmenu");
    maintainrightclickmenu.setAttribute("display", "none");
    theBody.insertBefore(maintainrightclickmenu, theBody.childNodes[0]);
 
});