Fetch Total Number of Viewable Hits

Fetches the current number of viewable hits on mturk.

  1. // ==UserScript==
  2. // @name Fetch Total Number of Viewable Hits
  3. // @author StubbornlyDesigned
  4. // @description Fetches the current number of viewable hits on mturk.
  5. // @namespace https://greasyfork.org/en/users/35961-stubbornlydesigned
  6. // @version 1.1
  7. // @match https://www.mturk.com/mturk/findhits?match=false*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. var total = 0,
  13. totalPages = 0,
  14. hitsUrl = 'https://www.mturk.com/mturk/findhits?match=false&pageSize=100',
  15. currentUrl = '';
  16.  
  17. function parse(data) {
  18. if(!data.querySelector('td[class="error_title"]')) {
  19. var available = data.querySelectorAll('a[id^="number_of_hits"]');
  20. totalPages = data.forms.hitGroupsForm.querySelector('td:nth-of-type(3) span:first-of-type a:last-of-type').search.match(/pageNumber=(\d+)/)[1];
  21.  
  22. if(available.length) {
  23. [].slice.call(available).forEach(function(el) {
  24. total = (total + Number(el.parentElement.parentElement.lastElementChild.innerText.trim()));
  25. });
  26.  
  27. var next = '';
  28.  
  29. if(data.forms.hitGroupsForm.querySelector('td:nth-of-type(3) span:first-of-type').outerHTML.includes('Next')) {
  30. [].slice.call(data.forms.hitGroupsForm.querySelectorAll('td:nth-of-type(3) span:first-of-type a')).forEach(function(el) {
  31. if(el.innerText.includes('Next')) {
  32. next = el.href + '&pageSize=100';
  33. }
  34. });
  35. }
  36.  
  37. if(next) {
  38. return next;
  39. } else {
  40. return 'completed';
  41. }
  42. }
  43. }
  44.  
  45. throw new Error('You have exceeded the maximum number of page requests.');
  46. }
  47.  
  48. function get(url) {
  49. return new Promise(function(resolve, reject) {
  50. var req = new XMLHttpRequest();
  51. req.open('GET', url);
  52. req.onload = function() {
  53. if (req.status == 200) {
  54. resolve(req.response);
  55. } else {
  56. reject(Error(req.statusText));
  57. }
  58. };
  59. req.responseType = 'document';
  60. req.onerror = function() {
  61. reject(Error("error"));
  62. };
  63. req.send();
  64. });
  65. }
  66.  
  67. function run() {
  68. var url = !currentUrl ? hitsUrl : currentUrl;
  69. get(url)
  70. .then(function(res) {
  71. var nextMove = parse(res);
  72. var currentPage = url.match(/pageNumber=(\d+)/) ? url.match(/pageNumber=(\d+)/)[1] : 1;
  73. document.getElementById('totalAvailableHits').innerText = currentPage + ' / ' + totalPages;
  74. if(nextMove) {
  75. if(nextMove.includes('mturk')) {
  76. currentUrl = nextMove;
  77. setTimeout(function() {run();}, 200);
  78. } else if(nextMove == 'completed') {
  79. document.getElementById('totalAvailableHits').innerText = total;
  80. total = 0;
  81. totalPages = '';
  82. currentUrl = '';
  83. }
  84. }
  85. })
  86. .catch(function(err) {
  87. console.log(err);
  88. setTimeout(function() {run();}, 1000);
  89. });
  90. }
  91.  
  92. function init() {
  93. var el = document.getElementById('user_name_field');
  94. el.setAttribute('id', 'totalAvailableHits');
  95. el.innerText = 'Fetch Total Available Hits';
  96. el.style.cursor = 'pointer';
  97.  
  98. el.addEventListener('click', function() {run();});
  99. }
  100.  
  101. init();
  102. })();