1

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"
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jackusCTB
  • 7
  • 4
  • This is definitely possible. Have you tried writing this code yourself? You'll probably find that you'll get more help with fixing an almost-working script than by essentially asking someone to write it all for you. – jb_314 Nov 28 '13 at 02:18
  • This doesn't work... works flawlessly! I hope you didn't waste too much time figuring out this workaround. Don't know how to thank you! Hope the best for you, mr. Brock! – jackusCTB Nov 28 '13 at 05:57
  • You're welcome! Once you get the hang if it, problems like this are quick and easy. Cleaning up the post took longer than figuring out the answer. – Brock Adams Nov 28 '13 at 06:25

1 Answers1

0

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.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295