Open main menu

UESPWiki β

User:Rpeh/reorderCategories.js

< User:Rpeh

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: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
  var _catFunction = function(sectionName)
  {
    var categoryList = document.getElementById( sectionName );
    if (null != categoryList)
    {
      var catRE=new RegExp(/<[^>]*>/g);
      var strRE=new RegExp(/^[^-]*-/);
      var catList = categoryList.innerHTML.split(/ \| /);
      var first = catList[0].split(": ");
      catList[0] = first[1];
      catList.sort(function(a,b){var A=a.toUpperCase().replace(catRE, ""); var B=b.toUpperCase().replace(catRE, ""); return ((A<B)?-1:(A==B)?0:1);});
      var colLen = catList.length
      var numCols = 1;
      if (colLen > 8)
      {
        colLen = Math.floor((catList.length+2) / 3);
        numCols = 3
      } else if (colLen > 4)
      {
        colLen = Math.floor((catList.length+1) / 2);
        numCols = 2;
      }

      var catTable = document.createElement("table");
      for (var i=0; i<colLen; i++)
      {
        var row = catTable.insertRow(-1);
        for (var r=0; (i+r<catList.length); r+=colLen)
        {
          var cell = row.insertCell(-1);
          cell.innerHTML = catList[i+r];
          var iText = cell.childNodes[0].childNodes[0];
          if (document.all)
          {
            if (iText.innerText.indexOf("-") != -1) iText.innerHTML = iText.innerText.replace(strRE, "");
          } else {
            if (iText.text.indexOf("-") != -1) iText.innerHTML = iText.text.replace(strRE, "");
          }
        }
      }
      categoryList.innerHTML = first[0];
      categoryList.appendChild(catTable);
    }
  }

  function reorderCategories()
  {
    _catFunction("mw-normal-catlinks");
    _catFunction("mw-hidden-catlinks");
  }

  addOnloadHook(reorderCategories);