Mturk PandaPush

Mturk PandaPush; Pushbullet for Panda Crazy

As of 2020-04-25. See the latest version.

// ==UserScript==
// @name         Mturk PandaPush
// @version      0.1
// @description  Mturk PandaPush; Pushbullet for Panda Crazy
// @author       Skar3
// @include     https://worker.mturk.com/requesters/PandaPush/projects
// @require     https://code.jquery.com/jquery-3.4.1.slim.min.js
// @require     https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js
// @require     https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js
// @require     https://greasyfork.org/scripts/401969-pushbullet-js/code/pushbullet-js.js?version=797701
// @resource    BootstrapCSS          https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css
// @grant       GM_addStyle
// @grant       GM_getResourceText
// @grant       GM_getResourceURL
// @namespace https://greasyfork.org/users/541458
// ==/UserScript==


document.head.appendChild(cssElement(GM_getResourceURL ("BootstrapCSS")));
document.title = 'PandaPush';

function cssElement(url) {
    var link = document.createElement("link");
    link.href = url;
    link.rel="stylesheet";
    link.type="text/css";
    return link;
}


(function() {
    'use strict';

    function initPage() {
        var pageHtml = '<header><nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">' +
            '<a class="navbar-brand" href="#">PandaPush</a>' +
            '</nav>' +
            '</header>' +
            '<main role="main" class="flex-shrink-0"><div class="container" style="padding-top: 60px;">'+
            '<form>' +
            '<div class="form-group">' +
                '<label for="pushbulletApi">Pushbullet API Key</label>' +
                '<input type="text" class="form-control" id="pushbulletApi">' +
            '</div>' +
            '<div class="form-group">' +
                '<label for="enablePush">Enable</label>' +
                '<input type="checkbox" checked class="form-control" id="enablePush">' +
            '</div>' +
            '<a href="#" id="api-save" class="btn btn-primary">Save</button>' +
            '</form>' +
            '</div></main>';
        $("body").addClass('d-flex flex-column h-100');
        $("body").html(pageHtml);

        $("#api-save").on( "click", function() {
            console.log($("#pushbulletApi").val());
            localStorage.setItem("PANDAPUSH_api_key", $("#pushbulletApi").val())
        });

        var key = localStorage.getItem("PANDAPUSH_api_key");
        if (key != null) {
            $("#pushbulletApi").val(key);
        }
    }

    var senthits = [];


    function sendPush(hit) {
        PushBullet.APIKey = $("#pushbulletApi").val();
        var pushBody = 'PAY: ' + hit.pay + "\r\nDURATION: " + hit.duration + "\r\n " + hit.description;
        PushBullet.push("link", null, null, {title: hit.title, url: hit.continueURL, body: pushBody}, function(err, res) {
            if(err) {
                throw err;
            } else {
                console.log("Push sent");
            }
        });
    }

    function getPandaQueue() {
        var queue = localStorage.getItem("JR_QUEUE_StoreData");
        if (queue != null) {
            queue = JSON.parse(queue);
            for (var i=0; i < queue.queue.length; i++) {
                var hit = queue.queue[i];
                if (senthits.indexOf(hit.assignmentId) == -1) {
                    if ($('#enablePush').is(":checked")) {
                        sendPush(hit);
                        senthits.push(hit.assignmentId);
                    }
                }
            }
        }
    }

    setTimeout(function(){
    	console.log('PandPush loaded');
        initPage();
        setInterval(getPandaQueue, 1000);
    }, 1000);
})();