User:Iritscen/TestSpace.js

From OniGalore
< User:Iritscen
Revision as of 23:45, 12 November 2015 by Iritscen (talk | contribs) (...)
Jump to navigation Jump to 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)
  • Microsoft Edge: 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.
/* Put test JS here! */
/***** autosort_sortable ********
 * Auto-sorts sortable tables by specified columns (why is this not built-in?!)
 *
 *  Maintainers: [[User:Iritscen]]
 */

function sortSortableTables()
{
   // Iterate over all <div> elements
   var divs = document.getElementsByTagName("div");
   if (!divs) return;
   for (var i = 0; i < divs.length; i++)
   {
      var theDiv = divs[i];
      var tables = theDiv.getElementsByTagName("table");
      if (!tables) continue;
      for (var j = 0; j < tables.length; j++)
      {
         alert("Looking at table " + j);
         var theTable = tables[j];
         // If we found a sortable table...
         if (hasClass(theTable, "sortable"))
         {
            alert("Found a sortable one.");
            // ...Look for sort button
            var allTHs = theTable.getElementsByTagName("th");
            if (!allTHs){alert("No THs found."); continue;}
            alert("Looking through " + allTHs.length + " THs.");
            for (var k = 0; k < allTHs.length; k++)
            {
               if (hasClass(allTHs[k], "headersort"))
               {
                  alert("Found the arrow.");
                  allTHs[k].trigger(click); // use jQuery's trigger() to send click event to this arrow
               }
            }
         }
      }
   }
}

addOnloadHook(sortSortableTables);