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 > a:contains('helpful')").parent().prev().each(function () {
helpful += parseInt($(this).text().replace(",",""));
});
$("td > a:contains('declined')").parent().prev().each(function () {
declined += parseInt($(this).text().replace(",",""));
});
$("td > a:contains('disputed')").parent().prev().each(function () {
disputed += parseInt($(this).text().replace(",",""));
});
$("td > a:contains('aged')").parent().prev().each(function () {
aged += parseInt($(this).text().replace(",",""));
});
$("td > a:contains('flags')").parent().prev().each(function () {
total += parseInt($(this).text().replace(",",""));
});
Here is an example of what the markup looks like:
<table id="flag-stat-info-table">
<tbody>
<tr class="group-name">
<td class="col1">441</td>
<td class="col2"><a href="?group=1">post flags</a></td>
</tr>
<tr>
<td class="col1">41</td>
<td class="col2"><a href="?group=1&status=1">waiting for review</a></td>
</tr>
<tr>
<td class="col1">310</td>
<td class="col2"><a href="?group=1&status=2">helpful</a></td>
</tr>
<tr>
<td class="col1">5</td>
<td class="col2"><a href="?group=1&status=3">declined</a></td>
</tr>
<tr>
<td class="col1">36</td>
<td class="col2"><a href="?group=1&status=4">disputed</a></td>
</tr>
<tr>
<td class="col1">49</td>
<td class="col2"><a href="?group=1&status=5">aged away</a></td>
</tr>
<tr class="group-name">
<td class="col1">412</td>
<td class="col2"><a href="?group=4">comment flags</a></td>
</tr>
<tr>
<td class="col1">6</td>
<td class="col2"><a href="?group=4&status=1">waiting for review</a></td>
</tr>
<tr>
<td class="col1">406</td>
<td class="col2"><a href="?group=4&status=2">helpful</a></td>
</tr>
<tr class="group-name">
<td class="col1">5</td>
<td class="col2"><a href="?group=2">spam flags</a></td>
</tr>
<tr>
<td class="col1">5</td>
<td class="col2"><a href="?group=2&status=2">helpful</a></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td></td>
<td class="disabled-link">Clear all filters</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2">Filter your flag history by selecting a category above.</td>
</tr>
</tbody>
</table>
Note: I've only included the relevant part as I didn't want to add a bunch of noise to this post.