Search code examples
javascriptfirefoxhtml-tablegreasemonkey

How to delete one table row that has an empty class attribute?


Is it possible for a Greasemonkey script to delete one row which has a null class (<tr class="" ...>)?

The problem is that inside a <tbody> tag there are two rows with a null class. The row to be deleted is the first one.

<table id="sort_table" class="tablesorter">
<thead>
    <tr>
        <th class="blacktext timesroman_italic">This</th>
        <th class="blacktext timesroman_italic">is a</th>
        <th class="blacktext timesroman_italic">header</th>
        <th class="blacktext timesroman_italic">row</th>
    </tr>
</thead>
<tbody>
    <!-- I WOULD LIKE TO DELETE FROM HERE -->
    <tr class="" valign="middle">
        <td class="bluetext timesroman align_middle">First</td>
        <td class="bluetext timesroman align_middle">blank</td>
        <td class="bluetext timesroman align_middle">class</td>
        <td class="bluetext timesroman align_middle">row</td>
    </tr>
    <!-- TO HERE -->

    <!-- BUT NOT FROM HERE -->
    <tr class="" valign="middle">
        <td class="bluetext timesroman align_middle">second</td>
        <td class="bluetext timesroman align_middle">blank</td>
        <td class="bluetext timesroman align_middle">class</td>
        <td class="bluetext timesroman align_middle">row</td>
    </tr>
    <tr class="someclass" valign="middle">
        <td class="bluetext timesroman align_middle">I gots</td>
        <td class="bluetext timesroman align_middle">me</td>
        <td class="bluetext timesroman align_middle">some</td>
        <td class="bluetext timesroman align_middle">class</td>
    </tr>
    <tr valign="middle">
        <td class="bluetext timesroman align_middle">no</td>
        <td class="bluetext timesroman align_middle">class</td>
        <td class="bluetext timesroman align_middle">attribute</td>
        <td class="bluetext timesroman align_middle">row</td>
    </tr>
    <!-- TO HERE -->
</tbody>
</table>


I would like to delete the first "blank class" row. Like this:

deletion scheme


Here's the pseudo-code I came up with but how do I do that in a script? :

  • Go to a table with the id == "sort_table"
  • Ignore the "thead" and go to "tbody", could be while trCount > 1 // "thead" has one "tr" so it should ignore one "tr" to skip to "thead";
  • On "tbody", while trCountf < 2, delete trCountf rows. // trCountf < 2 because we should ignore the second row in "tbody"

Solution

  • Use jQuery and you can do it with one line of code:

    $('#sort_table tbody tr:not([class!=""]):first').remove ();
    

    See "Select elements without any class".

    The complete Greasemonkey script would be like this:

    // ==UserScript==
    // @name     _Remove the first body row that ain't got no class
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
    // @grant    GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    
    $('#sort_table tbody tr:not([class!=""]):first').remove ();
    


    Or see the code at jsFiddle.