User:Subh83/JavaScriptTools/refRightclickMenu.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 ''

if (typeof(UserSubh83_utils1)=='undefined') importScript("User:Subh83/JavaScriptTools/utils1.js");


function getRefMenuHTML(refname) {
    var refRawname = refname.split("#")[1];
    var theLI = document.getElementById(refRawname);
    if (theLI)
        var theAs = theLI.getElementsByTagName('a');

    // Generate the HTML
    var oddeven = ['odd','even'];
    var ret = "<div class='rightclickmenu'> " +
                  "<span class='heading'>" + refRawname + "</span><span class='summary'><br/>" + theLI.innerHTML + 
                  "</span><hr/><div class='items'>";
    var j = 0;
    for (var i=0; i<theAs.length; i++) {
        if ( theAs[i].className.toString().indexOf("external") >= 0 && theAs[i].className.toString().indexOf("text") >= 0 ) {
            ret += "<div class='menuitem "+oddeven[j%2]+"'><a href=\""+theAs[i].href+"\" target='_blank'>" + theAs[i].href +"</a></div>";
            j++;
        }
    }
    ret += "</div></div>";
 
    return ret;
}

function showRefMenu(e)
{
    if (!e) var e = window.event;
    var rightclick;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
 
    var targ;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // Safari bug
        targ = targ.parentNode;
 
    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;
 
    var refrightclickmenu = document.getElementById("refrightclickmenu");
    if (!refrightclickmenu) {
        alert("Unable to locate rightclick-menu!");
        return;
    }
    if (rightclick && keyPressed && targ.tagName.toLowerCase()=="a") {
        refrightclickmenu.innerHTML = getRefMenuHTML(targ.href);
        setMenuPosition (e, refrightclickmenu);
        refrightclickmenu.style.zIndex = "10000";
        refrightclickmenu.style.display = "block";
        return false;
    }
    else 
        refrightclickmenu.style.display = "none";
}

var onRefMenu = false;
$( function () {
 
    // Add right-click menu to each ref link
    var refSups = document.getElementsByTagName("sup");
    if (refSups && refSups.length>0)
        for (var i=0; i<refSups.length; i++) 
            if (refSups[i].getAttribute("class") == "reference") {
                refSups[i].getElementsByTagName('a')[0].onmouseup = showRefMenu;
                refSups[i].getElementsByTagName('a')[0].oncontextmenu = showRefMenu;
            }
    AppendHandlerToEvent( "document.onmousedown", 
                          function() { if(!onRefMenu) document.getElementById('refrightclickmenu').style.display='none'; } );

    var refrightclickmenu = document.createElement("div");
    refrightclickmenu.setAttribute("id", "refrightclickmenu");
    refrightclickmenu.setAttribute("display", "none");
    var theBody = document.getElementsByTagName("body")[0];
    theBody.insertBefore(refrightclickmenu, theBody.childNodes[0]);

    document.getElementById('refrightclickmenu').onmouseover = function() { onRefMenu = true; };
    document.getElementById('refrightclickmenu').onmouseout = function() { onRefMenu = false; };
});