MediaWiki:Common.js

From Tales of Maj'Eyal
Revision as of 14:02, 27 May 2013 by Darkgod (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences
/* Any JavaScript here will be loaded for all users on every page load. */
// ==UserScript==
// @name       TE4 tooltips for wiki
// @namespace 
// @version    1.0
// @description  enter dragon
// @match      http://te4.org/wiki2/*
// @match      http://www.te4.org/wiki2/*
// @match      http://www.te4.org/w/*
// @copyright  2012+, nomnom
// ==/UserScript==

/* MediaWiki:Common.js */
try
{
   /* Any JavaScript here will be loaded for all users on every page load. */
   var tooltip=function()
   {
      var id = 'tooltip_mouseover';
      var top = 3;
      var left = 15;
      var speed = 10;
      var timer = 20;
      var endalpha = 98;
      var alpha = 0;
      var tt,h;
      var ie = document.all ? true : false;
      var ttArray = new Array();
      var ttArrayWidths = new Array();
      var current_article;
      return{
         create:function(v, w)
         {
            if(tt == null)
            {
               tt = document.createElement('div');
               tt.setAttribute('id',id);
               document.body.appendChild(tt);
               tt.style.opacity = 0;
               tt.style.position = 'absolute';
               tt.style.zIndex = 100;
               tt.style.border = '2px solid #000000';
               tt.style.backgroundColor = '#000000';
                    tt.style.color = '#ffffff';
               tt.style.filter = 'alpha(opacity=0)';
               document.onmousemove = this.pos;
            }
            tt.style.display = 'block';
            tt.innerHTML = v;
            tt.style.width = w ? w : 'auto';
            h = parseInt(tt.offsetHeight) + top;
            clearInterval(tt.timer);
            tt.timer = setInterval(function(){tooltip.fade(1)},timer);
         },
         pos:function(e)
         {
            var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
            var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
               
                var doc = document.documentElement, body = document.body;
                var top = (doc && doc.scrollTop  || body && body.scrollTop  || 0);
               
                if( u-h > top )
               tt.style.top = (u-h) + 'px';
                else
                    tt.style.top = (top + 15) + 'px';
               
            if( l+left+parseInt(tt.style.width) > window.innerWidth)
               tt.style.left = (l- left - parseInt(tt.style.width)) + 'px';
                else
                   tt.style.left = (l+left) + 'px';
         },
         fade:function(d)
         {
            var a = alpha;
            if((a != endalpha && d == 1) || (a != 0 && d == -1))
            {
               var i = speed;
               if(endalpha - a < speed && d == 1)
               {
                  i = endalpha - a;
               }else if(alpha < speed && d == -1)
               {
                  i = a;
               }
               alpha = a + (i * d);
               tt.style.opacity = alpha * .01;
               tt.style.filter = 'alpha(opacity=' + alpha + ')';
            }else
            {
               clearInterval(tt.timer);
               if(d == -1){tt.style.display = 'none'}
            }
         },
         hide:function()
         {
            if (tt != null)
            {
               clearInterval(tt.timer);
               tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
            }
         },
         show:function(article, maxwidth)
         {
            current_article = article;
            if (ttArray[article] == null)
            {
               ttArrayWidths[article] = maxwidth;
               tooltip.create('Retrieving information...', maxwidth);
               if (tooltip.access(article) == false)
               {
                  tt.innerHTML = 'Sorry, your browser is not compatible with tooltips.';
               }
            }
            else
            {
               tooltip.create(ttArray[article], ttArrayWidths[article]);
            }
         },
         update:function(httpRequest,article)
         {
            if (httpRequest.readyState == 4)
            {
               if (httpRequest.status == 200)
               {
                  if (tt != null && current_article == article)
                  {
                     var searchStr = new RegExp('Click here to start this page!');
                     if ((httpRequest.responseText).search(searchStr) != -1)
                     {
                        tt.innerHTML = 'This article does not yet exist.';
                     }
                     else
                     {
                        searchStr = '<span id="tooltipstart"></span>';
                        var startPoint = (httpRequest.responseText).indexOf(searchStr) + searchStr.length;
                        if (startPoint != -1)
                        {
                           var endPoint = (httpRequest.responseText).lastIndexOf('<span id="tooltipend"></span>');
                           if (endPoint != -1)
                           {
                              tt.innerHTML = (httpRequest.responseText).substr(startPoint,endPoint-startPoint);
                              ttArray[article] = tt.innerHTML;
                              return;
                           }
                        }
                        tt.innerHTML = 'This article does not yet have a properly formatted tooltip.';
                     }
                  }
                  h = parseInt(tt.offsetHeight) + top;
               }
               else
               {
                  if (tt != null)
                  {
                     tt.innerHTML = 'Could not retrieve information.';
                  }
               }
            }
         },
         access:function(article)
         {
            var httpRequest;
            if (window.XMLHttpRequest)
            {
               httpRequest = new XMLHttpRequest();
               if (httpRequest.overrideMimeType)
               {
                  httpRequest.overrideMimeType('text/xml');
               }
            }
            else if (window.ActiveXObject)
            {
               try
               {
                  httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
               }
               catch (e)
               {
                  try
                  {
                     httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  catch (e) {}
               }
            }
            if (!httpRequest)
            {
               // Browser incompatible with AJAX
               return false;
            }
            httpRequest.onreadystatechange = function()
            {
               tooltip.update(httpRequest,article);
            };
            httpRequest.open('GET', 'http://te4.org/w/api.php?format=txt&action=parse&page='+article+'&prop=text', true);
            httpRequest.send();
         }
      };
   }();
   /* Check for tooltip links when document finishes loading */
   function mouseoverTooltip(article, maxwidth) {
      return function()
      {
         article = article.replace(".27","'");
         tooltip.show(article, maxwidth);
      };
   }
   function putTooltips()
   {
      var spanArray = document.getElementsByTagName("span");
      for (var i=0 ; i<spanArray.length ; i++)
      {
         if (spanArray[i].className == "tooltip_link")
         {
                var links = spanArray[i].getElementsByTagName("a");
                var tlink = spanArray[i].getElementsByTagName("a")[0];
                if(typeof(tlink) != "undefined")
                {
                    var article = (tlink.href).substr(tlink.href.lastIndexOf('/') + 1);
                    spanArray[i].onmouseover = mouseoverTooltip(article, spanArray[i].style.maxWidth);
                    spanArray[i].onmouseout = function(){ tooltip.hide() };
                    // Change link inside to mouseover link style
                    if (tlink && tlink.className != "new")
                    {
                        tlink.title = "";
                        tlink.style.color = spanArray[i].style.color;
                    }
                }
         }
      }
   }
   if (window.addEventListener)
      window.addEventListener("load", putTooltips, false);
   else if (window.attachEvent)
      window.attachEvent("onload", putTooltips);
   else if (document.getElementById)
      window.onload=putTooltips();
}
catch(err)
{
   window._customJSerror = err;
}