// ==UserScript== // @name mmmturkeybacon Seven Days Dashboard and Weekly Total // @author mmmturkeybacon // @description Puts the last seven days, whether they were worked on or not, on the dashboard. Puts the sum of the columns for the week in weekly total fields. Let's you choose which day is the start of the week for calculating the weekly total. // @namespace http://userscripts.org/users/523367 // @match https://www.mturk.com/mturk/dashboard // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js // @version 1.11 // @grant GM_getValue // @grant GM_setValue // @downloadURL none // ==/UserScript== /* WEEK_STARTS_ON let's you choose which day to start the weekly total for: * Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6 */ //var WEEK_STARTS_ON = 0; var WEEK_STARTS_ON = GM_getValue('mmmturkeybacon_seven_days_dashboard_week_start', 0); $(document).ready(function() { var $src; function append_extra_days() { var $last_status_line_link = $('a[href^="/mturk/statusdetail"]').eq(4); var last_status_line_date = $last_status_line_link.attr('href').substring(32); var six_days_ago = new Date(); six_days_ago.setMinutes(six_days_ago.getMinutes() - (480 - six_days_ago.getTimezoneOffset())); // set to mturk timezone six_days_ago.setDate(six_days_ago.getDate() - 6); //http://stackoverflow.com/questions/3605214/javascript-add-leading-zeroes-to-date var six_days_ago_date = ('0' + (six_days_ago.getMonth()+1)).slice(-2) + ('0' + six_days_ago.getDate()).slice(-2) + six_days_ago.getFullYear(); var $extra_days = $src.find('a[href^="/mturk/statusdetail"]').filter(function(){return ($(this).attr('href').substring(32) >= six_days_ago_date && $(this).attr('href').substring(32) < last_status_line_date);}); for (var i = $extra_days.length-1; i >= 0; i--) { $extra_days.eq(i).parent().attr('class', 'metrics-table-first-value'); $last_status_line_link.parent().parent().after($extra_days.eq(i).parent().parent()[0].outerHTML); } } function insert_missing_days() { var date = new Date(); date.setMinutes(date.getMinutes() - (480 - date.getTimezoneOffset())); // set to mturk timezone var date_today = date.getDate(); var months_short = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var $insertion_ref = $('th[id="user_activities.date_column_header.tooltip"][class="metrics-table-first-header"]').parent(); for (var i = 0; i < 7; i++) { var date_zero_formatted = ('0' + (date.getMonth()+1)).slice(-2) + ('0' + date.getDate()).slice(-2) + date.getFullYear(); var date_short_formatted = months_short[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear(); if (i == 0) { date_short_formatted = 'Today'; } var $status_line_link = $('a[href$="'+date_zero_formatted+'"]'); if ($status_line_link.length <= 0) { $insertion_ref.after(''+date_short_formatted+'0000$0.00') $insertion_ref = $('a[href$="'+date_zero_formatted+'"]').parent().parent(); } else { $insertion_ref = $status_line_link.parent().parent(); } date.setDate(date.getDate() - 1); } } function clean_up() { var $status_line_links = $('a[href^="/mturk/statusdetail"]'); for (var i = $status_line_links.length-1; i >= 7; i--) { // remove all status lines that didn't fall in the last 7 days $status_line_links.eq(i).parent().parent().remove(); } $('th[id="user_activities.date_column_header.tooltip"][class="metrics-table-first-header"]').parent().nextAll('tr').each(function(index) { $(this).attr('class', ((index % 2 == 0) ? 'even' : 'odd')); }); } function append_weekly_total() { $('tr[id="mtb_total_line"]').remove(); var previous_sunday = new Date(); previous_sunday.setMinutes(previous_sunday.getMinutes() - (480 - previous_sunday.getTimezoneOffset())); // set to mturk timezone previous_sunday.setDate(previous_sunday.getDate() - ((7 - WEEK_STARTS_ON + previous_sunday.getDay()) % 7)); var previous_sunday_date = ('0' + (previous_sunday.getMonth()+1)).slice(-2) + ('0' + previous_sunday.getDate()).slice(-2) + previous_sunday.getFullYear(); var submitted_total = 0; var approved_total = 0; var rejected_total = 0; var pending_total = 0; var earnings_total = 0; var $this_week = $src.find('a[href^="/mturk/statusdetail"]').filter(function(){return ($(this).attr('href').substring(32) >= previous_sunday_date);}); $this_week.each(function() { submitted_total += parseInt($(this).parent().parent().find('td:eq(1)').text(), 10); approved_total += parseInt($(this).parent().parent().find('td:eq(2)').text(), 10); rejected_total += parseInt($(this).parent().parent().find('td:eq(3)').text(), 10); pending_total += parseInt($(this).parent().parent().find('td:eq(4)').text(), 10); earnings_total += parseInt($(this).parent().parent().find('span[class="reward"]').text().replace(/[^0-9]/g,''), 10); }); // // //$('a[href^="/mturk/statusdetail"]').last().parent().parent().after('Weekly Total'+'$' + (total/100).toFixed(2)+''); $('a[href^="/mturk/statusdetail"]').last().parent().parent().after('Total For The Week Starting On: '+submitted_total+''+approved_total+''+rejected_total+''+pending_total+''+'$' + (earnings_total/100).toFixed(2)+''); $('select[id="mtb_day_select"]').prop('selectedIndex', WEEK_STARTS_ON); $('select[id="mtb_day_select"]')[0].onchange = function(){WEEK_STARTS_ON = this.selectedIndex; GM_setValue('mmmturkeybacon_seven_days_dashboard_week_start', this.selectedIndex); append_weekly_total();}; } $.get('/mturk/status', function(data) { $src = $(data); var maxpagerate = $src.find("td[class='error_title']:contains('You have exceeded the maximum allowed page request rate for this website.')"); if (maxpagerate.length == 0) { append_extra_days(); insert_missing_days(); clean_up(); append_weekly_total(); } }); });