Advertisement
Mikescher

Untitled

Jul 13th, 2021
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. // ==UserScript==
  2. // @name New script - clockify.me
  3. // @namespace Violentmonkey Scripts
  4. // @match https://clockify.me/*
  5. // @grant none
  6. // @version 1.0
  7. // @author -
  8. // @description 18/04/2021, 05:13:34
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. const __CMC_OPT_WORKSPACEID = "5bb60d8ab079874290c79e98";
  13. const __CMC_OPT_USERID = "5bb60d88b079874290c79e93";
  14. const __CMC_OPT_TOKEN = "W7YNx7B5h0KQx584";
  15.  
  16. function $(sel)
  17. {
  18. return document.querySelector(sel);
  19. }
  20.  
  21. function $ajax(method, apikey, url)
  22. {
  23. return new Promise(function (resolve)
  24. {
  25. const request = new XMLHttpRequest();
  26. request.open(method, url, true);
  27. request.setRequestHeader("X-Api-Key", apikey);
  28.  
  29. request.onload = function()
  30. {
  31. let headerMap = new Map();
  32. request.getAllResponseHeaders().trim().split(/[\r\n]+/).forEach(function (line) { const parts = line.split(': '); const header = parts.shift(); headerMap.set(header.toLowerCase(), parts.join(': ')); });
  33. resolve({success: true, status: this.status, statusText: this.statusText, body: JSON.parse(this.response), headers: headerMap });
  34. }
  35. request.onerror = function()
  36. {
  37. resolve({ success: false, status: null, statusText: null, body: null, headers: null });
  38. }
  39.  
  40. request.send();
  41. });
  42. }
  43.  
  44. async function refresh_month_ctr(div1, div2)
  45. {
  46. let start = moment(moment.now()).startOf("month").toISOString();
  47. let end = moment(moment.now()).endOf("month").toISOString();
  48.  
  49. let allresults = [];
  50. for(let page=1;;page++)
  51. {
  52. let r = await $ajax("GET", __CMC_OPT_TOKEN, "https://api.clockify.me/api/v1/workspaces/"+__CMC_OPT_WORKSPACEID+"/user/"+__CMC_OPT_USERID+"/time-entries?start="+start+"&end="+end+"&page="+page);
  53. console.log(r);
  54. if (r.status !== 200) return;
  55. if (r.body.length === 0) break;
  56. allresults = allresults.concat(r.body);
  57. }
  58. console.log(allresults);
  59.  
  60. let total = moment.duration(0);
  61. for (let e of allresults) total = total.add(moment.duration(e.timeInterval.duration));
  62. console.log(total);
  63.  
  64. clockify_month_counter_last_manual_upate = Date.now();
  65. div1.innerText = "Month total (BFB):";
  66. div2.innerText = (""+(total.hours() + total.days()*24)).padStart(2, '0') + ":" + (""+total.minutes()).padStart(2, '0') + ":" + (""+total.seconds()).padStart(2, '0');
  67. }
  68.  
  69. function refresh()
  70. {
  71. let div1 = $("approval-header .cl-align-items-end .cl-lh-1");
  72. let div2 = $("approval-header .cl-align-items-end .cl-ml-2");
  73.  
  74. clockify_month_counter_last_manual_upate = Date.now();
  75. div1.innerText = "Month total (BFB):";
  76. div2.innerText = "--:--:--";
  77.  
  78. if (!div1 || !div2)
  79. {
  80. console.log("CMC-Script :: delay-refresh");
  81. setTimeout(refresh, 500);
  82. return;
  83. }
  84.  
  85. console.log("CMC-Script :: refresh");
  86. refresh_month_ctr(div1, div2);
  87. }
  88.  
  89. var clockify_month_counter_last_manual_upate = 0;
  90. var clockify_month_counter_last_observe = 0;
  91. var clockify_month_counter_full_init = false;
  92. var clockify_month_counter_observer = null;
  93.  
  94. function init()
  95. {
  96. if (!document.location.href.includes("clockify.me/tracker"))
  97. {
  98. console.log("CMC-Script :: abort-init -- wrong location");
  99. return;
  100. }
  101.  
  102. let div0 = $(".cl-tracker-entries-wrapper");
  103. let div1 = $("approval-header .cl-align-items-end .cl-lh-1");
  104. let div2 = $("approval-header .cl-align-items-end .cl-ml-2");
  105.  
  106. if (!div0 || !div1 || !div2)
  107. {
  108. console.log("CMC-Script :: delay-init");
  109. setTimeout(init, 250);
  110. return;
  111. }
  112.  
  113. if (clockify_month_counter_observer != null) clockify_month_counter_observer.disconnect();
  114. var observer = new MutationObserver(function(mutations) {
  115. console.log("CMC-Script :: mutated");
  116. clockify_month_counter_last_observe = Date.now();
  117.  
  118. if (clockify_month_counter_full_init && Date.now() - clockify_month_counter_last_manual_upate > 750)
  119. {
  120. console.log("CMC-Script :: re-calc");
  121. refresh();
  122. }
  123. });
  124. observer.observe(div0, { attributes: false, childList: true, characterData: false, subtree: true });
  125. clockify_month_counter_last_observe = Date.now();
  126. clockify_month_counter_observer = observer;
  127.  
  128. setTimeout(function(){ real_init(div0, div1, div2); }, 0);
  129. refresh();
  130. }
  131.  
  132. function real_init(div0, div1, div2)
  133. {
  134. if (!document.location.href.includes("clockify.me/tracker"))
  135. {
  136. console.log("CMC-Script :: abort-real_init -- wrong location");
  137. return;
  138. }
  139.  
  140. if (Date.now() - clockify_month_counter_last_observe > 2500)
  141. {
  142. console.log("CMC-Script :: real_init");
  143. clockify_month_counter_full_init = true;
  144. }
  145. else
  146. {
  147. console.log("CMC-Script :: delay-real_init-more");
  148. setTimeout(function(){ real_init(div0, div1, div2); }, 250);
  149. }
  150.  
  151.  
  152. }
  153.  
  154. setTimeout(init, 100)
  155.  
  156.  
  157. var clockify_month_counter_href = window.location.href;
  158. window.setInterval(function ()
  159. {
  160. if (window.location.href != clockify_month_counter_href) {
  161. clockify_month_counter_href = window.location.href;
  162. console.log("CMC-Script :: hash-changed ("+window.location.href+")");
  163. init();
  164. }
  165. }, 500);
  166.  
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement