// ==UserScript==
// @name JR Mturk Panda Crazy Helper
// @version 0.2.5
// @namespace https://gf.qytechs.cn/users/6406
// @description A script add on for Panda Crazy sending commands to main script.
// @include http*://*mturk.com/mturk/myhits*
// @include http*://*mturk.com/mturk/findhits*
// @include http*://*mturk.com/mturk/sorthits*
// @include http*://*mturk.com/mturk/viewhits*
// @include http*://*mturk.com/mturk/searchbar*
// @include http*://*mturk.com/mturk/sortsearchbar*
// @include http*://*mturk.com/mturk/previewandaccept?*
// @include http*://*mturk.com/mturk/preview?*
// @include http*://*mturk.com/mturk/continue?*
// @include http*://*mturk.com/mturk/return*
// @include http*://*mturk.com/mturk/submit*
// @include http*://*mturk.com/mturk/dashboard*
// @exclude http*://*mturk.com/mturk/findhits?*hit_scraper*
// @require http://code.jquery.com/jquery-2.1.4.min.js
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
var gScriptVersion = "0.2.5";
var gScriptName = "pandacrazy";
var gLocation = window.location.href;
var gOtherScripts = {"TPE":-1,"HDM":-1,"PEFT":-1};
var gScriptsCounter = {"TPE":-1,"HDM":-1,"PEFT":-1};
var gConstantSearch = null;
var jobDataDefault = {"requesterName":"","requesterId":"","groupId":"","pay":"","title":"","duration":"0","hitsAvailable":0,"searchLink":""};
function createMessageData(command,data) { return {"time":(new Date().getTime()),"command":command,"data":data}; }
function sendCommandMessage(data) { localStorage.setItem("JR_message_" + gScriptName, JSON.stringify(data)); }
function createQueueData(length) { return {"queueLength":length}; }
function createProjectedData(earnings) { return {"projectedEarnings":earnings}; }
function createJobData(jobData) { return {"groupId":jobData.groupId,"title":jobData.title,"requesterName":jobData.requesterName,"requesterId":jobData.requesterId,
"pay":jobData.pay,"duration":jobData.duration,"hitsAvailable":jobData.hitsAvailable}; }
function sendMessageData(command,theData) {
var messageData = createMessageData(command,theData);
sendCommandMessage(messageData);
}
function sendQueueData(queueLength) { sendMessageData("queueData",createQueueData(queueLength)); }
function sendProjectedData(projectedEarnings) { sendMessageData("projectedEarnings",createProjectedData(projectedEarnings)); }
function sendJobData(jobData) { sendMessageData("addJob",createJobData(jobData)); }
function sendJobOnceData(jobData) { sendMessageData("addOnceJob",createJobData(jobData)); }
function sendPingMessage() { localStorage.setItem("JR_message_ping_" + gScriptName, JSON.stringify({"command":"areYouThere","time":(new Date().getTime())})); }
function appendPandaButtons(element,jobData) {
$(element).append($("<span>").html("Add: ")
.append($("<button>").html("Panda").css({"font-size":"10px","line-height":"10px","padding":"1px"})
.data("jobData",jobData)
.click(function(e) { sendJobData($(e.target).data("jobData")); return false; }))
.append($("<button>").html("Once").css({"font-size":"10px","line-height":"10px","padding":"1px"})
.data("jobData",jobData)
.click(function(e) { sendJobOnceData($(e.target).data("jobData")); return false; }))
);
}
function doSortResultsTable() {
var sortResultsTable = $("#sortresults_form").next("table");
$(sortResultsTable).find("> tbody > tr").each(function(i, row) {
var jobData = jQuery.extend(true, {}, jobDataDefault);
var titleElement = $("#capsule" + i + "-0");
jobData.groupId = $(titleElement).closest("tr").find(".capsulelink a").attr("href").split("groupId=")[1];
jobData.title = $(titleElement).text().trim();
var requesterElement = $(row).find(".requesterIdentity");
jobData.requesterName = $(requesterElement).text().trim();
jobData.requesterId = $(requesterElement).closest("a").attr("href").split("&requesterId=")[1];
jobData.duration = $("#duration_to_complete\\.tooltip--" + i).closest("tr").find(".capsule_field_text").text().trim();
jobData.hitsAvailable = $("#number_of_hits\\.tooltip--" + i).closest("tr").find(".capsule_field_text").text().trim();
var rewardTrTarget = $("#reward\\.tooltip--" + i).closest("tr");
jobData.pay = $(rewardTrTarget).find(".reward").text().replace("$","").trim();
$(rewardTrTarget).append($("<td rowspan='2' width='100%'>").append($("<div>")
.attr({"class":"JR_PandaCrazy"}).css({"text-align":"center","width":"100%","display":"inline-block"})));
$(row).find(".JR_PandaCrazy").append($("<span>").html("[ PandaCrazy ]<br>"));
appendPandaButtons($(row).find(".JR_PandaCrazy"),jobData);
});
}
function sendHighestAmount() {
var highestAmount = (gOtherScripts.TPE>gOtherScripts.HDM && gOtherScripts.TPE>gOtherScripts.PEFT) ? gOtherScripts.TPE :
(gOtherScripts.HDM>gOtherScripts.PEFT) ? gOtherScripts.HDM : gOtherScripts.PEFT;
//console.log("highest amount = " + highestAmount);
sendProjectedData(highestAmount);
}
function searchThisUntil(scriptNN,searchThis,doThis,counterMax,reset) {
if (!counterMax) return;
gScriptsCounter[scriptNN]++;
if (reset) gScriptsCounter[scriptNN] = 0;
if (searchThis()) doThis();
else {
if (gScriptsCounter[scriptNN]<counterMax) setTimeout( function() { searchThisUntil(scriptNN,searchThis,doThis,counterMax,false); },1000 );
}
}
function searchTPE(searchTimer) {
searchThisUntil("TPE",function() {
if ($("#TPE_div a:contains('Today\\'s Projected Earnings')").length) return true;
else return false;
},function() {
gOtherScripts.TPE = parseFloat($("#TPE_div .reward").html().replace("$",""));
sendHighestAmount();
},searchTimer,true);
}
function searchHDM(searchTimer) {
searchThisUntil("HDM",function() {
var dbStatus = ($("#hdbStatusText").length) ? $("#hdbStatusText").html() : null;
if (dbStatus!==null && (dbStatus==="" || dbStatus=="Update Complete!")) return true;
else return false;
},function() {
gOtherScripts.HDM = parseFloat($("#projectedDayProgress").attr("value"));
sendHighestAmount();
},searchTimer,true);
}
function searchPEFT(searchTimer) {
searchThisUntil("PEFT",function() {
if ($("a:contains('Projected Earnings for Today')").length) return true;
else return false;
},function() {
var thisPEFT = $("a:contains('Projected Earnings for Today')").closest("td").next().html().replace("$","");
gOtherScripts.PEFT = parseFloat(thisPEFT);
sendHighestAmount();
},searchTimer,true);
}
function findProjectedEarnings() {
// find if Projected Earnings scripts are installed and get info.
if ($("#TPE_div").length && gOtherScripts.TPE==-1) { searchTPE(40);
$("#TPE_div a").click( function() { searchTPE(300); } );}
if ($("#projectedDayProgress").length && gOtherScripts.MHDM==-1) { searchHDM(40);
$("#hdbUpdate").click( function() { searchHDM(2300); } );}
searchPEFT(70);
}
function mainListener(e) {
if ( e.key == 'JR_message_pong_' + gScriptName && gLocation == (JSON.parse(e.newValue).url)) {
window.removeEventListener("storage", mainListener, false);
var noHitsError = ($("td.error_title:contains('There are currently no HITs assigned to you.')").length > 0);
var noHitsAlert = ($("#alertboxHeader:contains('There are currently no HITs assigned to you.')").length > 0);
var returnedAlert = ($("#alertboxHeader:contains('The HIT has been returned.')").length > 0);
if (gLocation.indexOf("mturk.com/mturk/myhits") != -1 || returnedAlert) {
// Get queue number and data then send to Panda Crazy
var queueLength = 0, queueData = null;
if (noHitsError) { sendQueueData(0); }
else {
var numberQueueText = $("td.title_orange_text").html();
if (numberQueueText) {
var words = numberQueueText.trim().split(' ');
if ($.isNumeric(words[words.length - 2])) queueLength = parseInt(words[words.length - 2]);
sendQueueData(queueLength);
}
}
} else if (gLocation.indexOf("mturk/findhits") != -1 || gLocation.indexOf("mturk/sorthits") != -1 || gLocation.indexOf("mturk/sortsearchbar") != -1 ||
gLocation.indexOf("mturk/viewhits") != -1 || gLocation.indexOf("mturk/searchbar") != -1 || noHitsAlert) {
// Add buttons to add pandas for Panda Crazy
doSortResultsTable();
} else if (gLocation.indexOf("mturk.com/mturk/preview") != -1 || gLocation.indexOf("mturk.com/mturk/return") != -1 ||
gLocation.indexOf("mturk.com/mturk/submit") != -1 || gLocation.indexOf("mturk.com/mturk/continue") != -1) {
var jobData = jQuery.extend(true, {}, jobDataDefault);
if ($("#alertBox").text().indexOf("There are no HITs in this group available") != -1) {
jobData.groupId = gLocation.split("groupId=")[1].split("&")[0];
$("#alertBox").append($("<div>").attr({"id":"JR_PandaCrazyTop"}).append($("<span>").html("[Panda Crazy] ")));
appendPandaButtons($("#JR_PandaCrazyTop"),jobData);
doSortResultsTable();
} else {
var capsulelinkElement = $("form[name='hitForm'] .capsulelink_bold");
var tableInfo = $(capsulelinkElement).closest("table").closest("table");
var buttonPosition = $("#theTime").closest("td");
jobData.groupId = (gLocation.indexOf("groupId=") != -1) ? gLocation.split("groupId=")[1].split("&")[0] : "";
jobData.title = $(capsulelinkElement).find("div").text().trim();
jobData.requesterId = ($("input[name='requesterId']:first").val()) ? $("input[name='requesterId']:first").attr("value") : "";
jobData.requesterName = $("#requester\\.tooltip").closest("td").next().text().trim();
jobData.duration = $("#time_left\\.tooltip").closest("td").next().text().trim();
jobData.hitsAvailable = $("#number_of_hits\\.tooltip").closest("td").next().text().trim();
jobData.pay = $("#reward\\.tooltip").closest("td").next().text().replace("$","").replace(" per HIT","").trim();
$(buttonPosition).append($("<div>").attr({"class":"JR_PandaCrazy"}).html("[PC] "));
appendPandaButtons($(".JR_PandaCrazy:first"),jobData);
}
} else if (gLocation.indexOf("mturk.com/mturk/dashboard") != -1) {
setTimeout( findProjectedEarnings,900 );
}
}
}
$(function() {
window.addEventListener("storage", mainListener, false);
setTimeout( function() { sendPingMessage(); }, 500);
});