juraj222 / pokus

// ==UserScript==
// @name pokus
// @description script for qbsw
// @copyright Year, Author (Author Website)
// @license MIT
// @version 0.2
// @updateURL https://openuserjs.org/meta/juraj222/pokus.meta.js
// @author juraj222
// @include *
// @grant    GM_setValue
// @grant    GM_getValue
// @match        https://jira.qbsw.sk/*
// @match        https://q-timer.com/*
// ==/UserScript==
if (window.location.pathname.includes('/browse')) {
  var myDiv = document.createElement('DIV');
  var myTable = document.createElement('TABLE');
  myTable.id = "myTable";
  myTable.setAttribute("border", 0);
  var row = myTable.insertRow(0);
  var button = document.createElement("BUTTON"); // Create a <button> element
  var t = document.createTextNode("Copy new task"); // Create a text node
  button.appendChild(t); // Append the text to <button>
  button.style = "width:100%";
  //button.style = "top:0;right:50%;position:absolute;z-index: 9999"
  //document.body.appendChild(button);
  var cell1 = row.insertCell(0);
  cell1.colSpan = 2
  cell1.appendChild(button);

  row = myTable.insertRow(1);
  var button2 = document.createElement("BUTTON"); // Create a <button> element
  t = document.createTextNode("Log time"); // Create a text node
  button2.appendChild(t); // Append the text to <button>
  button2.style = "width:100%";
  //button2.style = "top:30px;right:55%;position:absolute;z-index: 9999";
  //document.body.appendChild(button2);
  cell1 = row.insertCell(0);
  cell1.appendChild(button2);

  var button3 = document.createElement("BUTTON"); // Create a <button> element
  t = document.createTextNode("Reset time"); // Create a text node
  button3.appendChild(t); // Append the text to <button>
  button3.style = "width:100%";
  //button3.style = "top:30px;right:50%;position:absolute;z-index: 9999";
  //document.body.appendChild(button3);
  cell1 = row.insertCell(1);
  cell1.appendChild(button3);

  myDiv.style = "top:0;left: 50%;transform: translateX(-50%);position:absolute;z-index: 9999;background-color: black;"
  myDiv.appendChild(myTable);
  document.body.appendChild(myDiv);
  button.onclick = function sync() {
    var pageContent = document.getElementById('key-val').innerHTML;
    GM_setValue("page1content", pageContent);
    console.log(pageContent);
  }
  button2.onclick = function setTimeSpent() {
    calcSpentTime();
  }
  button3.onclick = function setTimeSpent() {
    var currentdate = new Date();
    GM_setValue("time", currentdate.getHours() + ":" + currentdate.getMinutes());
  }
}

function calcSpentTime() {
  if (GM_getValue("time") !== null) {
    console.log(GM_getValue("time"));
    var time = GM_getValue("time").split(":");
    var hr = parseInt(time[0]);
    var mn = parseInt(time[1]);
    var currentdate = new Date();
    if (currentdate.getMinutes() - mn < 0) {
      hr += 1;
      mn = 60 - mn + currentdate.getMinutes();
      console.log(mn);
    }
    else {
      mn = currentdate.getMinutes() - mn;
    }
    console.log("spent time: " + (currentdate.getHours() - hr).toString() + "h " + (mn).toString() + "m");
    if (document.getElementById('log-work-time-logged') !== null) {
      document.getElementById('log-work-time-logged').value = (currentdate.getHours() - hr).toString() + "h " + (mn).toString() + "m";
      var original = document.getElementById('log-work-date-logged-date-picker').value.split(" ");
      var hourOfDay = parseInt(GM_getValue("time").split(":")[0]);
      var minute = parseInt(GM_getValue("time").split(":")[1]);
      var time_new = ((hourOfDay > 12) ? hourOfDay % 12 : hourOfDay) + ":" + (minute < 10 ? ("0" + minute) : minute) + " " + ((hourOfDay >= 12) ? "PM" : "AM");
      document.getElementById('log-work-date-logged-date-picker').value = original[0] + " " + time_new.toString();
    }
  }
  return (currentdate.getHours() - hr).toString() + "h " + (mn).toString() + "m";
}

