mmmturkeybacon Queue Order Fix

After completing a HIT anywhere within your queue (i.e. HITs Assigned To You), this script will automatically continue the HIT at the top of your queue.

Versión del día 18/03/2015. Echa un vistazo a la versión más reciente.

// ==UserScript==
// @name        mmmturkeybacon Queue Order Fix
// @author      mmmturkeybacon
// @description After completing a HIT anywhere within your queue (i.e. HITs Assigned To You), this script will automatically continue the HIT at the top of your queue. 
// For example, if you sort your queue by Time Left (least first), you can use this script to work on the HITs that are closest to expiring instead of mturk's default behavior ignoring what your queue is sorted by and putting the oldest HIT next in line. You should only need to set the sort order once. HITs accepted after setting the sort order will be sorted automatically by mturk. Additionally you can manually continue the HIT at the top of your queue if you visit "https://www.mturk.com/mturk/myhits?first". Create a bookmark on your toolbar to quickly jump to the first HIT in your queue. This is especially useful if you just caught a HIT that will expire quickly.
// @namespace   http://userscripts.org/users/523367
// @match       https://*.mturk.com/mturk/continue*
// @match       https://*.mturk.com/mturk/submit
// @match       https://*.mturk.com/mturk/return?requesterId=*
// @match       https://*.mturk.com/mturk/myhits?first
// @require     https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @version     2.02
// @grant       none
// ==/UserScript==

/*
 * IMPORTANT: if any GM_ functions are @granted event.result from Confirm Return HIT will be unavailable and this script will cause the HIT to be returned
 * even if Cancel was clicked in Confirm HIT Return.
 */

/*
 * Mechanical Turk has two types of HITs. By far the ExternalQuestion HITs are 
 * the most common. These HITs use an iframe to display the task, disable the 
 * yellow Submit HIT button, and use the hidden link with the id hitExternalNextLink
 * to direct the browser to a new page after the task has been submitted in the
 * iframe. The second type of HIT is the QuestionForm HIT. These HITs do not use
 * an iframe, use the yellow Submit HIT button to submit, and do not use
 * hitExternalNextLink to direct the browser to a new page.
 *
 * For ExternalQuestion HITs, fixing the queue order is as simple as replacing
 * hitExternalNextLink with the URL of the next link you wish to work on.
 *
 * QuestionForm HITs don't use the hitExternalNextLink, however we can utilize
 * the fact that after a QuestionForm HIT is submitted the URL ends with 
 * /mturk/submit to redirect the browser to the top of the queue.
 *
 */

var TIMEOUT_TIMELIMIT = 10000; // 10000 [ms]

$(document).ready(function()
{
    var this_URL = window.location.href.split("mturk.com")[1];
    /* preview?groupId=GROUPIDSTRING&isPreviousIFrame= indicates not in queue
     * preview?isPreviousIFrame= indicates we are in the queue */
    var hitExternalNextLink = $('a[href^="/mturk/preview?isPreviousIFrame="][id="hitExternalNextLink"]');

    if (hitExternalNextLink.length > 0)
    { // this HIT is in the queue pipeline

        /* play nice with MTurk Confirm Return HIT
         * if event.result is true then confirm from Confirm Return HIT returned true
         * but event.result might be undefined because MTurk Confirm Return HIT isn't running
         * so if event.result is not false then return HIT and load HIT at top of the queue. */
        var $return_button = $('a[href^="/mturk/return?"]');
        var return_URL = $return_button.attr('href');
        var return_redirect_URL = '/mturk/myhits?first';
        $return_button.click(function(event)
        {
            if(event.result != false)
            {
                event.preventDefault();
                $.ajax({
                    url: return_URL,
                    type: 'GET',
                    success: function(data)
                    {
                        var $src = $(data);
                        var maxpagerate = $src.find('td[class="error_title"]:contains("You have exceeded the maximum allowed page request rate for this website.")');
                        if (maxpagerate.length == 0)
                        {
                            window.location.replace(return_redirect_URL);
                        }
                        else
                        {
                            alert('mmmturkeybacon Queue Order Fix Error: Maximum allowed page request rate. HIT not returned.');
                        }
                    },
                    error: function(xhr, status, error)
                    {
                        alert('mmmturkeybacon Queue Order Fix Error: Page request error. HIT not returned.');
                    },
                    timeout: TIMEOUT_TIMELIMIT
                });
            }
        });

        if (this_URL.split('?')[0] == '/mturk/continue' && $('img[alt="Submit from within the iframe"][src="/media/submit_hit_disabled.gif"]').length > 0)
        { // ExternalQuestion HIT
          /* Setting hitExternalNextLink in a QuestionForm is harmless but doing
           * so would require an unnecessary $.get page request, so it's best to
           * only change hitExternalNextLink for ExternalQuestion HITs */
            $.get('/mturk/myhits', function(data)
            {
                var first_queue_URL = $(data).find('a[href^="/mturk/continue"]').eq(0).attr('href');
                if (first_queue_URL)
                {
                    if (this_URL == first_queue_URL)
                    {
                        var second_queue_URL = $(data).find('a[href^="/mturk/continue"]').eq(1).attr('href');
                        if (second_queue_URL)
                        {
                            hitExternalNextLink.attr('href', second_queue_URL);
                            //$return_button.click(function(event){if(event.result != false){event.preventDefault(); $.get(return_URL); window.location.replace(second_queue_URL);}});
                            return_redirect_URL = second_queue_URL;
                        }
                    }
                    else
                    {
                        hitExternalNextLink.attr('href', first_queue_URL);
                        return_redirect_URL = first_queue_URL;
                    }
                }
            });
        }
        //else if (this_URL == '/mturk/submit' || this_URL.split('?')[0] == '/mturk/return') // next HIT after a QuestionForm HIT was submitted or next HIT after a HIT was returned
        else if (this_URL == '/mturk/submit') // next HIT after a QuestionForm HIT was submitted
        {
            $.get('/mturk/myhits', function(data)
            {
                var first_queue_URL = $(data).find('a[href^="/mturk/continue"]').eq(0).attr('href');
                if (first_queue_URL)
                {
                    window.location.replace(first_queue_URL);
                }
            });
        }
    }
    else if (this_URL.split('?')[0] == '/mturk/continue' && $('span[id="alertboxHeader"]:contains("You have already")').length > 0)
    {
        var first_queue_URL = $('a[href^="/mturk/continue"]').eq(0).attr('href');
        if (first_queue_URL)
        {
            window.location.replace(first_queue_URL);
        }
    }
    else if (this_URL == '/mturk/myhits?first')
    { // bookmark link was used or next HIT in queue was already completed
        $.get('/mturk/myhits', function(data)
        {
            var first_queue_URL = $(data).find('a[href^="/mturk/continue"]').eq(0).attr('href');
            if (first_queue_URL)
            {
                window.location.replace(first_queue_URL);
            }
        });
    }
    // else it's a submit link but not inside the queue, so do nothing

});