User:DatRoot/Scripts/RichRefTooltips.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.
/*
RichRefTooltips

Adds rich html tooltips to reference subscripts in mainspace articles. 
It takes the html from the corresponding footnote, therefore giving 
clickable links.

This script is still in beta but seems to work ok. Works for me in latest versions of IE, Firefox & Opera.

To use add:

importScript("User:DatRoot/Scripts/RichRefTooltips.js");

to your js file. You can also customise the delay for showing/hiding the tooltip by adding underneath:

addOnloadHook(function() {
DatRoot.RichRefTooltips.showDelay = 400;
DatRoot.RichRefTooltips.hideDelay = 400;
});

substituting in the values you want (defaults are 400ms).
*/

importScript("User:DatRoot/Scripts/Common.js");

addOnloadHook(function(){ DatRoot.RichRefTooltips = function()
{
    var obj = DatRoot.createContextFloater();
    
    if(wgNamespaceNumber == 0 && wgAction == "view")
    {
        obj.onShow = function()
        {
            var linkElem = obj.sourceElement.getElementsByTagName("a")[0];
            if(linkElem)
            {
                linkElem.title = "";
                // Get footnote id from link href and add html content to link tooltip
                var linkHref = linkElem.href;
                var noteElem = 
                    document.getElementById(linkHref.substr(linkHref.indexOf("#") + 1));
                if(noteElem) 
                {
                    var dispElem = obj.displayElement;
                    dispElem.innerHTML = noteElem.innerHTML;
                    
                    // Remove the first child, which is always the caret
                    dispElem.removeChild(dispElem.firstChild);
                    // Remove from the front all superscript links 
                    // (actually just hide them for ease & to save time)
                    for(var node = dispElem.firstChild; node != null; node = node.nextSibling)
                    {
                        if(node.nodeType == 3) continue;
                        if(node.firstChild && node.firstChild.nodeName == "SUP") 
                            node.style.display = "none";
                        else break;
                    }
                }
            }
        };
        
        DatRoot.addOnloadHook(function()
        {
            var refElems = 
                getElementsByClassName(DatRoot.getArticleElement(), "SUP", "reference");
            for(var i = 0; i < refElems.length; i++)
            {
                obj.applyToElement(refElems[i]);
            }
        }, "RichRefToolTips");
    }
    
    return obj;
}();});