User:DatRoot/Scripts/RichRefTooltips.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
/*
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;
}();});