User:Phlip/utilities.js

From Homestar Runner Wiki

< User:Phlip(Difference between revisions)
Jump to: navigation, search
m (replace all)
m (Finally getting around to fixing this... getArticleNamespace didn't work on HRWiki: or HRWiki_talk: pages, 'cause wgCanonicalNamespace was 'Project' instead)
 
(includes 12 intermediate revisions)
Line 21: Line 21:
   utilities.getArticleURL = function()
   utilities.getArticleURL = function()
   {
   {
-
     if (typeof(utilities.cachedarticleurl) != "undefined")
+
     return utilities.fullurl(utilities.getArticleTitle(), utilities.getArticleNamespace());
-
      return utilities.cachedarticleurl;
+
-
 
+
-
    // find the print footer, it has a canonical page URL in it
+
-
    // DON'T use window.location, as it can have tags like ?action=edit and things which will mess us up
+
-
 
+
-
    // why is there no getElementsByAttr?
+
-
    var a = document.getElementsByTagName('DIV');
+
-
    var pageUrl = ""
+
-
    for (var i = 0; i < a.length; i++)
+
-
    {
+
-
      if (a[i].className == "printfooter")
+
-
      {
+
-
        // sometimes it's <div class="printfooter"><p>Retrieved from <a>url</a></p></div>
+
-
        // sometimes it's <div class="printfooter">Retrieved from <a>url</a></div>
+
-
        // depends on version and skin...
+
-
        var b = utilities.getChildType(a[i], "P", 0);
+
-
        if (!b) b = a[i];
+
-
        b = utilities.getChildType(b, "A", 0);
+
-
        if (b)
+
-
          pageUrl = b.href;
+
-
        break;
+
-
      }
+
-
    }
+
-
    return utilities.cachedarticleurl = pageUrl;
+
   }
   }
   utilities.getArticleFullTitle = function()
   utilities.getArticleFullTitle = function()
   {
   {
-
     if (typeof(utilities.cachedarticlefulltitle) != "undefined")
+
     return wgPageName;
-
      return utilities.cachedarticlefulltitle;
+
-
    var url = utilities.getArticleURL();
+
-
    // the url will either be http://<path>/Title or http://<path>?title=Title, depending on the wiki's settings.
+
-
    // either way, the title is at the end, and a fixed distance from the start.
+
-
    // CHANGETHISLINE if you want to use it on another wiki
+
-
    var title = url.substring(32); // 32 == "http://www.hrwiki.org/index.php/".length
+
-
    title = unescape(title.replace(/_/g, " "));
+
-
    return utilities.cachedarticlefulltitle = title;
+
   }
   }
-
   utilities.namespaces = new Array("", "Talk", "User", "User talk", "HRWiki", "HRWiki talk", "Image", "Image talk", "MediaWiki", "MediaWiki talk", "Template", "Template talk", "Help", "Help talk", "Category", "Category talk");
+
   utilities.namespaces = new Array("", "Talk", "User", "User talk", "HRWiki", "HRWiki talk", "File", "File talk", "MediaWiki", "MediaWiki talk", "Template", "Template talk", "Help", "Help talk", "Category", "Category talk", "STUFF", "Subtitles", "Subtitles talk");
   // arrays start at 0... add the negatives after
   // arrays start at 0... add the negatives after
   // probably nonstandard, but works everywhere I've tried it
   // probably nonstandard, but works everywhere I've tried it
   utilities.namespaces[-1] = "Special";
   utilities.namespaces[-1] = "Special";
   utilities.namespaces[-2] = "Media";
   utilities.namespaces[-2] = "Media";
-
   utilities.namespacedescription = new Array("Article", "Discussion", "User Page", "Discussion", "Project Page", "Discussion", "File", "Discussion", "Message", "Discussion", "Template", "Discussion", "Help", "Discussion", "Category", "Discussion", "Special Page", "File");
