Bardiensten

Shows the length of the shifts calculated from the start and end time

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bardiensten
// @namespace    http://tampermonkey.net/
// @version      2025-12-18
// @description  Shows the length of the shifts calculated from the start and end time
// @author       You
// @match        https://mijn.knltb.club/*
// @icon         https://www.knltb.club/favicon.ico
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function calculateDuration(startTime, endTime) {
        // Parse time strings (format: HH:MM)
        const [startHour, startMin] = startTime.split(':').map(Number);
        const [endHour, endMin] = endTime.split(':').map(Number);

        // Convert to minutes
        let startMinutes = startHour * 60 + startMin;
        let endMinutes = endHour * 60 + endMin;

        // Handle shifts that cross midnight
        if (endMinutes < startMinutes) {
            endMinutes += 24 * 60;
        }

        // Calculate duration
        const durationMinutes = endMinutes - startMinutes;
        const hours = Math.floor(durationMinutes / 60);
        const minutes = durationMinutes % 60;

        // Format as H:MM and return both formatted string and total minutes
        return {
            formatted: `${hours}:${minutes.toString().padStart(2, '0')}`,
            minutes: durationMinutes
        };
    }

    function addDurations() {
        // Find all time spans
        const timeElements = document.querySelectorAll('.time');

        timeElements.forEach(element => {
            const timeText = element.textContent.trim();
            const match = timeText.match(/(\d{2}:\d{2})\s*-\s*(\d{2}:\d{2})/);

            if (match) {
                const startTime = match[1];
                const endTime = match[2];
                const duration = calculateDuration(startTime, endTime);

                // Check if duration hasn't been added yet
                if (!element.querySelector('.duration')) {
                    const durationSpan = document.createElement('span');
                    durationSpan.className = 'duration';
                    durationSpan.style.fontWeight = 'bold';
                    durationSpan.style.marginLeft = '3px';
                    durationSpan.textContent = `= ${duration.formatted}`;

                    // Make red if 3 hours or less
                    if (duration.minutes <= 180) {
                        durationSpan.style.color = 'red';
                    }

                    element.appendChild(durationSpan);
                }
            }
        });
    }

    // Run when page loads
    addDurations();

    // Also observe for dynamic content changes
    const observer = new MutationObserver(addDurations);
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();