UserScript that summarizes Stack Exchange flag counts I am working on a UserScript that displays total flag counts and other information on the Flag Summary page. 

Example:



Everything is working correctly but this bit of code seems rather repetitive and I'm sure it could be improved upon.

The following code pulls the total count of each flag type from the flag summary page. 

Is there anything I can do to improve or shorten this code? 

var helpful = 0,
    declined = 0,
    disputed = 0,
    aged = 0,
    total = 0;

$("td &gt; a:contains('helpful')").parent().prev().each(function () {
    helpful += parseInt($(this).text().replace(",",""));
});

$("td &gt; a:contains('declined')").parent().prev().each(function () {
    declined += parseInt($(this).text().replace(",",""));
});

$("td &gt; a:contains('disputed')").parent().prev().each(function () {
    disputed += parseInt($(this).text().replace(",",""));
});

$("td &gt; a:contains('aged')").parent().prev().each(function () {
    aged += parseInt($(this).text().replace(",",""));
});

$("td &gt; a:contains('flags')").parent().prev().each(function () {
    total += parseInt($(this).text().replace(",",""));
});


Here is an example of what the markup looks like:

&lt;table id="flag-stat-info-table"&gt;
    &lt;tbody&gt;
        &lt;tr class="group-name"&gt;
            &lt;td class="col1"&gt;441&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=1"&gt;post flags&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="col1"&gt;41&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=1&amp;amp;status=1"&gt;waiting for review&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="col1"&gt;310&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=1&amp;amp;status=2"&gt;helpful&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="col1"&gt;5&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=1&amp;amp;status=3"&gt;declined&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="col1"&gt;36&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=1&amp;amp;status=4"&gt;disputed&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="col1"&gt;49&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=1&amp;amp;status=5"&gt;aged away&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class="group-name"&gt;
            &lt;td class="col1"&gt;412&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=4"&gt;comment flags&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="col1"&gt;6&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=4&amp;amp;status=1"&gt;waiting for review&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="col1"&gt;406&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=4&amp;amp;status=2"&gt;helpful&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr class="group-name"&gt;
            &lt;td class="col1"&gt;5&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=2"&gt;spam flags&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="col1"&gt;5&lt;/td&gt;
            &lt;td class="col2"&gt;&lt;a href="?group=2&amp;amp;status=2"&gt;helpful&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td class="disabled-link"&gt;Clear all filters&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td colspan="2"&gt;Filter your flag history by selecting a category above.&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;


Note: I've only included the relevant part as I didn't want to add a bunch of noise to this post.
I don't particularly have an issue with your HTML, however your JavaScript could be improved.

Currently, you have a lot of duplicate logic. You should move it to a function that each case can call.

Like the following:

function flagCount(selector){
    var count = 0;
    $(selector).parent().prev().each(function () {
        count += parseInt($(this).text().replace(",", ""));
    });
    return count;
}



If I pass 0 into parseInt without the second parameter, it will return a hex number...


Using Number is better, and more readable.

Leaving you totally with:

function flagCount(selector){
    var count = 0;
    $(selector).parent().prev().each(function () {
        count += Number($(this).text().replace(",", ""));
    });
    return count;
}
var helpful = flagCount("td &gt; a:contains('helpful')");
var declined = flagCount("td &gt; a:contains('declined')");
var disputed = flagCount("td &gt; a:contains('disputed')");
var aged = flagCount("td &gt; a:contains('aged')");
var total = flagCount("td &gt; a:contains('flags')");


Note, you could string concatenate the selector inside the function, but that limits the use case that you select another DOM element.