[MTurk Worker] No HIT Reloader

Reloads pages automatically if no HIT is loaded for provided Group Ids

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         [MTurk Worker] No HIT Reloader
// @namespace    https://github.com/Kadauchi
// @version      2.1.1
// @description  Reloads pages automatically if no HIT is loaded for provided Group Ids
// @author       Kadauchi
// @icon         http://i.imgur.com/oGRQwPN.png
// @include      https://worker.mturk.com/*
// @grant        GM_getTab
// @grant        GM_saveTab
// ==/UserScript==

(function () {
    GM_getTab((tab) => {
        const gid = location.href.match(/projects\/([A-Z0-9]+)/) ? location.href.match(/projects\/([A-Z0-9]+)/)[1] : null;
        const gids = localStorage.NHR_gids ? JSON.parse(localStorage.NHR_gids) : {};

        let timeout = null;

        const reloader = () => {
            if (!gid && gids[tab.gid]) {
                timeout = setTimeout(() => {
                    window.location.replace(`https://worker.mturk.com/projects/${tab.gid}/tasks/accept_random`);
                }, 500);
            }
            else {
                clearTimeout(timeout);
            }
        };

        if (location.href.indexOf(`https://worker.mturk.com/projects`) !== -1) {
            if (gid) {
                tab.gid = gid;
            }
            else {
                reloader();
            }

            if (tab.gid) {
                const button = document.createElement(`button`);
                button.title = `Auto Reload ${tab.gid} If No HIT?`;
                button.className = `m-l-sm fa fa-refresh btn ${gids[tab.gid] ? `btn-success` : `btn-default`}`;
                button.addEventListener(`click`, (event) => {
                    button.classList.toggle(`btn-success`);
                    button.classList.toggle(`btn-default`);

                    gids[tab.gid] = button.classList.contains(`btn-success`);
                    localStorage.NHR_gids = JSON.stringify(gids);

                    reloader();
                });

                document.getElementsByClassName(`navbar-content`)[0].appendChild(button);
            }
        }
        else {
            tab.gid = undefined;
        }

        GM_saveTab(tab);
    });
})();