RS07 Poll Rounding

Fixes rounding errors (upwards), on RS07 polls

  1. // ==UserScript==
  2. // @name RS07 Poll Rounding
  3. // @namespace rs07pr
  4. // @description Fixes rounding errors (upwards), on RS07 polls
  5. // @name RS07 Poll Rounding
  6. // @include http://services.runescape.com/m=poll/oldschool/results.ws?id=*
  7. // @version 1
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. $('fieldset').each(
  12. function() {
  13. table = $(this)[0].getElementsByTagName('table')[0];
  14. yes = table.getElementsByTagName('tr')[0].getElementsByTagName('td')[2];
  15. no = table.getElementsByTagName('tr')[1].getElementsByTagName('td')[2];
  16. votes_yes = parseInt(yes.textContent.match(/^\d+% \((\d+) votes\)$/)[1]);
  17. votes_no = parseInt(no.textContent.match(/^\d+% \((\d+) votes\)$/)[1]);
  18. votes_total = votes_yes + votes_no;
  19. votes_yes_pct = ((votes_yes / votes_total) * 100).toFixed(2);
  20. votes_no_pct = (100 - votes_yes_pct).toFixed(2);
  21. pass_color = (votes_yes_pct >= 75) ? "green" : "red";
  22. pass_votes = Math.ceil(votes_total * .75);
  23. passing_by = Math.abs(pass_votes - votes_yes);
  24. yes.innerHTML = votes_yes_pct + "% (" + numberWithCommas(votes_yes) + " votes)\n[" + "<span style=color:" + pass_color + ";><b>" + numberWithCommas(passing_by) + "</b></span>" + "]";
  25. no.innerHTML = votes_no_pct + "% (" + numberWithCommas(votes_no) + " votes)";
  26. }
  27. );
  28.  
  29. function numberWithCommas(x) {
  30. return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  31. }