metazeta / 101 - Leveling planner

// ==UserScript==
// @name 101 - Leveling planner
// @namespace https://openuserjs.org/scripts/metazeta/101_-_Leveling_planner
// @version 0.3
// @author Emmanuel Ruaud and Benjamin Fuhro
// @description Estimates total XP on user profiles and gives the amount won by current projects
// @match https://profile.intra.42.fr/users/*
// @match https://profile.intra.42.fr/
// @match https://profile.intra.42.fr/users/*/experiences/*/*
// @updateURL https://openuserjs.org/meta/metazeta/101_-_Leveling_planner.meta.js
// @license MIT
// ==/UserScript==

function get_total_xp(lvl, fraction) {
  let curlvl = (1000 * lvl) + 4.79 * (lvl * lvl) + 9.02 * (lvl * lvl * lvl);
  let lvlp1 = (1000 * (lvl + 1)) + 4.79 * ((lvl + 1) * (lvl + 1)) + 9.02 * ((lvl + 1) * (lvl + 1) * (lvl + 1));
  return (curlvl + ((fraction / 100) * (lvlp1 - curlvl)));
}

function get_project() {
  let progress = document.getElementsByClassName("on-progress");
  let lvlstr = progress[0].innerText || progress[0].textContent;
  let fraction = Number(parseFloat(lvlstr.match(/\d+\%/)[0]));
  let curlvl = Number(lvlstr.match(/\d+/)[0]);
  let maxlvl = get_total_xp(curlvl + 1, 0);
  let prevlvl = get_total_xp(curlvl, 0);
  let lvl = get_total_xp(curlvl, fraction);
  let text = document.createTextNode("   (" + (10 * Math.round((lvl - prevlvl) / 10)) + "/" + (10 * Math.round((maxlvl - prevlvl) / 10)) + " XP)");
  progress[0].appendChild(text);
  let projects = document.getElementsByClassName("project-item block-item");
}

function experience() {
  let lvlbox = document.getElementsByTagName("code");
  let reasons = document.getElementsByClassName("reason");
  let lvlstr;
  let split;
  let currentXP;
  let previousXP = 0;
  let i = lvlbox.length - 1;
  while (lvlbox[i]) {
    lvlstr = lvlbox[i].innerText || lvlbox[i].textContent;
    split = lvlstr.split(".");
    currentXP = 10 * Math.round((get_total_xp(Number(split[0]), Number(split[1]))) / 10);
    reasons[i].innerHTML = reasons[i].innerHTML + "<div style='font-size:12px;color:#73BDBC;display:inline-block;left:60%;'> +" + (currentXP - previousXP) + " XP" + "</div>";
    previousXP = currentXP;
    i--;
  }
}

(function () {
  'use strict';
  if ((document.baseURI).match(/^https:\/\/profile\.intra\.42\.fr\/users\/[a-z0-9-_]+$/i) ||
    (document.baseURI).match(/^https:\/\/profile\.intra\.42\.fr\/$/i)
  ) {
    get_project();
  }
  else {
    experience();
  }
})();