iamabear

[mTurk userscript] Today's Projected Earnings

Oct 17th, 2012
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           Today's Projected Earnings
  3. // @namespace      http://bluesky.software.com/turkscripts
  4. // @description    Adds a projected earnings item to mturk dashboard
  5. // @include        https://www.mturk.com/mturk/dashboard
  6. // ==/UserScript==
  7.  
  8. //
  9. // We are on the dashboard page. We want to go to the status_detail
  10. // pages for today and add up the amount for all the hits done so far today
  11. // and paste that into the dashboard page like the current earnings script
  12. // does. We will use the XMLHttpRequest Object to get the pages and then
  13. // process them one by one until we have done them all.
  14. //
  15. // But first since we are on the dashboard page when we get invoked we search
  16. // for the Today link to get the encoded date and the number of hits submitted
  17. // so we know how many detailed status pages we have to fetch and how to encode
  18. // the links with today's date.
  19. //
  20. // 01/25/2011 Beta Version ready
  21. //
  22. // 01/31/2011
  23. // Modified to save the subtotal calculated so far to avoid having to fetch
  24. // pages that have already been totalled. This is done by saving the subtotal
  25. // for complete pages in a cookie. Two auxillary cookies are needed also, one
  26. // for today's encoded date, and one for complete pages subtotalled so far.
  27. // Also optimized by changing the logic of the first if statement.
  28. //
  29. // 02/01/2011
  30. // Fixed bug that occurred when you had no partial page and you ran the script
  31. // more than once, it would show zero earnings. Removed the CompletedSubtotal
  32. // var in the process of fixing that. Got rid of do it yourself
  33. // getElementsByClassName.
  34. //
  35. // 06/23/2011
  36. // Fixed to handle new html that puts a <span class > around the reward amount
  37. //
  38. // 07/12/2011
  39. // Changed to make it clickable to clear the cookies to start the subtotalling over
  40. // to account for out of sequence completion of HITs and for rejections that happen after
  41. // a page has been subtotalled. Basically this just makes it add everything up from scratch
  42. // to account for changes that have occurred.
  43. //
  44. // 07/12/2011
  45. // Changed .onclick to .addEventListener so that the script works in FF also.
  46.  
  47.  
  48. var TodaysEarnings = 0;
  49. var NumberOfPages = 0;
  50. var todays_values = get_todays_data();
  51.  
  52. if(todays_values.length != 0)
  53. {
  54.    // Extract Today's Date from link
  55.  
  56.    TodaysDate = todays_values[0].innerHTML;
  57.    TodaysDate = TodaysDate.substr(TodaysDate.search(/Date=/)+5,8);
  58.  
  59.    // Check whether the date has rolled over since the last time we were called
  60.    
  61.    if(TodaysDate != getCookie("MturkDate"))
  62.    {
  63.       setCookie("MturkDate",TodaysDate,1);
  64.       setCookie("MturkSubtotal",0,1);
  65.       setCookie("MturkPagesDone",0,1);
  66.    }
  67.  
  68.    // Calculate Number of detailed status pages we have to add up
  69.    // based on the fact there is a 25 hits/page limit.
  70.    // We now only have to add in pages not already totalled and saved
  71.    // in the MturkSubtotal cookie
  72.  
  73.    NumberOfPages = Math.ceil(todays_values[1].innerHTML/25);
  74.    NumberOfCompletePages = Math.floor(todays_values[1].innerHTML/25);
  75.    PagesDone = parseFloat(getCookie("MturkPagesDone"));
  76.    TodaysEarnings = parseFloat(getCookie("MturkSubtotal"));
  77.  
  78.    if(NumberOfCompletePages > PagesDone)
  79.    {
  80.       for(page = PagesDone + 1; page <= NumberOfCompletePages; page++) // process each completed detailed status page one by one
  81.       {
  82.          detailed_status_page_link = "https://www.mturk.com/mturk/statusdetail?sortType=All&pageNumber=" + page + "&encodedDate=" + TodaysDate;            
  83.          TodaysEarnings += process_page(detailed_status_page_link);      
  84.       }
  85.       setCookie("MturkSubtotal",TodaysEarnings,1);      
  86.       setCookie("MturkPagesDone",NumberOfCompletePages,1);
  87.    }
  88.    if(NumberOfPages > NumberOfCompletePages)   // Handle partial pages
  89.    {
  90.       detailed_status_page_link = "https://www.mturk.com/mturk/statusdetail?sortType=All&pageNumber=" + NumberOfPages + "&encodedDate=" + TodaysDate;            
  91.       TodaysEarnings += process_page(detailed_status_page_link);      
  92.    }            
  93. }
  94.  
  95. //
  96. // Insert the Projected Earnings in the dashboard.
  97. // Copied from current_earnings script - Copyright (c) 2008, Mr. Berserk
  98. //
  99. // Modified to suit
  100. //
  101.  
  102. var allTds, thisTd;
  103. allTds = document.getElementsByTagName('td');
  104. for (var i = 0; i < allTds.length; i++)
  105. {
  106.    thisTd = allTds[i];
  107.    if ( thisTd.innerHTML.match(/Total Earnings/) && thisTd.className.match(/metrics\-table\-first\-value/) )
  108.    {
  109.       var row = document.createElement('tr');
  110.       row.className = "even";
  111.  
  112.  
  113.       var projectedEarningsLink = document.createElement('a');
  114.       projectedEarningsLink.href =  "https://www.mturk.com/mturk/dashboard";
  115.       projectedEarningsLink.innerHTML = "Today's Projected Earnings";
  116.       projectedEarningsLink.addEventListener('click',clearCookies,false);
  117.  
  118.       var cellLeft = document.createElement('td');
  119.       cellLeft.className = "metrics-table-first-value";
  120.       cellLeft.appendChild(projectedEarningsLink);
  121.       row.appendChild(cellLeft);
  122.              
  123.       var cellRight = document.createElement('td');      
  124.       cellRight.innerHTML = "$" + TodaysEarnings.toFixed(2);
  125.       row.appendChild(cellRight);
  126.              
  127.       thisTd.parentNode.parentNode.insertBefore(row,thisTd.parentNode.nextSibling);
  128.    }
  129. }
  130.  
  131. //
  132. // Functions
  133. //
  134.  
  135. //
  136. // This will grab the data from the table for today. If today doesn't exist yet
  137. // we return an empty array so things still work.
  138. //
  139.  
  140. function get_todays_data()
  141. {
  142.    var tables = document.getElementsByClassName('metrics-table');
  143.    for (var m = 0; m < tables.length; m++)
  144.    {
  145.       var table_rows = tables[m].getElementsByTagName('tr');
  146.       for (var n = 0; n < table_rows.length; n++)
  147.       {
  148.          var table_data = table_rows[n].getElementsByTagName('td');
  149.          status_link = table_rows[n].getElementsByTagName('a');
  150.          if(status_link[0])
  151.          {
  152.             if(table_data[0].innerHTML.match('Today'))
  153.             {
  154.                return table_data;
  155.             }
  156.          }
  157.       }
  158.    }
  159.    no_today = [];
  160.    return no_today;  // If no Today found we have to return something else it dies silently
  161. }
  162.  
  163.  
  164. //
  165. // Process a detailed status page by added up the value of all the hits
  166. //
  167.  
  168. function process_page(link)
  169. {
  170.    // use XMLHttpRequest to fetch the entire page, use async mode for now because I understand it
  171.    var page = getHTTPObject();
  172.    page.open("GET",link,false);      
  173.    page.send(null);
  174.    return earnings_subtotal(page.responseText);
  175. }
  176.  
  177.  
  178. //
  179. // Add up all the hit values on this detailed status page
  180. // For the code to work we have to turn the responseText back
  181. // into a DOM object so we can get the values using the
  182. // getElementsByClassName function. We use the create div trick.
  183. //
  184. // Modified to not add in those hits that have already been rejected
  185. //
  186.  
  187. function earnings_subtotal(page_text)
  188. {
  189.    var sub_total= 0;
  190.    var index = 0;
  191.    var page_html = document.createElement('div');
  192.    page_html.innerHTML = page_text;
  193.  
  194.    var amounts = page_html.getElementsByClassName('statusdetailAmountColumnValue');
  195.    var statuses = page_html.getElementsByClassName('statusdetailStatusColumnValue');
  196.  
  197.    for(var k = 0; k < amounts.length; k++)
  198.    {
  199.       if(statuses[k].innerHTML != 'Rejected')
  200.       {
  201.          index = amounts[k].innerHTML.indexOf('$');
  202.          sub_total = sub_total + parseFloat(amounts[k].innerHTML.substring(index+1));
  203.       }
  204.    }
  205.    return sub_total;
  206. }
  207.  
  208.  
  209. //
  210. // XMLHttpRequest wrapper from web
  211. //
  212.  
  213. function getHTTPObject()  
  214. {
  215.    if (typeof XMLHttpRequest != 'undefined')
  216.    {
  217.       return new XMLHttpRequest();
  218.    }
  219.    try
  220.    {
  221.       return new ActiveXObject("Msxml2.XMLHTTP");
  222.    }
  223.    catch (e)
  224.    {
  225.       try
  226.       {
  227.          return new ActiveXObject("Microsoft.XMLHTTP");
  228.       }
  229.       catch (e) {}
  230.    }
  231.    return false;
  232. }
  233.  
  234.  
  235. //
  236. //  Cookie functions copied from http://www.w3schools.com/JS/js_cookies.asp
  237. //
  238.  
  239. function setCookie(c_name,value,exdays)
  240. {
  241.    var exdate=new Date();
  242.    exdate.setDate(exdate.getDate() + exdays);
  243.    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  244.    document.cookie=c_name + "=" + c_value;
  245. }
  246.  
  247.  
  248. function getCookie(c_name)
  249. {
  250.    var i,x,y,ARRcookies=document.cookie.split(";");
  251.    for (i=0;i<ARRcookies.length;i++)
  252.    {
  253.       x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  254.       y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  255.       x=x.replace(/^\s+|\s+$/g,"");
  256.       if (x==c_name)
  257.       {
  258.          return unescape(y);
  259.       }
  260.    }
  261. }
  262.  
  263. function clearCookies()
  264. {
  265.    setCookie("MturkSubtotal",0,1);
  266.    setCookie("MturkPagesDone",0,1);
  267.    return true;
  268. }
Add Comment
Please, Sign In to add comment