+
  utilities.namespacesrev = new Object();
 +
  for (var i = -2; i < utilities.namespaces.length; i++)
 +
    utilities.namespacesrev[utilities.namespaces[i].toLowerCase()] = i;
 +
  // Other namespace aliases
 +
  utilities.namespacesrev["project"] = 4; // HRWiki:
 +
  utilities.namespacesrev["project talk"] = 5; // HRWiki_talk:
 +
  utilities.namespacesrev["image"] = 6; // File:
 +
  utilities.namespacesrev["image talk"] = 7; // File_talk:
 +
   utilities.namespacedescription = new Array("Article", "Discussion", "User Page", "Discussion", "Project Page", "Discussion", "File", "Discussion", "Message", "Discussion", "Template", "Discussion", "Help", "Discussion", "Category", "Discussion", "STUFF", "Subtitles", "Discussion");
   utilities.namespacedescription[-1] = "Special Page";
   utilities.namespacedescription[-1] = "Special Page";
   utilities.namespacedescription[-2] = "File";
   utilities.namespacedescription[-2] = "File";
-
   utilities.articlenamespace = new Array(0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, 16, 17);
+
   utilities.articlenamespace = new Array(0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, 16, 17, 17);
   utilities.articlenamespace[-1] = -1;
   utilities.articlenamespace[-1] = -1;
   utilities.articlenamespace[-2] = -2;
   utilities.articlenamespace[-2] = -2;
-
   utilities.talknamespace =    new Array(1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11, 13, 13, 15, 15, 16, 7);
+
   utilities.talknamespace =    new Array(1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11, 13, 13, 15, 15, undefined, 18, 18);
   //utilities.talknamespace[-1] = undefined;
   //utilities.talknamespace[-1] = undefined;
-
   utilities.talknamespace[-2] = 7; // ie Image_talk:
+
   utilities.talknamespace[-2] = 7; // ie File_talk:
   utilities.getArticleNamespace = function()
   utilities.getArticleNamespace = function()
Line 81: Line 57:
     if (typeof(utilities.cachedarticlenamespace) != "undefined")
     if (typeof(utilities.cachedarticlenamespace) != "undefined")
       return utilities.cachedarticlenamespace;
       return utilities.cachedarticlenamespace;
-
     var title = utilities.getArticleFullTitle();
+
     var searchNS = wgCanonicalNamespace.toLowerCase().replace('_',' ');
-
    var i = title.indexOf(':');
+
     if (typeof(utilities.namespacesrev[searchNS]) != "undefined")
-
     if (i < 0)
+
      return utilities.cachedarticlenamespace = utilities.namespacesrev[searchNS];
 +
    else
       return utilities.cachedarticlenamespace = 0;
       return utilities.cachedarticlenamespace = 0;
-
    var namespacepart = title.substring(0, i).toLowerCase();
 
-
    // find it by number, this will also reject not-real namespaces
 
-
    // eg on pages like 12:00
 
-
    for (i = -2; i < utilities.namespaces.length; i++)
 
-
      if (namespacepart == utilities.namespaces[i].toLowerCase())
 
-
        return utilities.cachedarticlenamespace = i;
 
-
    return utilities.cachedarticlenamespace = 0;
 
   }
   }
   utilities.getArticleTitle = function()
   utilities.getArticleTitle = function()
   {
   {
-
     if (typeof(utilities.cachedarticletitle) != "undefined")
+
     return wgTitle;
-
      return utilities.cachedarticletitle;
+
-
    var ns = utilities.getArticleNamespace();
+
-
    if (ns == 0)
+
-
      return utilities.cachedarticletitle = utilities.getArticleFullTitle();
+
-
    else
+
-
      return utilities.cachedarticletitle = utilities.getArticleFullTitle().substring(utilities.namespaces[ns].length + 1);
+
   }
   }
Line 119: Line 83:
       if (namespace)
       if (namespace)
         title = utilities.namespaces[namespace] + ":" + title;
         title = utilities.namespaces[namespace] + ":" + title;
-
       title = escape(title.replace(/ /g, "_"));
+
       title = encodeURIComponent(title.replace(/ /g, "_")).replace("%3A",":").replace("%2F","/");
       // CHANGETHISLINE if you want to use it on another wiki
       // CHANGETHISLINE if you want to use it on another wiki
-
       return "/index.php?title=" + title + "&" + extras;
+
       return "/w/index.php?title=" + title + "&" + extras;
     }
     }
     else
     else
Line 127: Line 91:
       if (namespace)
       if (namespace)
         title = utilities.namespaces[namespace] + ":" + title;
         title = utilities.namespaces[namespace] + ":" + title;
-
       title = escape(title.replace(/ /g, "_"));
+
       title = encodeURIComponent(title.replace(/ /g, "_")).replace("%3A",":").replace("%2F","/");
       // CHANGETHISLINE if you want to use it on another wiki
       // CHANGETHISLINE if you want to use it on another wiki
