Scripts:Animepage:Sourcecode2

Revision as of 17:22, 9 August 2005 by Der Idiot (talk | contribs)
// ==UserScript==
// @name           select_all_of_a_group
// @namespace      lupin_sanseis_scripts
// @description    selects all files of a release group
// @include        http://anidb.info/*
// ==/UserScript==

(function() {

   // 2D-array containing all the checkbox names corresponding to a group
   checkbox_names = new Array();

   function initialize() {

      var gids = new Array();
      // walk through all tbodies (they are created by the browser DOM internally, they don't have to be in source)
      for (var i=0; i < document.getElementsByTagName("tbody").length; i++) {
         var tbody = document.getElementsByTagName("tbody")[i];

         // the group-listing-table contains no inner table, so it's the wrong tbody if a table is found inside
         match = tbody.innerHTML.match(/table/i);
         if (match) { continue; }

         // search for "Group Info"
         match = tbody.innerHTML.match(/Group Info:/i);
         if (match) {
            // found the right table
            var regexp = /gid=(\d+)"/;
            var tr = tbody.firstChild;

            // walk through all rows of the table
            while (tr.nextSibling) {
               if (tr.nodeType == 1
                && tr.nodeName == 'TR')
               {
                  // create an adittional column to add to the end of the row
                  var td = document.createElement("td");
                  if (match = regexp.exec(tr.innerHTML)) {
                     // found a group-id, so create the subarray for the checkboxnames and the checkbox
                     var gid = match[1];
                     gids.push(gid);
                     checkbox_names[gid] = new Array();
                     var input = document.createElement('input');
                     input.setAttribute('type', 'checkbox');
                     input.setAttribute('id', 'toggle_gid_'+gid);
                     input.setAttribute('onclick', 'toggle('+gid+')');
                     td.appendChild(input);
                  } else
                  if (tr.innerHTML.match(/Group Info:/i)) {
                     td.setAttribute('bgcolor', '#b0b0b0');
                     td.appendChild(document.createTextNode('toggle all'));
                  }
                  tr.appendChild(td);
               }
               tr = tr.nextSibling;
            }
         }
      }

      // if checkbox_names still contains no elements, it's no animelisting --> exit function
      if (checkbox_names.length == 0) { return; }

      // walk through all checkboxes in the page
      for (var i=0; i < document.getElementsByTagName("input").length; i++) {
         var input = document.getElementsByTagName("input")[i];
         if (input.type == 'checkbox'
          && input.name.search(/^madd\.f\.\d+$/) != -1) {
            var regexp = /gid=(\d+)/;

            // if the checkboxname is like madd.f.xyz, try to find the gid in the same table-row
            // and add the checkboxname to the corresponding array
            var tr = input.parentNode.parentNode;
            if (match = regexp.exec(tr.innerHTML)) {
               var gid = match[1];
               if (checkbox_names[gid]) {
                  checkbox_names[gid].push(input.name);
               }
            }
         }
      }

      // add a title attribute to the created checkboxes
      for (var i=0; i < gids.length; i++) {
         document.getElementById('toggle_gid_'+gids[i]).setAttribute('title', 'Toggle all files of this anime-group ('+checkbox_names[gids[i]].length+' files)');
         document.getElementById('toggle_gid_'+gids[i]).parentNode.appendChild(document.createTextNode('('+checkbox_names[gids[i]].length+')'));
      }

      // expand the grouplist too when expanding all file-entries (change href of plus icon)
      for (var i=0; i < document.getElementsByTagName("a").length; i++) {
         var a = document.getElementsByTagName("a")[i];
         if (a.href.match(/expandall/)
          && a.innerHTML.match(/all entries/)) {
            a.href += '&showallag=1';
            break;
         }
      }

      // finally add the function for the checkbox onclick-handler to the document
      // has to be done this way, because of scoping of function names
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.appendChild(document.createTextNode("\
      function toggle(_gid) {\
         for (var i=0; i < checkbox_names[_gid].length; i++) {\
            document.getElementsByName(checkbox_names[_gid][i])[0].checked = document.getElementById('toggle_gid_'+_gid).checked;\
         }\
         if (document.getElementById('toggle_gid_'+_gid).checked) {\
            alert('Selected ' + checkbox_names[_gid].length + ' files. Please check for correctness!');\
         } else {\
            alert('Removed all checkmarks for this group!');\
         }\
      }"));

      document.getElementsByTagName("head")[0].appendChild(script);
   }


   window.addEventListener("load", initialize(), false);

})();
MediaWiki spam blocked by CleanTalk.
MediaWiki spam blocked by CleanTalk.