// ==UserScript==
// @name JR Crowdsource keyword/ranking mturk.com scripts
// @version 0.1
// @description A userscript for crowdsource keyword and ranking hits on mturk.com. It should be working for the keywords for any Google location. Also it will work for the ranking hits. It will even work for the choose the best url hits.
// @author John Ramirez (JohnnyRS)
// @include *
// @grant none
// @namespace https://greatest.deepsurf.us/users/6406
// ==/UserScript==
// Description: This script makes it easier to do those silly Keywords on Google search sites. It should work for any Google country.
// --Keywords on Google (version 2)
// These are the ones which ask you to fill in the page and rank of a keyword. Then they ask a question you need to find on the URL
// page they want you to search for. This script adds a button to each task which will open up a new window with the Google search page.
// On the search page it should show 50 URLs down the page. There will be a pick button on the right of the site names.
// Pressing q will show the URL in an alert box.
// Pressing n will fill in None automatically if you can't find the URL on the page.
// Pressing p will close the search window and return you to the hit page without filling in anything.
// If the color of the button is red that it is an exact match and it's your lucky day. Click on it to fill in the page and rank numbers
// automatically. It will open the new page in another window. There will be an alert popup which will remind you of the question
// and now it's your turn to find the answer.
// Pressing q on that page will show the question and instructions again in an alert box.
// Pressing w will take the URL of the page as the answer and fill it in the hit automatically.
// Pressing e will show you a popup window where you can enter your own answer. Good when it's only a number not easily copied.
// Pressing p will close that window and return you to the Google search page so you can pick another site or go back to the hit.
// Selecting any words on the page with your mouse will show a button at your mouse pointer when you release it. When you press the
// submit button it will automatically fill in the answer and close the page with the search page. Ready to go to the next task.
// --Keywords on Google (version 1)
// Another hit which it will work is the version 1 of these where it asks you to fill in the best sentence for the keyword.
// It does everything like version 2 but it won't have a question so just select a sentence and submit.
// --Keywords on Google (URL version)
// These are the ones where it asks for the best URL for the keywords. These are very fast. Nothing to match so press the pick button
// next to the best site and it will fill in the URL automatically and close the search page.
//
// This should work on firefox and chrome. Had to fight chrome to make it work but finally tamed it.
// Because this script has to run for every page I recommend you to disable it after you use it. Makes things go faster.
// It shouldn't interfere with any page in normal situations. But some rare situations can occur so disable it.
// Best to disable scripts for individual requesters or hits anyway. Makes browsers go much faster and won't give problems like
// not letting you type in letters or submit automatically by mistake.
// Constant variables used to verify it's working for only the correct hit.
// Also to check if it's in preview mode or not.
var VERIFY1 = "return information about a keyword search in";
var VERIFY2 = "copy and paste the url of the most relevant result";
var OFF1 = "You are currently in preview mode";
function getHostName(url) {
var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i);
if (match !== null && match.length > 2 &&
typeof match[2] === 'string' && match[2].length > 0) {
return match[2];
}
else {
return "";
}
}
var gLocation = document.URL.toString();
var gHostname = getHostName(gLocation);
var gMyWindow=null, gSearchURLs=[], gTaskNumber="", gQuestions=[], GTag = -1, gMouseTimer=null, gSearchVersion = 0;
function urlMatch(url1, url2) {
if(typeof(url1)==='undefined' || typeof(url2)==='undefined') return 0;
var hostName1 = getHostName(url1);
var hostName2 = getHostName(url2);
if ( url1 == url2 ) { return 1; }
else if ( hostName1 == hostName2 ) { return 2; }
else { return 0; }
}
function dataFill(action, data1, data2, data3, data4) {
if(typeof(data1)==='undefined') data1 = "";
if(typeof(data2)==='undefined') data2 = "";
if(typeof(data3)==='undefined') data3 = "";
if(typeof(data4)==='undefined') data4 = "";
var tempe = {"verify": "feetsmell", "tag": GTag, "action": action, "data1": data1, "data2": data2, "data3": data3, "data4": data4};
return tempe;
}
function verifyMessage(e) {
if (e.data.verify === "feetsmell") {
if ( (GTag!=-1 && e.data.tag!=-1 && e.data.tag == GTag) || GTag == -1 || e.data.tag == -1) {
return true;
}
}
return false;
}
function doFrameMessage(e) {
if (e.data.action == "initialize google") {
var theData = dataFill( "search URL", GTag );
if (gSearchVersion==2) {
theData = dataFill( "search URL", gSearchVersion, gSearchURLs[e.data.data1-1], gQuestions[e.data.data1-1], GTag );
} else if (gSearchVersion==1) {
theData = dataFill( "search URL", gSearchVersion, GTag );
}
gMyWindow.postMessage( theData,"*");
}
else if (e.data.action == "found page and rank") {
document.getElementById("PageNumber-"+e.data.data1).value = e.data.data2;
document.getElementById("Ranking-"+e.data.data1).value = e.data.data3;
if (e.data.data4 !== "") document.getElementById("WorkerResponse-"+e.data.data1).value = e.data.data4;
} else if (e.data.action == "found url") { // This is for version 1 of the keyword search
document.getElementById("URL-"+e.data.data1).value = e.data.data2;
} else if (e.data.action == "found answer") {
document.getElementById("WorkerResponse-"+e.data.data1).value = e.data.data2;
}
}
function doGoogleMessage(e) {
if (e.data.action == "initialize question") {
var theData = dataFill( "answer question", gQuestions[0], GTag );
gMyWindow.postMessage( theData,"*");
} else if (e.data.action == "search URL" && GTag == -1) {
gSearchVersion = e.data.data1;
if (gSearchVersion==2) {
gSearchURLs[0] = e.data.data2;
gQuestions[0] = e.data.data3;
GTag = e.data.data4;
} else if (gSearchVersion==1) {
GTag = e.data.data2;
}
var results = document.getElementsByClassName("g");
var i=0; var pageNum=1; var rankNum=1;
while ( (result = results[i++]) ) {
if ( result.getElementsByClassName("r").length>0 && result.getElementsByClassName("_SWb").length>0 ) {
var url = result.getElementsByTagName("a")[0];
var btn = document.createElement("BUTTON");
btn.innerHTML = "Pick";
btn.type = "button";
btn.style.marginRight = "6px";
btn.name = "B," + pageNum + "," + rankNum + "," + url.href;
matching = urlMatch( url.href, gSearchURLs[0] );
if ( matching == 1 ) {
btn.style.background='#FE2E2E';
btn.style.color='#FFFFFF';
} else if ( matching == 2 ) {
btn.style.background="#F5D0A9";
btn.style.color="#000000";
} else if ( matching == 3 ) {
btn.style.background="#F8E0F7";
btn.style.color="#000000";
}
btn.style.fontSize="small";
btn.disabled = false;
btn.onclick = function() {
var splitName = this.name.split(',');
if (gSearchVersion == "2") {
var theData = dataFill( "found page and rank", gTaskNumber, splitName[1], splitName[2] );
window.opener.postMessage(theData,"*");
gMyWindow = window.open(splitName[3]);
theData = dataFill( "start", gTaskNumber, gQuestions[0] );
gMyWindow.postMessage(theData,"*");
} else if (gSearchVersion == "1") {
var theData = dataFill( "found url", gTaskNumber, splitName[3] );
window.opener.postMessage(theData,"*");
self.close();
}
};
url.parentNode.insertBefore(btn,url);
rankNum++;
if ( rankNum>10 ) {
rankNum=1; pageNum++;
}
}
}
document.addEventListener("keydown",function(e) {
if(e.keyCode == 81) { // q = Show URL to search for
var myInstructions = "Press q to show URL. Press p to go back to crowdsource task.\n";
myInstructions += "Press n to fill in None automatically if url can't be found in these 50 urls.\n";
alert( myInstructions + "\n\nURL to search for:\n" + gSearchURLs[0]);
e.stopPropagation();
} else if (e.keyCode == 78) { // n = fill in None if url not found and close window
var theData = dataFill( "found page and rank", gTaskNumber, "None", "None", "None" );
window.opener.postMessage(theData,"*");
self.close();
} else if (e.keyCode == 80) { // p = close this window and go back to crowdsource task
self.close();
}
},false);
} else if (e.data.action == "submit answer") {
console.log("...G Submit answer message received: " + e.data.data1 + " - " + e.data.data2 );
var theData = dataFill( "found answer", gTaskNumber, e.data.data1 );
window.opener.postMessage( theData,"*");
self.close();
}
}
function doUrlMessage(e) {
if (e.data.action == "answer question" && GTag == -1) {
gQuestions[0] = e.data.data1;
GTag = e.data.data2;
var myInstructions = "Press q to show question again. Press w to use the URL for the answer.\n";
myInstructions += "Press e to fill in answer. Press p to go back to google\n";
alert(myInstructions + "\n\nQuestion to answer:\n" + gQuestions[0]);
document.body.addEventListener("mouseup",function(e) {
var theSelection = window.getSelection().toString();
var btn = document.createElement("BUTTON");
btn.innerHTML = "Submit";
btn.type = "button";
btn.style.fontSize="small";
btn.style.width="80px";
btn.style.height="30px";
btn.style.backgroundColor="#A0A0A0";
btn.style.color="initial";
btn.style.position = "absolute";
btn.style.zIndex = "999999";
btn.style.display = "none";
btn.onclick = function() {
btn.style.left = "0px";
btn.style.top = "0px";
btn.style.display = "none";
e = dataFill( "submit answer", theSelection );
window.opener.postMessage(e,"*");
self.close();
};
btn.onmouseleave = function() {
btn.style.border = "solid 2px black";
gMouseTimer = setTimeout( function() {
btn.style.left = "0px";
btn.style.top = "0px";
btn.style.display = "none";
}, 1300);
};
btn.onmouseenter = function() {
btn.style.border = "ridge 2px white";
clearTimeout( gMouseTimer );
};
document.body.appendChild(btn);
if ( theSelection !== "" ) {
posX = e.pageX;
posY = e.pageY;
btn.style.left = posX + "px";
btn.style.top = posY + "px";
btn.style.display = "block";
}
},false);
document.addEventListener("keydown",function(e) {
if(e.keyCode == 81) { // q = Show question
alert( myInstructions + "\n\nQuestion to answer:\n" + gQuestions[0]);
e.stopPropagation();
} else if (e.keyCode == 87) { // w = Use URL for answer
e = dataFill( "submit answer", document.URL.toString() );
window.opener.postMessage(e,"*");
self.close();
} else if (e.keyCode == 69) { // e = Type in answer
var yourAnswer = prompt( "\n\nQuestion to answer:\n" + gQuestions[0] + "\nType in your answer:\n" );
if (yourAnswer !== null && yourAnswer !== "") {
e = dataFill( "submit answer", yourAnswer );
window.opener.postMessage(e,"*");
self.close();
}
e.stopPropagation();
} else if (e.keyCode == 80) { // p = close this window and go back to google search
self.close();
}
},false);
}
}
function taskSearchButtons() {
var tasks = document.getElementsByClassName("task cf");
var i=0;
while ( (task = tasks[i++]) ) {
var theTextReps = task.getElementsByClassName("text-rep");
var theKeyword = theTextReps[0].innerHTML;
theKeyword = theKeyword.replace(/(<([^>]+)>)/ig, "");
theKeyword = theKeyword.replace(/^keyword:\s/i,"");
theKeyword = theKeyword.replace(/&+/,"%26");
theKeyword = theKeyword.replace(/\s+$/, ''); // remove spaces at end of string
if (gSearchVersion==2) { // Version 2 needs the URL and question.
var theURL = theTextReps[1].innerHTML.replace("<strong>Url:</strong> ","");
theURL = theURL.replace(/\s+$/, '');
gSearchURLs[i-1] = theURL;
var theQuestion = theTextReps[2].innerHTML.replace("Instructions: ","");
theQuestion = theQuestion.replace(/\s+$/, '');
gQuestions[i-1] = theQuestion;
}
var btn = document.createElement("BUTTON");
btn.innerHTML = "Search";
btn.type = "button";
btn.name = "B," + i + "," + theKeyword ;
btn.style.fontSize="small";
btn.style.marginLeft = "5px";
btn.disabled = false;
btn.onclick = function() {
var splitName = this.name.split(',');
gMyWindow = window.open("http://www." + googleDomain + "/search?num=50&q=" + splitName[2].replace(/\s+/g,"+") + "&jrio=feetwalk&ttt=" + splitName[1]);
};
theTextReps[0].appendChild(btn);
}
}
function mainListener(e) { // Just a repeater to the frame (Google Chrome fix because messaging to the frame wasn't working.)
if ( verifyMessage(e) ) {
if (e.origin.indexOf("google") != -1) {
var doc=document.getElementsByTagName("iframe")[0].contentWindow;
doc.postMessage(e.data,"*");
}
}
}
function frameListener(e) {
if ( verifyMessage(e) ) {
if (e.origin.indexOf("mturk") != -1 || e.origin.indexOf("google") != -1) doFrameMessage(e);
}
}
// Check if page is on crowdsource or google. If not then check if page was opened by script. If not then exit.
if ( gHostname === "work.crowdsource.com" || gLocation.indexOf("jrio=feetwalk") != -1 || window.opener || gHostname === "mturk.com" ) {
console.log (".Script starts...");
// The magic begins here by first making sure I am on a crowdsource page
// and then checking if it's a crowdsource hit that it will work with.
// After I set up the buttons to make the search for keywords easier.
if (gHostname=="work.crowdsource.com"){ // Make sure this is on the crowdsource site.
GTag = new Date().getTime(); // A unique global tag used to pass messages only for this session.
var instructions = document.getElementById("instructions").innerHTML;
if (instructions.toLowerCase().indexOf(VERIFY1) != -1) { // make sure it only works on Keywords on google version 2
gSearchVersion = 2;
window.addEventListener("message", function(e){ frameListener(e); }, false);
var title = document.getElementsByClassName("projectTitle")[0].innerHTML;
var title = title.replace("Search: Keywords on ","");
var splitTitle = title.split(' ');
var googleDomain = splitTitle[0];
taskSearchButtons();
} else if (instructions.toLowerCase().indexOf(VERIFY2) != -1) { // make sure it only works on Keywords on google version 1
gSearchVersion = 1;
window.addEventListener("message", function(e){ frameListener(e); }, false);
var title = document.getElementsByClassName("projectTitle")[0].innerHTML;
var title = title.replace("Search: Keywords on ","");
var splitTitle = title.split(' ');
var googleDomain = splitTitle[0];
taskSearchButtons();
}
} else if (gLocation.indexOf("jrio=feetwalk") != -1) {
gTaskNumber = (gLocation.substr(gLocation.indexOf("jrio=feetwalk"))).split("&")[1];
gTaskNumber = gTaskNumber.replace("ttt=","");
window.addEventListener("message", function(e){ if ( verifyMessage(e) ) doGoogleMessage(e); }, false);
var theData = dataFill( "initialize google", gTaskNumber );
parent.window.opener.postMessage(theData,"*");
} else if (gHostname=="mturk.com") {
window.addEventListener("message", function(e){ mainListener(e); }, false);
} else {
var theData = dataFill( "initialize question" );
window.opener.postMessage(theData,"*");
window.addEventListener("message", function(e){ if ( verifyMessage(e) ) doUrlMessage(e); }, false);
}
}