-
       return "/index.php/" + title
+
       return "/wiki/" + title
     }
     }
 +
  }
 +
  utilities.fullurl = function(title, namespace, extras)
 +
  {
 +
    return wgServer + utilities.localurl(title, namespace, extras)
   }
   }

Current revision as of 08:51, 26 August 2010

// protect from double inclusions
if (!window.utilities)
{
  window.utilities = new Object();

  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // General DOM Helpers
  utilities.getChildType = function(node, type, n)
  {
    if (!node)
      return null;
    a = node.getElementsByTagName(type);
    if (a.length >= n)
      return a[n];
    else
      return null;
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Getting article details
  utilities.getArticleURL = function()
  {
    return utilities.fullurl(utilities.getArticleTitle(), utilities.getArticleNamespace());
  }

  utilities.getArticleFullTitle = function()
  {
    return wgPageName;
  }

  utilities.namespaces = new Array("", "Talk", "User", "User talk", "HRWiki", "HRWiki talk", "File", "File talk", "MediaWiki", "MediaWiki talk", "Template", "Template talk", "Help", "Help talk", "Category", "Category talk", "STUFF", "Subtitles", "Subtitles talk");
  // arrays start at 0... add the negatives after
  // probably nonstandard, but works everywhere I've tried it
  utilities.namespaces[-1] = "Special";
  utilities.namespaces[-2] = "Media";
  utilities.namespacesrev = new Object();
  for (var i = -2; i < utilities.namespaces.length; i++)
    utilities.namespacesrev[utilities.namespaces[i].toLowerCase()] = i;
  // Other namespace aliases
  utilities.namespacesrev["project"] = 4; // HRWiki:
  utilities.namespacesrev["project talk"] = 5; // HRWiki_talk:
  utilities.namespacesrev["image"] = 6; // File:
  utilities.namespacesrev["image talk"] = 7; // File_talk:
  utilities.namespacedescription = new Array("Article", "Discussion", "User Page", "Discussion", "Project Page", "Discussion", "File", "Discussion", "Message", "Discussion", "Template", "Discussion", "Help", "Discussion", "Category", "Discussion", "STUFF", "Subtitles", "Discussion");
  utilities.namespacedescription[-1] = "Special Page";
  utilities.namespacedescription[-2] = "File";
  utilities.articlenamespace = new Array(0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, 16, 17, 17);
  utilities.articlenamespace[-1] = -1;
  utilities.articlenamespace[-2] = -2;
  utilities.talknamespace =    new Array(1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11, 13, 13, 15, 15, undefined, 18, 18);
  //utilities.talknamespace[-1] = undefined;
  utilities.talknamespace[-2] = 7; // ie File_talk:

  utilities.getArticleNamespace = function()
  {
    // use typeof, otherwise it won't use the cache if the ns is 0 (main namespace)
    if (typeof(utilities.cachedarticlenamespace) != "undefined")
      return utilities.cachedarticlenamespace;
    var searchNS = wgCanonicalNamespace.toLowerCase().replace('_',' ');
    if (typeof(utilities.namespacesrev[searchNS]) != "undefined")
      return utilities.cachedarticlenamespace = utilities.namespacesrev[searchNS];
    else
      return utilities.cachedarticlenamespace = 0;
  }
  utilities.getArticleTitle = function()
  {
    return wgTitle;
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Miscellaneous functions

  // this can be used as, for example:
  // utilities.localurl("Main Page")
  // utilities.localurl("User:Me") (less preferred)
  // utilities.localurl("Me", 2) (more preferred)
  // utilities.localurl("Me", 2, "action=edit")
  // utilities.localurl(utilities.getArticleTitle(), utilities.getArticleNamespace(), "action=edit") (an "edit this page" link)
  utilities.localurl = function(title, namespace, extras)
  {
    if (extras)
    {
      if (namespace)
        title = utilities.namespaces[namespace] + ":" + title;
      title = encodeURIComponent(title.replace(/ /g, "_")).replace("%3A",":").replace("%2F","/");
      // CHANGETHISLINE if you want to use it on another wiki
      return "/w/index.php?title=" + title + "&" + extras;
    }
    else
    {
      if (namespace)
        title = utilities.namespaces[namespace] + ":" + title;
      title = encodeURIComponent(title.replace(/ /g, "_")).replace("%3A",":").replace("%2F","/");
      // CHANGETHISLINE if you want to use it on another wiki
      return "/wiki/" + title
    }
  }
  utilities.fullurl = function(title, namespace, extras)
  {
    return wgServer + utilities.localurl(title, namespace, extras)
  }

  // this is probably not useful for user scripts (it will always return the same thing in the same script)
  // it is used below for the skin-specific functions
  utilities.getSkin = function()
  {
    // this catches most skins, but not all...
    if (window.skin)
      return window.skin;
    // this is being called from a user script - so there will at least be somewhere a <script> tag
    // with a src of User:Username/skin.js... so this should trigger at least once.
    // this will need to be updated for all skins that are annoying and don't set the skin variable
    // if you know a better way to do this, please suggest it...
    var scripts = document.getElementsByTagName('script')
    for (var i = 0; i < scripts.length; i++)
    {
      if (scripts[i].src && scripts[i].src.indexOf("cologneblue") >= 0)
        return window.skin = "cologneblue";
      if (scripts[i].src && scripts[i].src.indexOf("nostalgia") >= 0)
        return window.skin = "nostalgia";
      if (scripts[i].src && scripts[i].src.indexOf("standard") >= 0)
        return window.skin = "standard";
      if (scripts[i].src && scripts[i].src.indexOf("tavistyle") >= 0)
        return window.skin = "tavistyle";
    }
    return "(unknown skin)";
  }
  // again, this is used by the skin-specific functins and is probably useless to a user script
  utilities.wrongSkinWarningMessage = function(functionname, wantedskin)
  {
    if (utilities.alreadyDidAWarningMessage) return;
    utilities.alreadyDidAWarningMessage = true;
    var div = document.createElement('div');
    var img = document.createElement('img');
    // CHANGETHISLINE if you want to use it on another wiki
    img.src = "http://www.hrwiki.org/images/thumb/5/53/Exclamation_Mark.png/30px-Exclamation_Mark.png";
    div.appendChild(img);
    div.appendChild(document.createTextNode(" Your "));
    var link = document.createElement('a');
    link.href = utilities.localurl("Mypage/" + utilities.getSkin() + ".js", -1);
    link.appendChild(document.createTextNode("custom JavaScript for " + utilities.getSkin()));
    div.appendChild(link);
    div.appendChild(document.createTextNode(" uses "));
    var code = document.createElement('code');
    code.appendChild(document.createTextNode(functionname));
    div.appendChild(code);
    div.appendChild(document.createTextNode(", which is specific to " + wantedskin + "."));
    div.setAttribute("style", "border: 1px solid red; background: #FCC; padding: 0.5em; text-align: center");
    var where = document.getElementById('bodyContent');
    if (!where) where = document.getElementById('article');
    if (!where) where = document.getElementById('content');
    if (!where) where = document.body;
    where.insertBefore(div, where.firstChild);
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Monobook-specific code

  // adds a link to the "quick bar" (the one with links to your user page, talk, log out, etc)
  utilities.addMonobookQuickbarLink = function(href, title, before)
  {
    if (utilities.getSkin() != "monobook")
    {
      utilities.wrongSkinWarningMessage("utilities.addMonobookQuickbarLink", "Monobook");
      return;
    }

    var li = document.createElement('li');
    var a = document.createElement('a');
    a.appendChild(document.createTextNode(title));
    a.href = href;
    li.appendChild(a);
    if (!before)
    {
      document.getElementById('pt-logout').parentNode.appendChild(li);
    }
    else
    {
      before = document.getElementById('pt-'+before);
      before.parentNode.insertBefore(li, before);
    }
    return li;
  }
  // adds a link to the tab bar (the one with links to the article, talk page, edit, etc)
  utilities.addMonobookTab = function(href, title, padding, atstart)
  {
    if (utilities.getSkin() != "monobook")
    {
      utilities.wrongSkinWarningMessage("utilities.addMonobookTab", "Monobook");
      return;
    }

    var li = document.createElement('li');
    var a = document.createElement('a');
    a.appendChild(document.createTextNode(title));
    a.href = href;
    li.appendChild(a);
    if (padding)
      if (atstart)
        li.style.marginRight = "1.6em";
      else
        li.style.marginLeft = "1.6em";
    var where = document.getElementById('p-cactions');
    where = utilities.getChildType(where, 'ul', 0);
    if (atstart)
      where.insertBefore(li, where.firstChild);
    else
      where.appendChild(li);
    return li;
  }
  // adds a link to the given navbox (the boxes on the left)
  // It uses the "navigation" one by default (the one with links to the Main page, community portal, etc)
  utilities.addMonobookNavboxLink = function(href, title, whichnavbox, atstart)
  {
    if (utilities.getSkin() != "monobook")
    {
      utilities.wrongSkinWarningMessage("utilities.addMonobookNavboxLink", "Monobook");
      return;
    }

    if (!whichnavbox) whichnavbox = "navigation";
    var li = document.createElement('li');
    var a = document.createElement('a');
    a.appendChild(document.createTextNode(title));
    a.href = href;
    li.appendChild(a);
    var where = document.getElementById('p-' + whichnavbox);
    where = utilities.getChildType(where, 'ul', 0);
    if (atstart)
      where.insertBefore(li, where.firstChild);
    else
      where.appendChild(li);
    return li;
  }
  // copies the top tab bar to the bottom of the page
  // note: if you use addMonobookTab as well, call this *last*
  utilities.addMonobookBottomTabs = function(nocss)
  {
    if (utilities.getSkin() != "monobook")
    {
      utilities.wrongSkinWarningMessage("utilities.addMonobookBottomTabs", "Monobook");
      return;
    }
    if (!nocss)
    {
      // style is modified from the defaults in /skins/monobook/main.css
      // Just adding a <style> object dynamically doesn't work in MSIE, so have to work with document.styleSheets directly.
      // I can't find a way to add a *new* stylesheet, luckily that doesn't matter for CSS, just add it to the first one.
      var newstyle = new Array(
        {selector: "#bottomtabs", rule: "margin: -0.3em 0 0  11.5em; white-space: nowrap; width: 76%; line-height: 1.1em; overflow: visible; background: none; border-collapse: collapse; padding-left: 1em; list-style: none; font-size: 95%;"},
        {selector: "#bottomtabs .hiddenStructure", rule: "display: none;"},
        {selector: "#bottomtabs ul", rule: "list-style: none;"},
        {selector: "#bottomtabs li", rule: "display: inline; border: 1px solid #aaa; border-top: none; padding: .1em 0 0 0; margin: 0 .3em 0 0; overflow: visible; background: white;"},
        {selector: "#bottomtabs li.selected", rule: "border-color: #fabd23; padding: .2em 0 0 0;"},
        {selector: "#bottomtabs li a", rule: "background-color: white; color: #002bb8; border: none; padding: .3em .8em 0 .8em; text-decoration: none; text-transform: lowercase; position: relative; z-index: 0; margin: 0;"},
        {selector: "#bottomtabs .selected a", rule: "z-index: 3;"},
        {selector: "#bottomtabs .new a", rule: "color: #ba0000;"},
        {selector: "#bottomtabs li a:hover", rule: "z-index: 3; text-decoration: none;"},
        {selector: "#bottomtabs h5", rule: "display: none;"},
        {selector: "#bottomtabs li.istalk", rule: "margin-right: 0;"},
        {selector: "#bottomtabs li.istalk a", rule: "padding-right: .5em;"},
        {selector: "#bottomtabs #bottomtabs-ca-addsection a", rule: "padding-left: .4em; padding-right: .4em;"},
        {selector: "li#bottomtabs-ca-talk", rule: "margin-right: 1.6em;"},
        {selector: "li#bottomtabs-ca-watch", rule: "margin-left: 1.6em;"},
        {selector: "li#bottomtabs-ca-unwatch", rule: "margin-left: 1.6em;"},
        {selector: "li#bottomtabs-ca-varlang-0", rule: "margin-left: 1.6em;"},
        {selector: "li#bottomtabs-ca-print", rule: "margin-left: 1.6em;"}
      );
      // as much as I hate browser detection, we need to load MSIE-specific fixes...
      // modified from /skins/monobook/IE*Fixes.css
      var browser = navigator.userAgent.toLowerCase();
      if (browser.indexOf("windows") > 0 && browser.indexOf("msie 5.0") > 0)
      {
        newstyle[newstyle.length] = {selector: "#bottomtabs li", rule: "float: left; padding-top: 0 !important; padding-bottom: 0; height: 0.9em;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li a", rule: "display: block; padding-top: 0.045em; margin-top: -0.045em;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li.selected a", rule: "padding-top: 0.17em; margin-top: -0.17em;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li a:hover", rule: "padding-top: 0.17em;  margin-top: -0.17em;"};
      }
      else if (browser.indexOf("windows") > 0 && browser.indexOf("msie 5.5") > 0)
      {
        newstyle[newstyle.length] = {selector: "#bottomtabs", rule: "width: 76% !important; z-index: 3 !important; float: none"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li", rule: "padding-top: 0 !imporant; border: none; background-color: transparent; cursor: default; float: none !important;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li a", rule: "display: inline-block !important; vertical-align: bottom; padding-top: 0; border: solid #aaa; border-width: 0 1px 1px;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li.selected a", rule: "border-color: #fabd23; padding-top: 0.17em; margin-top: -0.17em;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li a:hover", rule: "padding-top: 0.17em; margin-top: -0.17em;"};
      }
      else if (browser.indexOf("windows") > 0 && browser.indexOf("msie 6") > 0)
      {
        newstyle[newstyle.length] = {selector: "#bottomtabs", rule: "z-index: 3"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li", rule: "padding-top: 0 !important; border: none; background-color: transparent; cursor: default; float: none !important;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li a", rule: "display: inline-block !important; vertical-align: top; padding-top: 0; border: solid #aaa; border-width: 0 1px 1px;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li.selected a", rule: "border-color: #fabd23; padding-top: 0.17em; margin-top: -0.17em;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li a:hover", rule: "padding-top: 0.17em; margin-top: -0.17em;"};
      }
      else if (browser.indexOf("windows") > 0 && browser.indexOf("msie 7") > 0)
      {
        //UNTESTED, I don't have access to MSIE7. It's identical to MSIE6 though, so should be fine.
        newstyle[newstyle.length] = {selector: "#bottomtabs", rule: "z-index: 3"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li", rule: "padding-top: 0 !important; border: none; background-color: transparent; cursor: default; float: none !important;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li a", rule: "display: inline-block !important; vertical-align: top; padding-top: 0; border: solid #aaa; border-width: 0 1px 1px;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li.selected a", rule: "border-color: #fabd23; padding-top: 0.17em; margin-top: -0.17em;"};
        newstyle[newstyle.length] = {selector: "#bottomtabs li a:hover", rule: "padding-top: 0.17em; margin-top: -0.17em;"};
      }
      var stylesheet = document.styleSheets[0];
      if (stylesheet.insertRule) // DOM
      {
        var numrules = stylesheet.cssRules ? stylesheet.cssRules.length : stylesheet.rules.length;
        for (var i = 0; i < newstyle.length; i++)
          stylesheet.insertRule(newstyle[i].selector + "{" + newstyle[i].rule + "}", numrules + i);
      }
      else if (stylesheet.addRule) // MSIE (grr)
      {
        for (var i = 0; i < newstyle.length; i++)
          stylesheet.addRule(newstyle[i].selector, newstyle[i].rule);
      }
    }

    var tabs = document.getElementById('p-cactions').cloneNode(true);
    tabs.id = 'bottomtabs';
    var listitems = tabs.getElementsByTagName('li');
    for (i=0;i<listitems.length;i++)
      if(listitems[i].id)
        listitems[i].id = 'bottomtabs-' + listitems[i].id;
    document.getElementById('column-content').appendChild(tabs);
    return tabs;
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Cologne blue-specific code

  // Cologne Blue's "you have new messages" message isn't nearly as in-your-face as Monobook's...
  // This lets you remedy that.
  utilities.cologneBlueNewMessages = function()
  {
    if (utilities.getSkin() != 'cologneblue')
    {
      utilities.wrongSkinWarningMessage("utilities.cologneBlueNewMessages", "Cologne Blue");
      return;
    }
    // if you have new messages, there is a "*" next to the My Talk link in the quickbar
    var quickbar = document.getElementById('quickbar');
    if (!quickbar)
      return false;
    var talklink = 0;
    for (i = 0; i < quickbar.childNodes.length; i++)
    {
      if (quickbar.childNodes[i].tagName == "A")
      {
        if (quickbar.childNodes[i].href.match(/User_talk:Phlip$/))
        {
          talklink = quickbar.childNodes[i];
          break;
        }
      }
    }
    if (!talklink)
      return false
    // nodeType 3 is "Text node" (nodeType 1 is "Element node")
    if (talklink.nextSibling.nodeType == 3 && talklink.nextSibling.nodeValue.indexOf('*') >= 0)
      return true;
    return false;
  }
  // adds a link to the Cologne Blue quick bar (the one on the left)
  utilities.addCologneBlueQuickbarLink = function(href, title, section, atstart)
  {
    if (utilities.getSkin() != 'cologneblue')
    {
      utilities.wrongSkinWarningMessage("utilities.addCologneBlueQuickbarLink", "Cologne Blue");
      return;
    }
    var quickbar = document.getElementById('quickbar');
    if (!quickbar)
      return; // the quickbar isn't always visible
    var link = document.createElement('a');
    link.href = href;
    link.appendChild(document.createTextNode(title));
    if (section)
    {
      var sectionlc = section.toLowerCase();
      var headers = quickbar.getElementsByTagName('h6');
      var i;
      for (i = 0; i < headers.length; i++)
        if (headers[i].firstChild.nodeValue.toLowerCase() == sectionlc)
          break;
      if (i >= headers.length)
      {
        headers[i] = document.createElement('h6');
        headers[i].appendChild(document.createTextNode(section));
        quickbar.appendChild(headers[i]);
      }
      var before = false;
      if (atstart)
        before = headers[i].nextSibling;
      if (before)
      {
        quickbar.insertBefore(link, before);
        quickbar.insertBefore(document.createElement('br'), before);
      }
      else
      {
        if (headers[i+1])
        {
          // sometimes there's an extra <br> at the end... make sure we don't end up with a double
          var lastnode = headers[i+1].previousSibling;
          while (lastnode && lastnode.nodeType == 3) lastnode = lastnode.previousSibling;
          if (!lastnode || (lastnode.nodeName.toLowerCase() != 'br' && lastnode.nodeName.toLowerCase() != 'h6'))
            quickbar.insertBefore(document.createElement('br'), headers[i+1]);
          quickbar.insertBefore(link, headers[i+1]);
        }
        else
        {
          // at end of the last section... just use appendChild
          var lastnode = quickbar.lastChild;
          while (lastnode && lastnode.nodeType == 3) lastnode = lastnode.previousSibling;
          if (!lastnode || (lastnode.nodeName.toLowerCase() != 'br' && lastnode.nodeName.toLowerCase() != 'h6'))
            quickbar.appendChild(document.createElement('br'));
          quickbar.appendChild(link);
        }
      }
    }
    else
    {
      // not sure what to do if they don't give a section...
      // it's kinda weird... either add at top of first section or bottom of last...
      // still, I want it to be optional, this'll have to do...
      var before = false;
      if (atstart)
        before = utilities.getChildType(quickbar, 'a', 0);
      if (before)
      {
        quickbar.insertBefore(link, before);
        quickbar.insertBefore(document.createElement('br'), before);
      }
      else
      {
        var lastnode = quickbar.lastChild;
        while (lastnode && lastnode.nodeType == 3) lastnode = lastnode.previousSibling;
        if (!lastnode || (lastnode.nodeName.toLowerCase() != 'br' && lastnode.nodeName.toLowerCase() != 'h6'))
          quickbar.appendChild(document.createElement('br'));
        quickbar.appendChild(link);
      }
    }
    return link;
  }
  utilities.addCologneBlueTopbarLink = function(href, title, atstart)
  {
    if (utilities.getSkin() != 'cologneblue')
    {
      utilities.wrongSkinWarningMessage("utilities.addCologneBlueTopbarLink", "Cologne Blue");
      return;
    }
    var topbar = document.getElementById('topbar');
    topbar = utilities.getChildType(topbar, "table", 0);
    topbar = utilities.getChildType(topbar, "tr", 0);
    topbar = utilities.getChildType(topbar, "td", 1);
    var link = document.createElement('a');
    link.href = href;
    link.appendChild(document.createTextNode(title));
    if (atstart)
    {
      topbar.insertBefore(document.createTextNode(" | "), topbar.firstChild);
      topbar.insertBefore(link, topbar.firstChild);
    }
    else
    {
      topbar.appendChild(document.createTextNode(" | "));
      topbar.appendChild(link);
    }
    return link;
  }
  utilities.addCologneBlueSubtopbarLink = function(href, title, atstart)
  {
    if (utilities.getSkin() != 'cologneblue')
    {
      utilities.wrongSkinWarningMessage("utilities.addCologneBlueSubtopbarLink", "Cologne Blue");
      return;
    }
    var topbar = document.getElementById('topbar');
    topbar = utilities.getChildType(topbar, "table", 0);
    topbar = utilities.getChildType(topbar, "tr", 1);
    topbar = utilities.getChildType(topbar, "td", 1);
    topbar = utilities.getChildType(topbar, "span", 0);
    var link = document.createElement('a');
    link.href = href;
    link.appendChild(document.createTextNode(title));
    if (atstart)
    {
      var breaks = topbar.getElementsByTagName('br');
      if (breaks && breaks.length > 0)
      {
        var lastbreak = breaks[breaks.length - 1];
        if (lastbreak.nextSibling)
        {
          topbar.insertBefore(document.createTextNode(" | "), lastbreak.nextSibling);
          topbar.insertBefore(link, lastbreak.nextSibling);
        }
        else
        {
          topbar.appendChild(link);
        }
      }
      else
      {
        topbar.insertBefore(document.createTextNode(" | "), topbar.firstChild);
        topbar.insertBefore(link, topbar.firstChild);
      }
    }
    else
    {
      topbar.appendChild(document.createTextNode(" | "));
      topbar.appendChild(link);
    }
    return link;
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Tavistyle-specific code

  utilities.addTavistyleTopbarLink = function(href, title, row, atstart)
  {
    if (utilities.getSkin() != 'tavistyle')
    {
      utilities.wrongSkinWarningMessage("utilities.addTavistyleTopbarLink", "Tavistyle");
      return;
    }
    var topbar = document.getElementById('topbar');
    topbar = utilities.getChildType(topbar, 'div', 0);
    if (topbar.id == "siteNotice")
    {
      topbar = topbar.nextSibling;
      while (topbar.nodeType == 3)
        topbar = topbar.nextSibling;
    }
    topbar = utilities.getChildType(topbar, 'td', 0);
    breaks = topbar.getElementsByTagName('br');
    var link = document.createElement('a');
    link.href = href;
    link.appendChild(document.createTextNode(title));
    if (breaks.length < row)
    {
      topbar.appendChild(document.createElement('br'));
      topbar.appendChild(link);
    }
    else if (breaks.length == row && !atstart)
    {
      topbar.appendChild(document.createTextNode(" \xB7 ")); // "\xB7" is &middot;
      topbar.appendChild(link);
    }
    else if (atstart)
    {
      topbar.insertBefore(document.createTextNode(" \xB7 "), breaks[row - 1].nextSibling);
      topbar.insertBefore(link, breaks[row - 1].nextSibling);
    }
    else
    {
      topbar.insertBefore(document.createTextNode(" \xB7 "), breaks[row]);
      topbar.insertBefore(link, breaks[row]);
    }
    return link;
  }
  utilities.addTavistyleBottombarLink = function(href, title, row, atstart)
  {
    if (utilities.getSkin() != 'tavistyle')
    {
      utilities.wrongSkinWarningMessage("utilities.addTavistyleBottombarLink", "Tavistyle");
      return;
    }
    var topbar = document.getElementById('footer');
    topbar = utilities.getChildType(topbar, 'div', 0);
    topbar = utilities.getChildType(topbar, 'div', 0);
    breaks = topbar.getElementsByTagName('br');
    var link = document.createElement('a');
    link.href = href;
    link.appendChild(document.createTextNode(title));
    var numbreaks = breaks.length - 4;
    if (row <= 1 && atstart)
    {
      topbar.insertBefore(document.createTextNode(" \xB7 "), topbar.firstChild);
      topbar.insertBefore(link, topbar.firstChild);
    }
    else if (numbreaks < row - 1)
    {
      topbar.insertBefore(document.createElement('br'), breaks[numbreaks].nextSibling);
      topbar.insertBefore(link, breaks[numbreaks].nextSibling);
    }
    else if (atstart)
    {
      topbar.insertBefore(document.createTextNode(" \xB7 "), breaks[row - 2].nextSibling);
      topbar.insertBefore(link, breaks[row - 2].nextSibling);
    }
    else
    {
      topbar.insertBefore(document.createTextNode(" \xB7 "), breaks[row - 1]);
      topbar.insertBefore(link, breaks[row - 1]);
    }
    return link;
  }
}
Personal tools