Swabucks Survey Effort Calculator

Provides the SB/Minute for each survey on the swagbucks answer page

  1. // ==UserScript==
  2. // @name Swabucks Survey Effort Calculator
  3. // @namespace http://your.homepage/
  4. // @version 1.0
  5. // @license MIT
  6. // @description Provides the SB/Minute for each survey on the swagbucks answer page
  7. // @author You
  8. // @match https://www.swagbucks.com/surveys*
  9. // @grant none
  10. // @require https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js
  11. // ==/UserScript==
  12.  
  13. // Generates the Rate column header element and returns it to be used by the main function
  14. function generateRateColumnHeader() {
  15. var rateColumnHeader = document.createElement('th');
  16. rateColumnHeader.className = "surveyEffort table-sortable table-sortable:numeric";
  17. rateColumnHeader.innerHTML = '<span class="sort">SB/Minute</span>';
  18. return rateColumnHeader;
  19. }
  20.  
  21. // generate the table cell with the calculated rate
  22. function generateRateCell(row) {
  23. var rateCell = document.createElement('td');
  24. rateCell.className = 'surveyRate';
  25. rateCell.innerHTML = `${calculateSurveyRate(row)} SB/min`;
  26. return rateCell;
  27. }
  28.  
  29. function calculateSurveyRate(surveyRow) {
  30. // Pull the row attributes out for the calculation
  31. var time = parseInt(surveyRow.getAttribute('loi'));
  32. var reward = parseInt(surveyRow.getAttribute('reward'));
  33. var bonus = parseInt(surveyRow.getAttribute('bonus'));
  34. // make sure time isn't falsy so we don't try to divide by 0
  35. if( time ) {
  36. var rate = (reward + bonus)/time;
  37. return _.round(rate, 2);
  38. }
  39. // Return 0 if we can't calculate the rate so it gets a low priority in any sorting
  40. return 0;
  41. }
  42.  
  43. // Loop through all of the table bodies and pull out the one we care about because it's compiled without an ID to reference
  44. function findCompiledSurveyTbody() {
  45. var tbodyElements = document.getElementsByTagName('tbody');
  46. var surveyTbody;
  47. _.forEach(tbodyElements, element => {
  48. // The compiled tbody doesn't have an id so we need to mine that out of the array of table bodies
  49. if( !element.id ) {
  50. surveyTbody = element;
  51. }
  52. });
  53. return surveyTbody;
  54. }
  55.  
  56. // Get the Survey times and amounts
  57. var time = document.getElementsByClassName('surveyTime');
  58. var sb = document.getElementsByClassName('surveySB');
  59.  
  60. // Append a Rate column header and make it sortable
  61. var tableHeader = document.getElementById("surveysTHead");
  62. var headerRow = tableHeader.children[0];
  63. headerRow.appendChild(generateRateColumnHeader());
  64.  
  65. // Get the tbody element to append the rate cell to
  66. var surveyTableBody = findCompiledSurveyTbody();
  67. var surveyTableRows = _.get(surveyTableBody, 'children', []);
  68. _.forEach(surveyTableBody.children, child => {
  69. var rateCell = generateRateCell(child);
  70. child.appendChild(rateCell);
  71. });