Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name New script - clockify.me
- // @namespace Violentmonkey Scripts
- // @match https://clockify.me/*
- // @grant none
- // @version 1.0
- // @author -
- // @description 18/04/2021, 05:13:34
- // @run-at document-end
- // ==/UserScript==
- const __CMC_OPT_WORKSPACEID = "5bb60d8ab079874290c79e98";
- const __CMC_OPT_USERID = "5bb60d88b079874290c79e93";
- const __CMC_OPT_TOKEN = "W7YNx7B5h0KQx584";
- function $(sel)
- {
- return document.querySelector(sel);
- }
- function $ajax(method, apikey, url)
- {
- return new Promise(function (resolve)
- {
- const request = new XMLHttpRequest();
- request.open(method, url, true);
- request.setRequestHeader("X-Api-Key", apikey);
- request.onload = function()
- {
- let headerMap = new Map();
- request.getAllResponseHeaders().trim().split(/[\r\n]+/).forEach(function (line) { const parts = line.split(': '); const header = parts.shift(); headerMap.set(header.toLowerCase(), parts.join(': ')); });
- resolve({success: true, status: this.status, statusText: this.statusText, body: JSON.parse(this.response), headers: headerMap });
- }
- request.onerror = function()
- {
- resolve({ success: false, status: null, statusText: null, body: null, headers: null });
- }
- request.send();
- });
- }
- async function refresh_month_ctr(div1, div2)
- {
- let start = moment(moment.now()).startOf("month").toISOString();
- let end = moment(moment.now()).endOf("month").toISOString();
- let allresults = [];
- for(let page=1;;page++)
- {
- 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);
- console.log(r);
- if (r.status !== 200) return;
- if (r.body.length === 0) break;
- allresults = allresults.concat(r.body);
- }
- console.log(allresults);
- let total = moment.duration(0);
- for (let e of allresults) total = total.add(moment.duration(e.timeInterval.duration));
- console.log(total);
- clockify_month_counter_last_manual_upate = Date.now();
- div1.innerText = "Month total (BFB):";
- div2.innerText = (""+(total.hours() + total.days()*24)).padStart(2, '0') + ":" + (""+total.minutes()).padStart(2, '0') + ":" + (""+total.seconds()).padStart(2, '0');
- }
- function refresh()
- {
- let div1 = $("approval-header .cl-align-items-end .cl-lh-1");
- let div2 = $("approval-header .cl-align-items-end .cl-ml-2");
- clockify_month_counter_last_manual_upate = Date.now();
- div1.innerText = "Month total (BFB):";
- div2.innerText = "--:--:--";
- if (!div1 || !div2)
- {
- console.log("CMC-Script :: delay-refresh");
- setTimeout(refresh, 500);
- return;
- }
- console.log("CMC-Script :: refresh");
- refresh_month_ctr(div1, div2);
- }
- var clockify_month_counter_last_manual_upate = 0;
- var clockify_month_counter_last_observe = 0;
- var clockify_month_counter_full_init = false;
- var clockify_month_counter_observer = null;
- function init()
- {
- if (!document.location.href.includes("clockify.me/tracker"))
- {
- console.log("CMC-Script :: abort-init -- wrong location");
- return;
- }
- let div0 = $(".cl-tracker-entries-wrapper");
- let div1 = $("approval-header .cl-align-items-end .cl-lh-1");
- let div2 = $("approval-header .cl-align-items-end .cl-ml-2");
- if (!div0 || !div1 || !div2)
- {
- console.log("CMC-Script :: delay-init");
- setTimeout(init, 250);
- return;
- }
- if (clockify_month_counter_observer != null) clockify_month_counter_observer.disconnect();
- var observer = new MutationObserver(function(mutations) {
- console.log("CMC-Script :: mutated");
- clockify_month_counter_last_observe = Date.now();
- if (clockify_month_counter_full_init && Date.now() - clockify_month_counter_last_manual_upate > 750)
- {
- console.log("CMC-Script :: re-calc");
- refresh();
- }
- });
- observer.observe(div0, { attributes: false, childList: true, characterData: false, subtree: true });
- clockify_month_counter_last_observe = Date.now();
- clockify_month_counter_observer = observer;
- setTimeout(function(){ real_init(div0, div1, div2); }, 0);
- refresh();
- }
- function real_init(div0, div1, div2)
- {
- if (!document.location.href.includes("clockify.me/tracker"))
- {
- console.log("CMC-Script :: abort-real_init -- wrong location");
- return;
- }
- if (Date.now() - clockify_month_counter_last_observe > 2500)
- {
- console.log("CMC-Script :: real_init");
- clockify_month_counter_full_init = true;
- }
- else
- {
- console.log("CMC-Script :: delay-real_init-more");
- setTimeout(function(){ real_init(div0, div1, div2); }, 250);
- }
- }
- setTimeout(init, 100)
- var clockify_month_counter_href = window.location.href;
- window.setInterval(function ()
- {
- if (window.location.href != clockify_month_counter_href) {
- clockify_month_counter_href = window.location.href;
- console.log("CMC-Script :: hash-changed ("+window.location.href+")");
- init();
- }
- }, 500);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement