// ==UserScript==
// @name New HIT Monitor v2
// @description Monitors new HIT page for search terms
// @version 2.1
// @namespace www.redpandanetwork.org
// @include https://www.mturk.com/HM
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant GM_openInTab
// @grant GM_setClipboard
// ==/UserScript==
document.title = "New HIT Monitor";
document.getElementsByTagName('table')[4].innerHTML = "";
headline = document.createElement("h1");
headline.innerHTML = "HIT Monitor";
headline.style.color = '#324F17'
headline.style.fontSize = '48'
headline.style.textAlign = 'center'
headline.style.textDecoration = "underline";
headline.style.marginBottom = "0px";
document.getElementsByTagName('table')[0].innerHTML = "";
document.getElementsByTagName('table')[0].appendChild(headline);
document.getElementById('subtabs_and_searchbar').style.visibility = "hidden";
document.getElementsByTagName('table')[0].style.border = "thick solid #324F17";
document.getElementsByTagName('table')[0].style.height = "100%";
var whites = document.createElement('div');
document.getElementsByTagName('table')[0].appendChild(whites);
whites.style.textAlign = 'center'
whites.style.marginBottom = "35px";
var search = document.createElement("input");
search.type = "button";
search.value = "Add Search";
search.style.height ="30px";
search.style.width ="100px";
search.style.margin = "5px";
whites.appendChild(search);
search.style.background = "#FFFFFF";
search.style.color = 'rgb(50, 79, 23)';
search.style.fontWeight = 'bold';
search.style.border = "thick solid #324F17";
search.addEventListener("click", addNew, false);
var run = document.createElement("input");
run.type = "button";
run.value = "Run";
run.style.height ="30px";
run.style.width ="100px";
run.style.margin = "5px";
whites.appendChild(run);
run.style.background = "#FFFFFF";
run.style.color = 'rgb(50, 79, 23)';
run.style.fontWeight = 'bold';
run.style.border = "thick solid #324F17";
run.addEventListener("click", scan, false);
var config = document.createElement("input");
config.type = "button";
config.value = "Configure";
config.style.height ="30px";
config.style.width ="100px";
config.style.margin = "5px";
whites.appendChild(config);
config.style.background = "#FFFFFF";
config.style.color = 'rgb(50, 79, 23)';
config.style.fontWeight = 'bold';
config.style.border = "thick solid #324F17";
config.addEventListener("click", configure, false);
var greens = document.createElement('div');
document.getElementsByTagName('table')[0].appendChild(greens);
var scanTime = document.createElement('div');
document.getElementsByTagName('table')[0].appendChild(scanTime);
scanTime.innerHTML = "Last scan:";
scanTime.style.color = '#324F17'
scanTime.style.fontSize = '16'
scanTime.style.fontWeight = 'bold';
scanTime.style.marginLeft = '6px';
scanTime.style.marginTop = '18px';
var logs = document.createElement('div');
document.getElementsByTagName('table')[0].appendChild(logs);
logs.style.color = '#324F17'
logs.style.fontSize = '16'
logs.style.fontWeight = 'bold';
logs.style.marginLeft = '6px';
logs.style.marginTop = '18px';
var recent = document.createElement('div');
document.getElementsByTagName('table')[0].appendChild(recent);
recent.style.color = '#324F17'
recent.style.fontSize = '12'
recent.style.fontWeight = 'bold';
recent.style.marginLeft = '6px';
recent.style.marginTop = '18px';
var dataClip = document.createElement('div');
document.getElementsByTagName('table')[0].appendChild(dataClip);
var copy = document.createElement("input");
copy.type = "button";
copy.value = "Copy Database to Clipboard";
copy.style.height ="30px";
copy.style.width ="200px";
copy.style.margin = "6px";
copy.style.marginTop = "35px";
dataClip.appendChild(copy);
copy.style.background = "#FFFFFF";
copy.style.color = 'rgb(50, 79, 23)';
copy.style.fontWeight = 'bold';
copy.style.border = "thick solid #324F17";
copy.addEventListener("click", copydb, false);
function copydb(){
GM_setClipboard(logArray);
alert('Database copied!');
}
var importer = document.createElement("input");
importer.type = "button";
importer.value = "Import Search List";
importer.style.height ="30px";
importer.style.width ="200px";
importer.style.margin = "6px";
importer.style.marginTop = "35px";
dataClip.appendChild(importer);
importer.style.background = "#FFFFFF";
importer.style.color = 'rgb(50, 79, 23)';
importer.style.fontWeight = 'bold';
importer.style.border = "thick solid #324F17";
importer.addEventListener("click", importerer, false);
function importerer(){
var importList = prompt('Import search list.\n\n**Warning** This will delete your current search list.');
if (importList.length > 0){
(GM_setValue("combined", importList));
alert('Search list imported. Reload page.')
}
}
var exporter = document.createElement("input");
exporter.type = "button";
exporter.value = "Export Search List";
exporter.style.height ="30px";
exporter.style.width ="200px";
exporter.style.margin = "6px";
exporter.style.marginTop = "35px";
dataClip.appendChild(exporter);
exporter.style.background = "#FFFFFF";
exporter.style.color = 'rgb(50, 79, 23)';
exporter.style.fontWeight = 'bold';
exporter.style.border = "thick solid #324F17";
exporter.addEventListener("click", exporterer, false);
function exporterer(){
GM_setClipboard(GM_getValue("combined"));
alert('Search list copied to clipboard');
};
var combined = [];
var dump = [];
var logArray = [];
var temp = [];
if (window.GM_getValue("dataBase")){
var dataGet = JSON.parse(GM_getValue("dataBase"));
for (f=0; f < dataGet.length; f++){
logArray.push(dataGet[f]);
};
};
if (window.GM_getValue("sleepDelay")){} else{
GM_setValue("sleepDelay",120);
};
if (window.GM_getValue("scanDelay")){} else{
GM_setValue("scanDelay",10);
};
if (window.GM_getValue("autoLaunch")){} else{
GM_setValue("autoLaunch","false");
};
if (window.GM_getValue("combined")){
var nameGet = JSON.parse(GM_getValue("combined"));
for (f=0; f < nameGet.length; f++){
combined.push(nameGet[f]);
};
var alpha = combined.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
for (f=0; f < combined.length; f++){
var add = document.createElement("input");
add.type = "button";
add.setAttribute("class","nameButton");
add.setAttribute("title", alpha[f].split('|*|')[1]);
add.value = alpha[f].split('|*|')[0];
add.style.marginRight = "0";
add.style.height ="25px";
add.style.width ="auto";
add.style.margin = "5px";
add.style.fontWeight = 'bold';
add.style.border = "thick solid #324F17";
greens.appendChild(add);
add.style.background = 'rgb(50, 79, 23)';
add.style.color = "#FFFFFF";
};
var nameButtons = document.getElementsByClassName('nameButton');
for (f = 0; f < nameButtons.length; f++){
nameButtons[f].addEventListener("click", toggle, false);
nameButtons[f].addEventListener("dblclick", scrap, false);
};
function toggle(){
if (this.style.background ==='rgb(50, 79, 23)'){
this.style.background = 'rgb(255,0,0)';
this.style.border = "thick solid rgb(255,0,0)"
var buttonName = this.value;
for (f = 0; f < combined.length; f++){
if (combined[f].split('|*|')[0].trim() === buttonName.trim()){
dump.push(combined[f]);
combined.splice(f,1);
};
};
}
else {
this.style.background = 'rgb(50, 79, 23)';
this.style.border = 'thick solid rgb(50, 79, 23)';
var buttonName = this.value;
for (f = 0; f < dump.length; f++){
if (dump[f].split('|*|')[0].trim() === buttonName.trim()){
combined.push(dump[f]);
dump.splice(f,1);
};
};
};
};
function scrap(){
var permarray = window.GM_getValue("combined");
for (f = 0; f < combined.length; f++){
if (combined[f].split('|*|')[0].trim() === this.value.trim()){
combined.splice(f,1);
GM_setValue("combined", JSON.stringify(combined));
this.remove();
};
};
};
};
function addNew(){
var searchIn = prompt('Enter search term.\n\nYour search term can be anything that can be\
found from the search page, including inside of links. This means that you can use requester IDs and\
group IDs in addition to requester names, HIT titles, key words and description text.\n\n Search terms\
are not case sensitive.');
if (searchIn !== null){
var nameIn = prompt('Enter name.\n\nThe name is what will appear on your buttons and in alerts, but\
is not used in searches.');
};
if ((searchIn !== null) && (nameIn !== null)){
combined.push(nameIn + "|*|" + searchIn);
GM_setValue("combined", JSON.stringify(combined));
var add = document.createElement("input");
add.type = "button";
add.setAttribute("class","nameButton");
add.setAttribute("title", searchIn);
add.value = nameIn;
add.style.marginRight = "0";
add.style.height ="25px";
add.style.width ="auto";
add.style.margin = "5px";
greens.appendChild(add);
add.style.background = 'rgb(50, 79, 23)';
add.style.color = "#FFFFFF";
add.style.fontWeight = 'bold';
add.style.border = "thick solid #324F17";
add.addEventListener("click", toggle, false);
add.addEventListener("dblclick", scrap, false);
};
};
function configure(){
var scanDelay = prompt('Enter scan delay.\n\nScan delay is the number of seconds betweeen searches of the new HITs page.');
GM_setValue("scanDelay", scanDelay);
var sleepDelay = prompt('Enter sleep delay.\n\nSleep delay is the number of seconds a search term will be disabled for\
after that term is found');
GM_setValue("sleepDelay", sleepDelay);
var autoLaunch = prompt('Set auto launch.\n\nAuto launch causes a search tab to be automatically opened when\
a search term is found. Set this value to true to enable auto launch. Any other value will disable auto launch.');
GM_setValue("autoLaunch", autoLaunch);
};
GM_setValue("run", "off");
function scan(){
if (GM_getValue("run") === "running"){
GM_setValue("run", "off");
run.value = "Run";
run.style.background = 'rgb(255, 255, 255)';
run.style.color = 'rgb(50, 79, 23)';
} else {
GM_setValue("run", "running");
run.value = "Running";
run.style.color = 'rgb(255, 255, 255)';
run.style.background = 'rgb(50, 79, 23)';
parse();
};
};
function parse(){
var d = new Date();
var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
var fullTime = d.getMonth() + "/" + d.getDay() + "/" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
scanTime.innerHTML = "Last scan: " + time;
if (GM_getValue("run") === "running"){
GM_xmlhttpRequest({
method: "GET",
url: "https://www.mturk.com/mturk/searchbar?selectedSearchType=hitgroups&sortType=LastUpdatedTime%3A1",
onload: function(response) {
for (f = 0; f < combined.length; f++){
if (response.responseText.toLowerCase().indexOf(combined[f].split('|*|')[1].toLowerCase()) !== -1){
if (response.responseText.toLowerCase().indexOf('groupid=' + combined[f].split('|*|')[1].toLowerCase()) !== -1){
var linko = "https://www.mturk.com/mturk/preview?groupId=" + combined[f].split('|*|')[1];
}
else if (response.responseText.toLowerCase().indexOf('requesterid=' + combined[f].split('|*|')[1].toLowerCase()) !== -1){
var linko = "https://www.mturk.com/mturk/searchbar?selectedSearchType=hitgroups&requesterId=" + combined[f].split('|*|')[1];
}
else {
var linko = "https://www.mturk.com/mturk/searchbar?selectedSearchType=hitgroups&searchWords=" + combined[f].split('|*|')[1] + "&minReward=0.00&x=0&y=0";
}
if (window.GM_getValue("autoLaunch")){
if (window.GM_getValue("autoLaunch") === "true"){
GM_openInTab(linko);
}
}
BellSound = new Audio("http://static1.grsites.com/archive/sounds/musical/musical002.wav");
BellSound.play();
var alertLink = document.createElement('a');
alertLink.href = linko
alertLink.innerHTML = combined[f].split('|*|')[0];
temp.push(" " + combined[f].split('|*|')[0]);
recent.innerHTML = "Most recent: " + temp;
logs.innerHTML = fullTime + " ";
logs.appendChild(alertLink);
logArray.push(" " + combined[f].split('|*|')[0] + " " + fullTime);
GM_setValue("dataBase", JSON.stringify(logArray));
var alerted = combined[f].split('|*|')[0];
var buttonz = document.getElementsByClassName('nameButton');
for (b = 0; b < buttonz.length; b++){
if (buttonz[b].value.trim() === alerted.trim()){
buttonz[b].style.background = 'rgb(255,0,0)';
buttonz[b].style.border = "thick solid rgb(255,0,0)"
var buttonName = buttonz[b].value;
for (f = 0; f < combined.length; f++){
if (combined[f].split('|*|')[0].trim() === buttonName.trim()){
dump.push(combined[f]);
combined.splice(f,1);
}
}
setTimeout(function(){
var buttonz = document.getElementsByClassName('nameButton');
for (f = 0; f < buttonz.length; f++){
if (buttonz[f].value === buttonName){
buttonz[f].click();
}
}
},1000 * GM_getValue("sleepDelay"))
}
}
}
}
}
});
setTimeout(function(){parse()},1000 * GM_getValue("scanDelay"));
};
};