stomp/stomp.js

101 lines
3.7 KiB
JavaScript

// ==UserScript==
// @name stomp
// @namespace https://git.bsiag.com/dmi
// @source https://git.bsiag.com/dmi/stomp
// @version 1.3.1
// @description try to take over the world!
// @author You
// @match https://services.bsi-software.com/bsicrm/*
// @updateURL https://raw.githubusercontent.com/dmtbz/stomp/main/stomp.js
// @downloadURL https://raw.githubusercontent.com/dmtbz/stomp/main/stomp.js
// @grant none
// ==/UserScript==
document.addEventListener('keyup', doc_keyUp, false);
const successColor = "#1c9c31"
const errorColor = "#fa3737"
function doc_keyUp(e) {
if (e.keyCode == 145) {
if (!document.querySelector("div.menubar-box.left > div:nth-child(3) > span.content.text").textContent.match("Neue Arbeit")) {
return;
}
var headers = document.getElementsByClassName("table-header-item-text");
var columnPosBuchungstext;
var columnPosHours;
for (let i = 0; i < headers.length; i++) {
if (headers[i]) {
if (headers[i].textContent.match(/Buchungstext/gm)) {
columnPosBuchungstext = i;
} else if (headers[i].textContent.match(/Dauer \[h]/gm)) {
columnPosHours = i;
}
}
}
var startpos;
var nodes = document.getElementsByClassName("table-row first")[0].parentNode.children;
for (let i = 0; i < nodes.length; i++) {
if (nodes[i].className.match(/table-row (?:selected select-single )?first/g)) {
startpos = i;
break;
}
}
// 0800-1200/1245-1700[]
var totalTimeMinutes;
for (let i = startpos; i < nodes.length - 1; i++) {
let node = nodes[i].childNodes[columnPosBuchungstext];
let text = node.textContent;
if (text && text.match(/^[0-9]{4}-[0-9]{4}(?:\/[0-9]{4}-[0-9]{4})?(?:\[[-]?[0-9]{0,2}\])?/gm)) {
let morning = getMinutes(text.substring(0, 4), text.substring(5, 9));
let afternoon = getMinutes(text.substring(10, 14), text.substring(15, 19));
totalTimeMinutes =+ morning + afternoon + parseInt(text.match(/\[([-]?[0-9]+)\]/)[1]);
let n = i;
while (nodes[n].childNodes[columnPosBuchungstext].className != "table-cell white-space-nowrap halign-left table-aggregate-cell empty") {
n--;
}
let sumNode = nodes[n].childNodes[columnPosHours];
let actualNode = nodes[n].childNodes[columnPosBuchungstext];
let workSum = nodes[n].childNodes[columnPosHours].getElementsByClassName("text")[0].textContent.split(".");
let workSumMinutes = toMinutes(parseInt(workSum[0]), parseInt(workSum[1] / 100 * 60));
if (workSumMinutes === totalTimeMinutes) {
node.style.background = successColor;
sumNode.style.background = successColor;
} else {
node.style.background = errorColor;
sumNode.style.background = errorColor;
actualNode.textContent = "stomp: " + toHoursAndMinutes(totalTimeMinutes);
actualNode.style.background = errorColor;
}
}
}
function getMinutes(first, second) {
var time_start = new Date();
var time_end = new Date();
var value_start = slice(first, 2);
var value_end = slice(second, 2);
time_start.setHours(value_start[0], value_start[1], 0, 0);
time_end.setHours(value_end[0], value_end[1], 0, 0);
return (time_end - time_start) / 1000 / 60 || 0;
}
function slice(str, index) {
return [str.slice(0, index), str.slice(index)];
}
function toHoursAndMinutes(totalMinutes) {
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60 / 60 * 100;
return hours + "." + minutes;
}
function toMinutes(hours, minutes) {
return hours * 60 + minutes;
}
}
}