Bootta11 / Attendo_ba_daily_work_time

// ==UserScript==
// @name         Attendo_ba_daily_work_time
// @namespace    http://tampermonkey.net/
// @version      0.25
// @license MIT
// @description  Add missing work timer for current day to attendo.ba 
// @author       Bootta
// @match        https://attendo.ba/attendo/app/entry
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @updateURL https://openuserjs.org/meta/Bootta11/Attendo_ba_daily_work_time.meta.js
// ==/UserScript==

(function () {
  'use strict';

  let entries_refresh_interval_in_seconds = 300;
  let enable_time_without_pause = false;

  let $ = window.jQuery;
  let worklog_entries;
  let refresh_enteries_interval;
  let today = new Date();
  let dd = today.getDate();
  let mm = today.getMonth() + 1; //January is 0!
  let yyyy = today.getFullYear();
  let today_datetime_string = yyyy + '-' + aln(mm) + '-' + aln(dd);
  let daily_work_time_element = `
<li id="daily_work_timer_holder">
<a href="#" onclick="return false" disabled>
Today worked: <big><span id="daily_work_timer" style="color: black"></span></big>
</a>
</li>`;

  refreshEnteries();
  refresh_enteries_interval = setInterval(refreshEnteries, 1000 * entries_refresh_interval_in_seconds);
  setInterval(refreshDailyWorkTime, 1000);

  function refreshDailyWorkTime() {
    if (worklog_entries) {
      let cumulative_daily_time = 0;
      for (let i = 0; i < worklog_entries.length; i++) {
        let entry = worklog_entries[i];

        if (entry.entryType == "CHECK_IN") {
          let check_in_start_datetime = new Date(entry.dateTime);
          let check_in_end_datetime = new Date();

          if (worklog_entries[i + 1]) {
            check_in_end_datetime = new Date(worklog_entries[i + 1].dateTime)
          }

          cumulative_daily_time += Math.abs(check_in_end_datetime - check_in_start_datetime);
        }
      }

      let cumulative_daily_time_without_pause = cumulative_daily_time - (1000 * 60 * 30);

      var seconds = Math.floor(((cumulative_daily_time % (1000 * 60)) / 1000));
      var minutes = Math.floor(((cumulative_daily_time % (1000 * 60 * 60)) / (1000 * 60)));
      var hours = Math.floor((cumulative_daily_time / (1000 * 60 * 60)));

      if (!$('#daily_work_timer_holder').length) {
        $('.navbar .navbar-right').prepend(daily_work_time_element);
      }

      let display_time = tsToDisplayTime(cumulative_daily_time);
      display_time += (enable_time_without_pause ? '(' + tsToDisplayTime(cumulative_daily_time_without_pause) + ')' : '');
      $('#daily_work_timer').text(display_time);
    }
  }

  function refreshEnteries() {
    $.get('https://attendo.ba/attendo/app/api/employee/overview/daily?date=' + today_datetime_string, function (data) {
      worklog_entries = data.entries;
    });
  }

  //add leading null
  function aln(n) {
    return (n < 10 ? '0' + n : n);
  }

  // timestamp to time
  function tsToDisplayTime(ts) {
    var seconds = Math.floor(((ts % (1000 * 60)) / 1000));
    var minutes = Math.floor(((ts % (1000 * 60 * 60)) / (1000 * 60)));
    var hours = Math.floor((ts / (1000 * 60 * 60)));

    return aln(hours) + ' : ' + aln(minutes) + ' : ' + aln(seconds);
  }
})();