Last active
February 12, 2024 09:53
-
-
Save Bonno/f706d9708cd1a594050b27e77757fa04 to your computer and use it in GitHub Desktop.
Calculate cumulative hours on the time overview page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name ActiveCollab: Cumulate daily hours | |
// @namespace Violentmonkey Scripts | |
// @match https://*/my-time | |
// @grant none | |
// @version 1.0 | |
// @author Bonno Nachtegaal | |
// @description Calculate cumulative hours on the time overview page | |
// @homepageURL https://gist.github.com/Bonno/f706d9708cd1a594050b27e77757fa04 | |
// | |
// @require https://raw.githubusercontent.com/RickStrahl/jquery-watch/master/jquery-watch.min.js | |
// ==/UserScript== | |
(function($) { | |
'use strict'; | |
// Based on gist from Jose Quintana (joseluisq) | |
// https://gist.github.com/joseluisq/dc205abcc9733630639eaf43e267d63f | |
function addTimes (startTime, endTime) { | |
var times = [ 0, 0, 0 ]; | |
var max = times.length; | |
var a = (startTime || '').split(':'); | |
var b = (endTime || '').split(':'); | |
// normalize time values | |
for (var i = 0; i < max; i++) { | |
a[i] = isNaN(parseInt(a[i])) ? 0 : parseInt(a[i]); | |
b[i] = isNaN(parseInt(b[i])) ? 0 : parseInt(b[i]); | |
} | |
// store time values | |
for (var i = 0; i < max; i++) { | |
times[i] = a[i] + b[i]; | |
} | |
var hours = times[0]; | |
var minutes = times[1]; | |
if (minutes >= 60) { | |
var h = (minutes / 60) << 0; | |
hours += h; | |
minutes -= 60 * h; | |
} | |
return hours + ':' + ('0' + minutes).slice(-2); | |
} | |
function init() { | |
$("html").watch({ | |
properties: "attr_class", | |
callback: function (data, i) { | |
if (data.vals[i].includes('initialized')) { | |
$('.tracking_objects_list_group').each(function(){ | |
var dailyTime = 0; | |
var dayBlock = $(this); | |
dayBlock.find('.tracking_object_value span').each(function() { | |
dailyTime = addTimes(dailyTime, $(this).html()); | |
}); | |
var dailyTimeBlock = $('<span>').css('font-weight', '500').css('margin-left', '11%').html('('+dailyTime+')'); | |
dayBlock.find('h2').append(dailyTimeBlock); | |
}); | |
} | |
} | |
}); | |
} | |
document.onreadystatechange = function() { | |
if (document.readyState == "complete") { | |
init(); | |
} | |
}; | |
})(window.$); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment