mmmturkeybacon Seven Days Dashboard and Weekly Total

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.

目前為 2015-03-03 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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
// ==/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('<tr class="even"><td class="metrics-table-first-value"><a href="/mturk/statusdetail?encodedDate='+date_zero_formatted+'">'+date_short_formatted+'</a></td><td>0</td><td>0</td><td>0</td><td>0</td><td><span class="reward">$0.00</span></td></tr>')
                $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);
        });
// 
//<option value="1">Monday</option><option value="2">Tuesday</option><option value="3">Wednesday</option><option value="4">Thursday</option><option value="5">Friday</option><option value="6">Saturday</option>

        //$('a[href^="/mturk/statusdetail"]').last().parent().parent().after('<tr class="odd"><td colspan="5">Weekly Total</td><td><span class="reward">'+'$' + (total/100).toFixed(2)+'</span></td></tr>');
        $('a[href^="/mturk/statusdetail"]').last().parent().parent().after('<tr id="mtb_total_line" class="grayHead"><td style="font: bold 12px arial, sans-serif; color: #369; width: 60%; text-align: left; padding-left: 15px;">Total For The Week Starting On: <select id="mtb_day_select"><option value="0">Sunday</option><option value="1">Monday</option><option value="2">Tuesday</option><option value="3">Wednesday</option><option value="4">Thursday</option><option value="5">Friday</option><option value="6">Saturday</option></select></td><td style="font: bold 12px arial, sans-serif; color: #369">'+submitted_total+'</td><td style="font: bold 12px arial, sans-serif; color: #369">'+approved_total+'</td><td style="font: bold 12px arial, sans-serif; color: #369">'+rejected_total+'</td><td style="font: bold 12px arial, sans-serif; color: #369">'+pending_total+'</td><td style="font: bold 12px arial, sans-serif; color: #369"><span class="reward">'+'$' + (earnings_total/100).toFixed(2)+'</span></td></tr>');

        $('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();
        }
    });
});