Fetch Total Number of Viewable Hits

Fetches the current number of viewable hits on mturk.

当前为 2016-06-05 提交的版本,查看 最新版本

  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.0
  7. // @match https://www.mturk.com/mturk/findhits?match=false
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (() => {
  12. let 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. totalPages = data.forms.hitGroupsForm.querySelector('td:nth-of-type(3) span:first-of-type a:last-of-type').search.match(/pageNumber=(\d+)/)[1]
  20.  
  21. let available = data.querySelectorAll('a[id^="number_of_hits"]')
  22. if(available.length) {
  23. [].slice.call(available).forEach((el) => {
  24. total = (total + Number(el.parentElement.parentElement.lastElementChild.innerText.trim()))
  25. })
  26.  
  27. let 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((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((resolve, reject) => {
  50. let 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. let url = !currentUrl ? hitsUrl : currentUrl
  69. get(url)
  70. .then((res) => {
  71. let nextMove = parse(res)
  72. let 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(() => {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((err) => {
  87. console.log(err)
  88. setTimeout(() => {run()}, 1000)
  89. })
  90. }
  91.  
  92. function init() {
  93. let 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', () => {run()})
  99. }
  100.  
  101. init()
  102. })()