您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Fetches the current number of viewable hits on mturk.
当前为
- // ==UserScript==
- // @name Fetch Total Number of Viewable Hits
- // @author StubbornlyDesigned
- // @description Fetches the current number of viewable hits on mturk.
- // @namespace https://greasyfork.org/en/users/35961-stubbornlydesigned
- // @version 1.0
- // @match https://www.mturk.com/mturk/findhits?match=false
- // @grant none
- // ==/UserScript==
- (() => {
- let total = 0,
- totalPages = 0,
- hitsUrl = 'https://www.mturk.com/mturk/findhits?match=false&pageSize=100',
- currentUrl = ''
- function parse(data) {
- if(!data.querySelector('td[class="error_title"]')) {
- totalPages = data.forms.hitGroupsForm.querySelector('td:nth-of-type(3) span:first-of-type a:last-of-type').search.match(/pageNumber=(\d+)/)[1]
- let available = data.querySelectorAll('a[id^="number_of_hits"]')
- if(available.length) {
- [].slice.call(available).forEach((el) => {
- total = (total + Number(el.parentElement.parentElement.lastElementChild.innerText.trim()))
- })
- let next = ''
- if(data.forms.hitGroupsForm.querySelector('td:nth-of-type(3) span:first-of-type').outerHTML.includes('Next')) {
- [].slice.call(data.forms.hitGroupsForm.querySelectorAll('td:nth-of-type(3) span:first-of-type a')).forEach((el) => {
- if(el.innerText.includes('Next')) {
- next = el.href + '&pageSize=100'
- }
- })
- }
- if(next) {
- return next
- } else {
- return 'completed'
- }
- }
- }
- throw new Error('You have exceeded the maximum number of page requests.')
- }
- function get(url) {
- return new Promise((resolve, reject) => {
- let req = new XMLHttpRequest()
- req.open('GET', url)
- req.onload = function() {
- if (req.status == 200) {
- resolve(req.response)
- } else {
- reject(Error(req.statusText))
- }
- }
- req.responseType = 'document'
- req.onerror = function() {
- reject(Error("error"))
- }
- req.send()
- })
- }
- function run() {
- let url = !currentUrl ? hitsUrl : currentUrl
- get(url)
- .then((res) => {
- let nextMove = parse(res)
- let currentPage = url.match(/pageNumber=(\d+)/) ? url.match(/pageNumber=(\d+)/)[1] : 1
- document.getElementById('totalAvailableHits').innerText = currentPage + ' / ' + totalPages
- if(nextMove) {
- if(nextMove.includes('mturk')) {
- currentUrl = nextMove
- setTimeout(() => {run()}, 200)
- } else if(nextMove == 'completed') {
- document.getElementById('totalAvailableHits').innerText = total
- total = 0
- totalPages = ''
- currentUrl = ''
- }
- }
- })
- .catch((err) => {
- console.log(err)
- setTimeout(() => {run()}, 1000)
- })
- }
- function init() {
- let el = document.getElementById('user_name_field')
- el.setAttribute('id', 'totalAvailableHits')
- el.innerText = 'Fetch Total Available Hits'
- el.style.cursor = 'pointer'
- el.addEventListener('click', () => {run()})
- }
- init()
- })()