Logtime Plus

https://github.com/pcluchet/logtime-plus

  1. // ==UserScript==
  2. // @name Logtime Plus
  3. // @namespace profile.intra.42.fr
  4. // @include https://profile.intra.42.fr/
  5. // @version 1.3
  6. // @grant none
  7. // @description https://github.com/pcluchet/logtime-plus
  8. // ==/UserScript==
  9. function minTommss(minutes){
  10. var sign = minutes < 0 ? "-" : "";
  11. var min = Math.floor(Math.abs(minutes));
  12. var sec = Math.floor((Math.abs(minutes) * 60) % 60);
  13. return sign + (min < 10 ? "0" : "") + min + "h" + (sec < 10 ? "0" : "") + sec;
  14. }
  15.  
  16. function load_js()
  17. {
  18. var head= document.getElementsByTagName('head')[0];
  19. var script= document.createElement('script');
  20. script.type= 'text/javascript';
  21. script.src= 'https://profile.intra.42.fr/assets/application-a482bc20dff3485ff22a2d7350c86b65accb523b5f0b6f711d36840efbefc37d.js';
  22. head.appendChild(script);
  23. }
  24. function wait(ms){
  25. var start = new Date().getTime();
  26. var end = start;
  27. while(end < start + ms) {
  28. end = new Date().getTime();
  29. }
  30. }
  31. function get_n(add, n){
  32. //console.log("Birdel0");
  33. var tt = n + add;
  34. console.log('n : ' + n + " add : " + add);
  35. if (tt == 0)
  36. {
  37. return 12;
  38. }
  39. else if ( tt < 0)
  40. {
  41. return 11;
  42. }
  43. else
  44. {
  45. return n+add;
  46. }
  47. }
  48.  
  49. function disp_hint_top(event){
  50.  
  51. var box = document.createElementNS("http://www.w3.org/2000/svg", 'g');
  52. var svg = document.getElementById("user-locations");
  53. var s = new XMLSerializer();
  54. // get the inner DOM of alpha.svg
  55. console.log('target = '+ s.serializeToString(event.target));
  56. var sum = event.target.parentNode.getAttribute('hoursum');
  57. var x = parseFloat(event.target.parentNode.getAttribute('spoof_x'));
  58. var y = parseFloat(event.target.parentNode.getAttribute('spoof_y'));
  59. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'rect')
  60. to_add.setAttribute("x", x+17);
  61. to_add.setAttribute("y", y-5);
  62. to_add.setAttribute("fill", '#fff');
  63. to_add.setAttribute("width", '53');
  64. to_add.setAttribute("height", '22');
  65. to_add.setAttribute("filter", 'url(#black-glow)');
  66. to_add.setAttribute("font-family", 'sans-serif');
  67. box.setAttribute("id", sum + x + y);
  68. box.appendChild(to_add);
  69. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'text')
  70. to_add.setAttribute("x", x+28);
  71. to_add.setAttribute("y", y+11);
  72. to_add.setAttribute("fill", '#444');
  73. to_add.setAttribute("width", '42');
  74. to_add.setAttribute("height", '22');
  75. to_add.setAttribute("font-family", 'sans-serif');
  76. to_add.setAttribute("font-size", '13');
  77. to_add.innerHTML = sum;
  78. box.setAttribute("id", sum + x + y);
  79. box.appendChild(to_add);
  80. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'line')
  81. to_add.setAttribute("x1", x+16);
  82. to_add.setAttribute("y1", y-5);
  83. to_add.setAttribute("x2", x+16);
  84. to_add.setAttribute("y2", y+18);
  85. to_add.setAttribute("stroke", '#666');
  86. to_add.setAttribute("stroke-width", '1');
  87. to_add.innerHTML = sum;
  88. box.setAttribute("id", sum + x + y);
  89. box.appendChild(to_add);
  90. console.log("created id = ", sum + x + y);
  91. //to_add.innerHTML = minTommss(tot);
  92. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'polygon')
  93. to_add.setAttribute("x", x+9);
  94. to_add.setAttribute("y", y+8);
  95. to_add.setAttribute("fill", '#666');
  96. var p1x = x+10;
  97. var p1y = y+8;
  98. var p2x = x+16;
  99. var p2y = y+2;
  100. var p3x = x+16;
  101. var p3y = y+14;
  102. to_add.setAttribute("points", p1x + ' ' + p1y + ', ' + p2x + ' ' + p2y + ', ' + p3x + ' ' + p3y);
  103. console.log('attribut pts :' + to_add.getAttribute('points'));
  104. to_add.setAttribute("height", '17');
  105. box.appendChild(to_add);
  106. svg.appendChild(box);
  107. console.log("sum =" +sum);
  108. //alert('sum = ' + sum);
  109. }
  110.  
  111. function disp_hint(event){
  112. console.log('FIREF');
  113. var box = document.createElementNS("http://www.w3.org/2000/svg", 'g');
  114. var svg = document.getElementById("user-locations");
  115. var s = new XMLSerializer();
  116. // get the inner DOM of alpha.svg
  117. console.log('target = '+ s.serializeToString(event.target));
  118. var sum = event.target.getAttribute('hoursum');
  119. var x = parseFloat(event.target.getAttribute('x'));
  120. var y = parseFloat(event.target.getAttribute('y'));
  121. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'rect')
  122. to_add.setAttribute("x", x+17);
  123. to_add.setAttribute("y", y-5);
  124. to_add.setAttribute("fill", '#fff');
  125. to_add.setAttribute("width", '53');
  126. to_add.setAttribute("height", '22');
  127. to_add.setAttribute("filter", 'url(#black-glow)');
  128. to_add.setAttribute("font-family", 'sans-serif');
  129. box.setAttribute("id", sum + x + y);
  130. box.appendChild(to_add);
  131. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'text')
  132. to_add.setAttribute("x", x+28);
  133. to_add.setAttribute("y", y+11);
  134. to_add.setAttribute("fill", '#444');
  135. to_add.setAttribute("width", '42');
  136. to_add.setAttribute("height", '22');
  137. to_add.setAttribute("font-family", 'sans-serif');
  138. to_add.setAttribute("font-size", '13');
  139. to_add.innerHTML = sum;
  140. box.setAttribute("id", sum + x + y);
  141. box.appendChild(to_add);
  142. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'line')
  143. to_add.setAttribute("x1", x+16);
  144. to_add.setAttribute("y1", y-5);
  145. to_add.setAttribute("x2", x+16);
  146. to_add.setAttribute("y2", y+18);
  147. to_add.setAttribute("stroke", '#666');
  148. to_add.setAttribute("stroke-width", '1');
  149. to_add.innerHTML = sum;
  150. box.setAttribute("id", sum + x + y);
  151. box.appendChild(to_add);
  152. console.log("created id = ", sum + x + y);
  153. //to_add.innerHTML = minTommss(tot);
  154. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'polygon')
  155. to_add.setAttribute("x", x+9);
  156. to_add.setAttribute("y", y+8);
  157. to_add.setAttribute("fill", '#666');
  158. var p1x = x+10;
  159. var p1y = y+8;
  160. var p2x = x+16;
  161. var p2y = y+2;
  162. var p3x = x+16;
  163. var p3y = y+14;
  164. to_add.setAttribute("points", p1x + ' ' + p1y + ', ' + p2x + ' ' + p2y + ', ' + p3x + ' ' + p3y);
  165. console.log('attribut pts :' + to_add.getAttribute('points'));
  166. to_add.setAttribute("height", '17');
  167. box.appendChild(to_add);
  168. svg.appendChild(box);
  169. console.log("sum =" +sum);
  170. //alert('sum = ' + sum);
  171. }
  172.  
  173. function disp_hint_reverse( event ) {
  174. console.log('FIREF');
  175. var box = document.createElementNS("http://www.w3.org/2000/svg", 'g');
  176. var svg = document.getElementById("user-locations");
  177. var s = new XMLSerializer();
  178. // get the inner DOM of alpha.svg
  179. console.log('target = '+ s.serializeToString(event.target));
  180. var sum = event.target.getAttribute('hoursum');
  181. var x = parseFloat(event.target.getAttribute('x'));
  182. var y = parseFloat(event.target.getAttribute('y'));
  183. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'rect')
  184. to_add.setAttribute("x", x-7-53);
  185. to_add.setAttribute("y", y-5);
  186. to_add.setAttribute("fill", '#fff');
  187. to_add.setAttribute("width", '53');
  188. to_add.setAttribute("height", '22');
  189. to_add.setAttribute("filter", 'url(#black-glow)');
  190. to_add.setAttribute("font-family", 'sans-serif');
  191. box.setAttribute("id", sum + x + y);
  192. box.appendChild(to_add);
  193. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'text')
  194. to_add.setAttribute("x", x-8-42);
  195. to_add.setAttribute("y", y+11);
  196. to_add.setAttribute("fill", '#444');
  197. to_add.setAttribute("width", '42');
  198. to_add.setAttribute("height", '22');
  199. to_add.setAttribute("font-family", 'sans-serif');
  200. to_add.setAttribute("font-size", '13');
  201. to_add.innerHTML = sum;
  202. box.setAttribute("id", sum + x + y);
  203. box.appendChild(to_add);
  204. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'line')
  205. to_add.setAttribute("x1", x-6);
  206. to_add.setAttribute("y1", y-5);
  207. to_add.setAttribute("x2", x-6);
  208. to_add.setAttribute("y2", y+18);
  209. to_add.setAttribute("stroke", '#666');
  210. to_add.setAttribute("stroke-width", '1');
  211. to_add.innerHTML = sum;
  212. box.setAttribute("id", sum + x + y);
  213. box.appendChild(to_add);
  214. console.log("created id = ", sum + x + y);
  215. //to_add.innerHTML = minTommss(tot);
  216. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'polygon')
  217. to_add.setAttribute("x", x+9);
  218. to_add.setAttribute("y", y+8);
  219. to_add.setAttribute("fill", '#666');
  220. var p1x = x-1;
  221. var p1y = y+8;
  222. var p2x = x-6;
  223. var p2y = y+2;
  224. var p3x = x-6;
  225. var p3y = y+14;
  226. to_add.setAttribute("points", p1x + ' ' + p1y + ', ' + p2x + ' ' + p2y + ', ' + p3x + ' ' + p3y);
  227. console.log('attribut pts :' + to_add.getAttribute('points'));
  228. to_add.setAttribute("height", '17');
  229. box.appendChild(to_add);
  230. svg.appendChild(box);
  231. console.log("sum =" +sum);
  232. //alert('sum = ' + sum);
  233. }
  234.  
  235. function remove_hint( event ) {
  236. console.log('MOUSEOUT');
  237. var svg = document.getElementById("user-locations");
  238. var s = new XMLSerializer();
  239. //console.log('svg = '+ s.serializeToString(event.target));
  240. var sum = event.target.getAttribute('hoursum');
  241. var x = event.target.getAttribute('x');
  242. var y = event.target.getAttribute('y');
  243. var to_remove = document.getElementById(sum + x + y);
  244.  
  245. to_remove.parentNode.removeChild(to_remove);
  246.  
  247. }
  248.  
  249. function remove_hint_spoof( event ) {
  250. console.log('MOUSEOUT');
  251. var svg = document.getElementById("user-locations");
  252. var s = new XMLSerializer();
  253. //console.log('svg = '+ s.serializeToString(event.target));
  254. var sum = event.target.parentNode.getAttribute('hoursum');
  255. var x = event.target.parentNode.getAttribute('spoof_x');
  256. var y = event.target.parentNode.getAttribute('spoof_y');
  257. var to_remove = document.getElementById(sum + x + y);
  258.  
  259. to_remove.parentNode.removeChild(to_remove);
  260.  
  261. }
  262. window.onload = function(){
  263. var mois = [];
  264. mois[1] = 0.0;
  265. mois[2] = 0.0;
  266. mois[3] = 0.0;
  267. mois[4] = 0.0;
  268. mois[5] = 0.0;
  269. mois[6] = 0.0;
  270. mois[7] = 0.0;
  271. mois[8] = 0.0;
  272. mois[9] = 0.0;
  273. mois[10] = 0.0;
  274. mois[11] = 0.0;
  275. mois[12] = 0.0;
  276. var mois_j = [];
  277. for (var w = 0; w <= 12; w++)
  278. {
  279. var jours = [];
  280. for (var z = 0; z <= 31; z++)
  281. {
  282. jours[z] = 0.0;
  283. }
  284. mois_j[w] = jours;
  285. }
  286.  
  287. var months = [];
  288. months[1] = 'Janvier';
  289. months[2] = 'Febrier';
  290. months[3] = 'Mars';
  291. months[4] = 'April';
  292. months[5] = 'May';
  293. months[6] = 'June';
  294. months[7] = 'July';
  295. months[8] = 'August';
  296. months[9] = 'Septembre';
  297. months[10] = 'Octobre';
  298. months[11] = 'Novembre';
  299. months[12] = 'Decembre';
  300. var jl = document.getElementsByClassName("container-inner-item boxed");
  301. var logtimes = document.getElementById("user-locations").getAttribute("data-location-graph");
  302. logtimes = logtimes.substr(1);
  303. logtimes = logtimes.substr(0, logtimes.length - 1);
  304. var set = logtimes.split(",");
  305. set.forEach(function(element) {
  306. var base = element.split(":");
  307. var annee = base[0].substr(1,4);
  308. var ms = base[0].substr(6,2);
  309. var jour = base[0].substr(9,2);
  310. var heures = base[1].substr(1,2);
  311. var minutes = base[2];
  312. var secondes = base[3].substr(0,2);
  313. mois[parseInt(ms)] += parseInt(heures) + parseInt(minutes)/60;//+(parseInt(minutes)/60)*10);
  314. mois_j[parseInt(ms)][parseInt(jour)] = parseInt(heures) + parseInt(minutes)/60;
  315. });
  316. // console.log('mj = ' + mois_j);
  317. var d = new Date();
  318. console.log("la date c: " + d.toISOString());
  319. var n = d.getMonth();
  320. var total = mois[n-1] + mois[n-0] + mois[n+1];
  321. var t = document.createElement("div");
  322. t.innerHTML = '<h4>Logtime Plus : '+ minTommss(total) + '</h4>';
  323. n = get_n(-1,d.getMonth());
  324. t.innerHTML = t.innerHTML + months[n-1] +' : '+ minTommss(mois[n-1])+'<br/>';
  325. n = get_n(0,d.getMonth());
  326. t.innerHTML = t.innerHTML + months[n-0]+' : '+minTommss(mois[n-0])+'<br/>';
  327. n = d.getMonth();
  328. t.innerHTML = t.innerHTML + months[n+1]+' : '+minTommss(mois[n+1])+'<br/>';
  329. var k = jl[2].getElementsByClassName('profile-title');
  330. k[0].innerHTML = 'Logtime⁺';
  331. var svg = document.getElementById("user-locations");
  332. //this kills the handlers
  333. //svg.innerHTML = svg.innerHTML;
  334. svg.setAttribute('viewBox','0 0 610 200');
  335. var s = new XMLSerializer();
  336. // get the inner DOM of alpha.svg
  337. console.log('svg = '+ s.serializeToString(svg));
  338. var svgBrut = s.serializeToString(svg);
  339. var parser = new DOMParser();
  340. //var svgDoc = parser.parseFromString(svgBrut, "text/xml");
  341. var svgDoc = svg;
  342. var defs = document.createElementNS("http://www.w3.org/2000/svg", 'defs');
  343. defs.innerHTML = "<filter id=\"black-glow\">\
  344. <feColorMatrix type=\"matrix\" values=\
  345. \"0 0 0 0 0\
  346. 0 0 0 0 0\
  347. 0 0 0 0 0\
  348. 0 0 0 0.1 0\"/>\
  349. <feGaussianBlur stdDeviation=\"2.5\" result=\"coloredBlur\"/>\
  350. <feMerge>\
  351. <feMergeNode in=\"coloredBlur\"/>\
  352. <feMergeNode in=\"SourceGraphic\"/>\
  353. </feMerge>\
  354. </filter>";
  355. svgDoc.insertBefore(defs, svgDoc.firstChild);
  356. var elemcol = [];
  357. var texts = svgDoc.getElementsByTagName("*");
  358. var kmatch = 999;
  359. var oldx = 0;
  360. var oldy = 0;
  361. var tot = 0.0;
  362. var match = false;
  363. var last = false;
  364. var max = texts.length;
  365. for(var i = 0; i <= max; i++) {
  366. if ( i == max)
  367. last = true;
  368. /*
  369. if (texts[i].tagName = 'g' && texts[i].firstChild && texts[i].firstChild.tagName == 'rect')
  370. {
  371. var sum = 0.0;
  372. n = get_n(kmatch,d.getMonth());
  373. sum = mois_j[n+kmatch][texts[i].childNodes[1].innerHTML]
  374. console.log('en x =' + texts[i].childNodes[0].getAttribute('x'));
  375. texts[i].setAttribute("hoursum", minTommss(sum));
  376. texts[i].setAttribute("spoof_x", texts[i].childNodes[0].getAttribute('x') );
  377. texts[i].setAttribute("spoof_y", texts[i].childNodes[0].getAttribute('y') );
  378. texts[i].addEventListener("mouseover", disp_hint_top);
  379. texts[i].addEventListener("mouseout",remove_hint_spoof);
  380. console.log('g');
  381. }
  382. */
  383. // console.log('elem: '+ texts[i].innerHTML);
  384. //valeurs a coté du nom du mois
  385. for(var k = -2; k <= 1; k++)
  386. {
  387. n = get_n(k, d.getMonth());
  388. var moistxt = months[n];
  389. //console.log('mois = ' + moistxt);
  390. //console.log('cmp = ' + moistxt.substr(0,3) + "html = "+ texts[i].innerHTML);
  391. if (texts[i].innerHTML == moistxt.substr(0,3))
  392. {
  393. match = true;
  394. kmatch = k;
  395. console.log("moistxt =" + moistxt + "k = " + k);
  396. console.log("MATCH MATCH MATCH");
  397. texts[i].innerHTML = moistxt.substr(0,3) +" - " + minTommss(mois[n]);
  398. texts[i].setAttribute('font-size','12');
  399. if (k == -2)
  400. {
  401. texts[i].setAttribute('x','50');
  402. }
  403. else if (k == -1)
  404. {
  405. texts[i].setAttribute('x','195');
  406. }
  407. else if (k == 0)
  408. {
  409. texts[i].setAttribute('x','339');
  410. }
  411. else if (k == 1)
  412. {
  413. texts[i].setAttribute('x','480');
  414. }
  415. }
  416. }
  417. //decalage pour faire la place du temps semaine
  418. var xxx = parseFloat(texts[i].getAttribute('x'));
  419. var yyy = parseFloat(texts[i].getAttribute('y'));
  420. if (kmatch == -2)
  421. {
  422. xxx += -10;
  423. texts[i].setAttribute('x',xxx);
  424. }
  425. else if (kmatch == -1)
  426. {
  427. texts[i].setAttribute('x',xxx);
  428. }
  429. else if (kmatch == 0)
  430. {
  431. xxx += 10;
  432. texts[i].setAttribute('x',xxx);
  433. }
  434. else if (kmatch == 1)
  435. {
  436. xxx += 20;
  437. texts[i].setAttribute('x',xxx);
  438. }
  439. //fin de semaine ou besoin d'écrire le total de semaine
  440. if ( oldx == 120.5 || oldx == 274.5 || oldx == 582.5 || oldx == 428.5 || match || last)
  441. {
  442. //creation du rectangle coloré pour la semaine
  443. var box = document.createElementNS("http://www.w3.org/2000/svg", 'g');
  444. var to_add = document.createElementNS("http://www.w3.org/2000/svg", 'rect');
  445. var en_x = 0;
  446. if (match)
  447. kmatch -= 1;
  448. console.log('mois = ' + kmatch);
  449. if (kmatch == -2)
  450. {
  451. en_x = 135.5;
  452. }
  453. else if (kmatch == -1)
  454. {
  455. en_x = 289.5;
  456. }
  457. else if (kmatch == 0)
  458. {
  459. en_x = 443.5;
  460. }
  461. else if (kmatch == 1)
  462. {
  463. en_x = 597.5;
  464. }
  465. if (match)
  466. kmatch += 1;
  467. to_add.setAttribute("x", en_x);
  468. to_add.setAttribute("y", oldy-10);
  469. if (tot)
  470. {
  471. var color = '#2CD57B';
  472. var ratio = (tot/60);
  473. }
  474. else
  475. {
  476. var color = '#F5F5F5';
  477. var ratio = 1;
  478. }
  479. to_add.setAttribute("fill", color );
  480. to_add.setAttribute("fill-opacity", ratio);
  481. to_add.setAttribute("width", '7');
  482. to_add.setAttribute("height", '17');
  483. to_add.setAttribute("font-family", 'sans-serif');
  484. to_add.setAttribute("font-size", '8');
  485. to_add.setAttribute("hoursum", minTommss(tot));
  486. var tmp = oldx-5;
  487. var tmp2 = oldy-10;
  488. box.appendChild(to_add);
  489. //ajout des listeners pour afficher le temps lors du survol.
  490. if (!(last || kmatch == 1))
  491. box.addEventListener("mouseover", disp_hint);
  492. else
  493. box.addEventListener("mouseover",disp_hint_reverse);
  494. box.addEventListener("mouseout",remove_hint);
  495. svg.appendChild(box);
  496. tot = 0.0;
  497. }
  498. n = get_n(kmatch,d.getMonth());
  499. if (parseInt(texts[i].innerHTML) && kmatch != 999)
  500. {
  501. // console.log("trying to get [" + n +"+"+kmatch + "][" + texts[i].innerHTML +"]");
  502. // console.log("inside = " + mois_j[n+kmatch][texts[i].innerHTML]);
  503. tot += mois_j[n][texts[i].innerHTML];
  504. }
  505. if (xxx)
  506. oldx = xxx;
  507. else
  508. oldx = -100;
  509. if (xxx)
  510. oldy = yyy;
  511. match = false
  512. }
  513. };