Google Calendar Time Box Duration

Shows the duration of each event.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           Google Calendar Time Box Duration
// @namespace      https://github.com/Row
// @description    Shows the duration of each event.
// @include        https://www.google.com/calendar*
// @version 0.0.1.20141019134026
// ==/UserScript==

var timer;
document.querySelector('#gridcontainer').addEventListener("DOMSubtreeModified",
    function(e)
    {
        window.clearTimeout(timer);
        timer = window.setTimeout(addDuration,500);
    }, false);

function addDuration()
{
    var timeElements = document.querySelectorAll('dl.cbrd dt');
    for (var i in timeElements) {
        var str = timeElements[i].innerHTML || '';
        str     = str.replace(/^(\d{2}:\d{2}) – (\d{2}:\d{2}) (.*?)$/, callbackTimeISO);
        str     = str.replace(/^(\d+)(\:?\d*?)(p?) – (\d+)(\:?\d*?)(p?) (.*?)$/, callbackTimeGoogleAMPM);
        timeElements[i].innerHTML = str;
        window.clearTimeout(timer);
    }
}

function AMPMto24(hour, isPM)
{
    hour = isPM && hour != '12' ? parseInt(hour) + 12 : hour;
    return pad(!isPM ? parseInt(hour) % 12 : hour, 2);
}

function callbackTimeGoogleAMPM(match, startHour, startMinute, startP, endHour, endMinute, endP, extra)
{
    var start = AMPMto24(startHour, startP == 'p');
    start    += startMinute != '' ? startMinute : ':00';
    var end   = AMPMto24(endHour, endP == 'p');
    end      += endMinute != '' ? endMinute : ':00';
    return startHour + startMinute + startP + ' - ' + endHour + endMinute + endP +
            ' (' + calculateDuration(start, end) + ') ' + extra;
}

function callbackTimeISO(match, start, end, extra)
{
    return start + ' - ' + end + ' (' + calculateDuration(start, end) + ') ' + extra;
}

function getTimeDifference(startDate,endDate)
{
    var totalDifference = endDate.getTime() - startDate.getTime();
    var diff            = new Object();
    diff.days           = Math.floor(totalDifference/1000/60/60/24);
    totalDifference    -= diff.days*1000*60*60*24;
    diff.hours          = Math.floor(totalDifference/1000/60/60);
    totalDifference    -= diff.hours*1000*60*60;
    diff.minutes        = Math.floor(totalDifference/1000/60);
    totalDifference    -= diff.minutes*1000*60;
    diff.seconds        = Math.floor(totalDifference/1000);
    return diff;
}

function pad(number, length)
{
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}

function calculateDuration(startTime,endTime)
{
    var dateString = "2011-11-11T";
    var s          = new Date(dateString+startTime);
    var e          = new Date(dateString+endTime);
    var diff       = getTimeDifference(s,e);
    return  pad(diff.hours,2) + ":" + pad(diff.minutes,2);
}