Torrinos / Fitocracy Profile - Big Three

// ==UserScript==
// @name         Fitocracy Profile - Big Three
// @namespace    https://www.fitocracy.com
// @version      0.1
// @description  This script will calculate the top of your Big Three movements using One Repetition Max formula and show it on Fitocracy profile screen
// @author       You
// @match        https://www.fitocracy.com/profile/*
// @grant        none
// ==/UserScript==

var getBench = function(){
    var url='https://www.fitocracy.com/_get_activity_history_json/?activity-id=1';
    return $.get(url);
};

var getSquat = function(){
    var url='https://www.fitocracy.com/_get_activity_history_json/?activity-id=2';
    return $.get(url);
};

var getDeadlift = function(){
    var url='https://www.fitocracy.com/_get_activity_history_json/?activity-id=3';
    return $.get(url);
};

var getBestOne = function(actions){
    max = actions[0].points;
    imax = 0;
    for (i = 1; i < actions.length; i++) {
        if (actions[i].points > max) {
            max = actions[i].points;
            imax = i;
        }
    }
    return {"weight":actions[imax].effort0,"reps":actions[imax].effort1};
};

var get1RM = function(record){
    return record.weight*(1+record.reps/30);
};

$(document).ready(function() {
  //clean up space
  $('.left.center-parent').select('').remove();  

  $.when(
    getBench(),
    getSquat(),
    getDeadlift()
    ).done(function(v1,v2,v3){
      //console.log(v1[0]);
      v1 = v1[0];
      v2 = v2[0];
      v3 = v3[0];
      
      //select last one
      bp = v1[v1.length-1];
      sq = v2[v2.length-1];
      dl = v3[v3.length-1];
      //console.log(bp.actions);
      bp = getBestOne(bp.actions);
      sq = getBestOne(sq.actions);
      dl = getBestOne(dl.actions);
      //Epley formula for 1rm
      bp1rm = get1RM(bp);
      sq1rm = get1RM(sq);
      dl1rm = get1RM(dl);
      val = (bp1rm+sq1rm+dl1rm).toString();
      //Add element
      var newline = '<li id="stat-lifts"><div class="li-container"><div class="stat-value">'+val+'</div><div class="stat-label">Big Three Latest</div></div></li>';
      $('#profile-stats-panel').append(newline);
    }
  );

});