Skip to content

Instantly share code, notes, and snippets.

@maurizi
Last active March 15, 2019 18:23
Show Gist options
  • Save maurizi/606f4e9e50ea2c707bbdb7ad143c2a81 to your computer and use it in GitHub Desktop.
Save maurizi/606f4e9e50ea2c707bbdb7ad143c2a81 to your computer and use it in GitHub Desktop.
Mike's Timesheet Improvements
// ==UserScript==
// @name Mike's timesheet improvements
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Add plus time buttons to the new GetMyTime
// @author Michael Maurizi
// @match https://app.getmytime.com/timesheet.aspx
// @grant none
// ==/UserScript==
(function() {
'use strict';
function format(num) {
num = '' + num;
return num.padStart(2, '0');
}
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
function addBtn(textbox, hours, minutes, msg) {
var btn = document.createElement('button');
btn.classList.add('add-minute');
btn.setAttribute('style', 'margin-left: 5px; margin-top: 5px;');
btn.appendChild(document.createTextNode(msg));
textbox.after(btn);
btn.addEventListener('click', function() {
var val = textbox.value;
if (val) {
var items = val.split(':');
var curHours = +items[0];
var curMinutes = +items[1];
curHours += hours;
curMinutes += minutes;
if (curMinutes >= 60) {
curHours += 1;
curMinutes -= 60;
}
textbox.value = format(curHours) + ':' + format(curMinutes);
} else {
textbox.value = format(hours) + ':' + format(minutes);
}
});
}
function addButtons(modal) {
var existingBtn = modal.querySelector('.add-minute');
if (!existingBtn) {
var minutes = modal.querySelector('[type="text"].minutes');
addBtn(minutes, 4, 0, '+ 4:00');
addBtn(minutes, 3, 0, '+ 3:00');
addBtn(minutes, 2, 0, '+ 2:00');
addBtn(minutes, 1, 0, '+ 1:00');
addBtn(minutes, 0, 45, '+ 0:45');
addBtn(minutes, 0, 30, '+ 0:30');
addBtn(minutes, 0, 15, '+ 0:15');
}
}
var body = document.querySelector('body');
var observer = new MutationObserver(function(mutationsList) {
var editModal = document.querySelector('.css_changetimeentry');
var createModal = document.querySelector('.css_createtimeentry');
if (editModal) {
addButtons(editModal);
}
if (createModal) {
var helpText = createModal.querySelector('[type="text"].minutes + .input-style');
if (helpText) {
helpText.remove();
}
addButtons(createModal);
}
});
observer.observe(body, {childList: true});
// Highlight zero minute entries
addGlobalStyle('.main [minutes="0"]:not([timeentry_id=""]) { outline: dashed 1px red; }');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment