New HIT Monitor v2

Monitors new HIT page for search terms

Verze ze dne 14. 12. 2015. Zobrazit nejnovější verzi.

// ==UserScript==
// @name        New HIT Monitor v2
// @description Monitors new HIT page for search terms
// @version     2.6
// @namespace   www.redpandanetwork.org
// @include     https://www.mturk.com/HM
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_deleteValue
// @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 = "0px";

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 ScanDelayMenu = document.createElement("select");
ScanDelayMenu.style.height ="30px";
ScanDelayMenu.style.width ="100px";
ScanDelayMenu.style.margin = "5px";
ScanDelayMenu.style.background = "#FFFFFF";
ScanDelayMenu.style.color = 'rgb(50, 79, 23)';
ScanDelayMenu.style.fontWeight = 'bold';
ScanDelayMenu.style.border = "thick solid #324F17";
whites.appendChild(ScanDelayMenu);
var optionDisplay = document.createElement("option");
var savedScanDelay = GM_getValue('scanDelay') ? GM_getValue('scanDelay') : "Scan Delay";
if (savedScanDelay !== "Scan Delay"){
    optionDisplay.text = savedScanDelay + " Seconds";
} else{
    optionDisplay.text = "Sleep Delay";
};
optionDisplay.value = savedScanDelay
ScanDelayMenu.add(optionDisplay);
var option3 = document.createElement("option");
option3.value = 3;
option3.text = "3 Seconds";
ScanDelayMenu.add(option3);
var option5 = document.createElement("option");
option5.value = 5;
option5.text = "5 Seconds";
ScanDelayMenu.add(option5);
var option7 = document.createElement("option");
option7.value = 7;
option7.text = "7 Seconds";
ScanDelayMenu.add(option7);
var option10 = document.createElement("option");
option10.value = 10;
option10.text = "10 Seconds";
ScanDelayMenu.add(option10);
var option15 = document.createElement("option");
option15.value = 15;
option15.text = "15 Seconds";
ScanDelayMenu.add(option15);
var option20 = document.createElement("option");
option20.value = 20;
option20.text = "20 Seconds";
ScanDelayMenu.add(option20);
var option30 = document.createElement("option");
option30.value = 30;
option30.text = "30 Seconds";
ScanDelayMenu.add(option30);
var option45 = document.createElement("option");
option45.value = 45;
option45.text = "45 Seconds";
ScanDelayMenu.add(option45);
var option60 = document.createElement("option");
option60.value = 60;
option60.text = "60 Seconds";
ScanDelayMenu.add(option60);

var SleepDelayMenu = document.createElement("select");
SleepDelayMenu.style.height ="30px";
SleepDelayMenu.style.width ="100px";
SleepDelayMenu.style.margin = "5px";
SleepDelayMenu.style.background = "#FFFFFF";
SleepDelayMenu.style.color = 'rgb(50, 79, 23)';
SleepDelayMenu.style.fontWeight = 'bold';
SleepDelayMenu.style.border = "thick solid #324F17";
whites.appendChild(SleepDelayMenu);
var optionDisplay = document.createElement("option");
var savedSleepDelay = GM_getValue('sleepDelay') ? GM_getValue('sleepDelay') : "Sleep Delay";
if (savedSleepDelay !== "Sleep Delay"){
    optionDisplay.text = savedScanDelay + " Minutes";
} else {
    optionDisplay.text = "Sleep Delay"
};
optionDisplay.value = savedSleepDelay;
SleepDelayMenu.add(optionDisplay);
var choice1 = document.createElement("option");
choice1.value = 1;
choice1.text = "1 Minute";
SleepDelayMenu.add(choice1);
var choice2 = document.createElement("option");
choice2.value = 2;
choice2.text = "2 Minutes";
SleepDelayMenu.add(choice2);
var choice3 = document.createElement("option");
choice3.value = 3;
choice3.text = "3 Minutes";
SleepDelayMenu.add(choice3);
var choice5 = document.createElement("option");
choice5.value = 5;
choice5.text = "5 Minutes";
SleepDelayMenu.add(choice5);
var choice7 = document.createElement("option");
choice7.value = 7;
choice7.text = "7 Minutes";
SleepDelayMenu.add(choice7);
var choice10 = document.createElement("option");
choice10.value = 10;
choice10.text = "10 Minutes";
SleepDelayMenu.add(choice10);
var choice15 = document.createElement("option");
choice15.value = 15;
choice15.text = "15 Minutes";
SleepDelayMenu.add(choice15);
var choice20 = document.createElement("option");
choice20.value = 20;
choice20.text = "20 Minutes";
SleepDelayMenu.add(choice20);
var choice30 = document.createElement("option");
choice30.value = 30;
choice30.text = "30 Minutes";
SleepDelayMenu.add(choice30);
var choice45 = document.createElement("option");
choice45.value = 45;
choice45.text = "45 Minutes";
SleepDelayMenu.add(choice45);
var choice60 = document.createElement("option");
choice60.value = 60;
choice60.text = "60 Minutes";
SleepDelayMenu.add(choice60);

var autoLaunchMenu = document.createElement("select");
autoLaunchMenu.style.height ="30px";
autoLaunchMenu.style.width ="100px";
autoLaunchMenu.style.margin = "5px";
autoLaunchMenu.style.background = "#FFFFFF";
autoLaunchMenu.style.color = 'rgb(50, 79, 23)';
autoLaunchMenu.style.fontWeight = 'bold';
autoLaunchMenu.style.border = "thick solid #324F17";
whites.appendChild(autoLaunchMenu);
var optionDisplay = document.createElement("option");
var savedAutoLaunch = GM_getValue('autoLaunch') ? GM_getValue('autoLaunch') : "Auto Launch";
optionDisplay.value = savedAutoLaunch
optionDisplay.text = savedAutoLaunch
autoLaunchMenu.add(optionDisplay);
var value1 = document.createElement("option");
value1.value = "Auto ON";
value1.text = "Auto ON";
autoLaunchMenu.add(value1);
var value2 = document.createElement("option");
value2.value = "Auto OFF";
value2.text = "Auto OFF";
autoLaunchMenu.add(value2);

var config = document.createElement("input");
config.type = "button";
config.value = "Manual";
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 dataClip = document.createElement('div');
document.getElementsByTagName('table')[0].appendChild(dataClip);
var copy = document.createElement("input");
dataClip.style.textAlign = 'center'
dataClip.style.marginBottom = '35px'

var run = document.createElement("input");
run.type = "button";
run.value = "Run";
run.style.height ="30px";
run.style.width ="100px";
run.style.margin = "5px";
dataClip.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);

copy.type = "button";
copy.value = "Copy DB";
copy.style.height ="30px";
copy.style.width ="100px";
copy.style.margin = "5px";
copy.style.marginTop = "0px";
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);

var importer = document.createElement("input");
importer.type = "button";
importer.value = "Import";
importer.style.height ="30px";
importer.style.width ="100px";
importer.style.margin = "5px";
importer.style.marginTop = "0px";
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);

var exporter = document.createElement("input");
exporter.type = "button";
exporter.value = "Export";
exporter.style.height ="30px";
exporter.style.width ="100px";
exporter.style.margin = "5px";
exporter.style.marginTop = "0px";
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);

var deleter = document.createElement("input");
deleter.type = "button";
deleter.value = "Delete All";
deleter.style.height ="30px";
deleter.style.width ="100px";
deleter.style.margin = "5px";
deleter.style.marginTop = "0px";
dataClip.appendChild(deleter);
deleter.style.background = "#FFFFFF";
deleter.style.color = 'rgb(50, 79, 23)';
deleter.style.fontWeight = 'bold';
deleter.style.border = "thick solid #324F17";
deleter.addEventListener("click", deleteAll, 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 combined = [];
var dump = [];
var logArray = [];
var temp = [];

var sleepDelayPh = GM_getValue('sleepDelay') ? GM_getValue('sleepDelay') : 300;
var scanDelayPh = GM_getValue('scanDelay') ? GM_getValue('scanDelay') : 10;
var autoLaunchPh = GM_getValue('autoLaunch') ? GM_getValue('autoLaunch') : "false";
var databasePh = GM_getValue('dataBase') ? GM_getValue('dataBase') : "";
var combinedPh = GM_getValue('combined') ? GM_getValue('combined') : "potatp";

function copydb(){
    alert('I have this disabled at the moment because it has some issues. I will try to have it fixed soon')
}

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.')
    }
}


function exporterer(){
    GM_setClipboard(GM_getValue('combined'));
    alert('Search list copied to clipboard');
};

function deleteAll(){
    var nuke = confirm('**WARNING** \n\nThis will delete all of your settings, your search list, and your database.\n\nAre you sure you want to do this?');
    if (nuke === true){
        GM_deleteValue("combined");
        GM_deleteValue("scanDelay");
        GM_deleteValue("sleepDelay");
        GM_deleteValue("autoLaunch");
        GM_deleteValue("run");
        GM_deleteValue("dataBase");
        alert('Storage Deleted');
    }
};

   

var dataGet = JSON.parse(databasePh);
for (f=0; f < databasePh.length; f++){
    logArray.push(databasePh[f]);
};

var nameGet = JSON.parse(combinedPh);
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) none repeat scroll 0% 0%' || 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);
};

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 date = (d.getMonth() + 1) + "/" + d.getDate() + "/" +  d.getFullYear();
    var seconds = ('0'  + d.getSeconds()).slice(-2);
    var hours = d.getHours();
    var minutes = d.getMinutes();
    var ampm = hours >= 12 ? ' PM' : ' AM';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0'+minutes : minutes;
    var shortTime = hours + ":" + minutes + ampm;
    var mediumTime = hours + ":" + minutes + ":" + seconds + ampm;
    var fullTime = date + " " + hours + ":" + minutes + ":" + seconds + ampm;
    scanTime.innerHTML = "Last scan: " + mediumTime;
    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";
                    }
                    autoLaunch = document.getElementsByTagName('select')[2].value;
                    GM_setValue('autoLaunch',autoLaunch);
                    if (GM_getValue('autoLaunch') === 'Auto ON'){
                        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.unshift(" " + combined[f].split('|*|')[0] + " " + shortTime);
                    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);
                                }
                            }
                            var sleepDelay = document.getElementsByTagName('select')[1].value;
			    GM_setValue('sleepDelay',sleepDelay);
                            setTimeout(function(){
                                var buttonz = document.getElementsByClassName('nameButton'); 
                                for (f = 0; f < buttonz.length; f++){
                                    if (buttonz[f].value === buttonName){
                                        buttonz[f].click();
                                    }
                                }                            
                            },60000 * GM_getValue('sleepDelay'));
                        }
                    }                
                }
            }             
         }        
        });
        var scanDelay = document.getElementsByTagName('select')[0].value;
        GM_setValue('scanDelay',scanDelay);
        setTimeout(function(){parse()},1000 * GM_getValue('scanDelay'));
    };
};