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 earnings column for the week in a weekly total field.

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

您需要先安裝使用者腳本管理器擴展,如 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 earnings column for the week in a weekly total field.
// @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.0
// @grant       GM_log
// ==/UserScript==

$(document).ready(function()
{

    function append_extra_days($src)
    {
        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($src)
    {
        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($src)
    {
        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() - previous_sunday.getDay());

        var previous_sunday_date = ('0' + (previous_sunday.getMonth()+1)).slice(-2) + ('0' + previous_sunday.getDate()).slice(-2) + previous_sunday.getFullYear();

        var 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(){total += parseInt($(this).parent().parent().find('span[class="reward"]').text().replace(/[^0-9]/g,''), 10);});

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

    $.get('/mturk/status', function(data)
    {
        var $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($src);
            insert_missing_days($src);
            clean_up();
            append_weekly_total($src);
        }
    });
});