timeSpent();
setInterval(function () {
   timeSpent() // this will run after every 5 seconds
}, 50000);

function timeSpent() {
  if (window.location.pathname.includes('/browse')) {
    var iDiv;
    if (document.getElementById('mojDiv') === null) {
      var table = document.getElementById('myTable');
      iDiv = document.createElement('div');
      iDiv.id = "mojDiv";
      iDiv.style = "color: white; padding: 0 10px";
     // document.body.appendChild(iDiv);
      var row = myTable.insertRow(2);
      var cell1 = row.insertCell(0);
      cell1.colSpan = 2;
      cell1.appendChild(iDiv);
    }
    else {
      iDiv = document.getElementById('mojDiv');
    }
    iDiv.innerHTML = "current task: <b>" + GM_getValue("page1content").toString() + "</b> spent time: <b>" + calcSpentTime() + "</b>";
  }
}

if (window.location.pathname.includes('/home')) {

  var myDivTimer = document.createElement('DIV');
  var myTableTimer = document.createElement('TABLE');
  myTableTimer.id = "myTable";
  myTableTimer.setAttribute("border", 1);
  var rowTimer = myTableTimer.insertRow(0);

  var btn = document.createElement("BUTTON"); // Create a <button> element
  t = document.createTextNode("Start new task"); // Create a text node
  btn.appendChild(t); // Append the text to <button>
  btn.style = "width:100%; background-color: #00a47e; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px;";
  cell1 = rowTimer.insertCell(0);
  cell1.appendChild(btn);

  var btn2 = document.createElement("BUTTON2"); // Create a <button> element
  t = document.createTextNode("Change task"); // Create a text node
  btn2.appendChild(t); // Append the text to <button>
  btn2.style = "width:100%; background-color: #00a47e; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px;";
  cell1 = rowTimer.insertCell(1);
  cell1.appendChild(btn2);

  myDivTimer.style = "top:0;left: 50%;transform: translateX(-50%);position:absolute;z-index: 9999;"
  myDivTimer.appendChild(myTableTimer);
  document.body.appendChild(myDivTimer);

  //   var phase = localStorage.getItem("page1content");
  //   console.log(phase);
  btn.onclick = function copyIt() {
    var phase = GM_getValue("page1content");
    console.log(phase);
    console.log(document.getElementsByName("timesheetPhase")[0].value);
    document.getElementsByName("timesheetPhase")[0].value = phase;
    console.log(document.getElementsByName("timesheetNote")[0].value);
    document.getElementsByName("timesheetNote")[0].value = "";

    var nextButton = document.getElementsByName("timesheetPhase")[0];
    triggerEvent(nextButton, 'keyup', 13); // simulate mouse/enter key press
    setTimeout(() => document.getElementsByName("newButton")[0].click(), 100);
  }

  btn2.onclick = function changeIt() {
    var phase = GM_getValue("page1content");
    console.log(phase);
    console.log(document.getElementsByName("timesheetPhase")[0].value);
    document.getElementsByName("timesheetPhase")[0].value = phase;
    console.log(document.getElementsByName("timesheetNote")[0].value);
    document.getElementsByName("timesheetNote")[0].value = "";

    var nextButton = document.getElementsByName("timesheetPhase")[0];
    triggerEvent(nextButton, 'keyup', 13); // simulate mouse/enter key press
    setTimeout(() => document.getElementsByName("changeButton")[0].click(), 100);
  }
}

function triggerEvent(el, type, keyCode) {

  // modern browsers, IE9+
  var e = document.createEvent('HTMLEvents');
  e.keyCode = keyCode;
  e.initEvent(type, false, true);
  el.dispatchEvent(e);

}