// ==UserScript==
// @name         Pivotal Tracker Report Enhanced
// @namespace    https://www.pivotaltracker.com/
// @version      0.1
// @description  Pivotal Tracker enhanced for Omnimed
// @author       Omnimed
// @match        https://www.pivotaltracker.com/reports/v2/projects/*/epics/*
// @grant        none
// ==/UserScript==

var $ = jQuery;

$( document ).ready(function() {
    init();
    $( document ).bind("ajaxSuccess",function(event, xhr, settings) {
        init();
    });
});

function init() {
    if ($("span[data-aid='stories-count']").length === 5) {
        displayStats();
    } else {
        setTimeout(function() {
            init();
        }, 100);
    }
}

function displayStats() {
    var accepted = $("span[data-aid='stories-count']")[0].textContent.match(/\d+/g);
    var delivered = $("span[data-aid='stories-count']")[1].textContent.match(/\d+/g);
    var finished = $("span[data-aid='stories-count']")[2].textContent.match(/\d+/g);
    var started = $("span[data-aid='stories-count']")[3].textContent.match(/\d+/g);
    var unstarted = $("span[data-aid='stories-count']")[4].textContent.match(/\d+/g);
    var iceboxed = $("span[data-aid='stories-count']")[5].textContent.match(/\d+/g);

    $('#stats').remove();

    $($($($("div[data-reactroot]").children()[0]).children()[0]).children()[2]).prepend("<div id='stats' style='padding: 13px 0px 0px;position: absolute;left: 780px;'><div style='border: 1px solid rgb(174, 192, 203);'><header style='font-size: 16px; font-weight: bold; color: rgb(81, 81, 81); padding: 13px; background-color: rgb(255, 255, 255); min-height: 42px; border-bottom: 1px solid rgb(210, 218, 223); box-sizing: border-box;'><div style='width: 100%; font-size: 14px; position: relative; border-collapse: collapse;'><div style='text-overflow: ellipsis; white-space: nowrap; overflow: hidden; font-size: 16px; font-weight: bold; margin-right: 75px;'>Stats de la version</div></div></header><div style='padding: 13px; background-color: rgb(248, 249, 250);'><div style='opacity: 1;'><table style='font-weight: 600;color: rgb(81, 81, 81);width: 100%;text-align: left;'><tbody>" +
                                                                                        "<tr><th><span>State</span></th><th><span>Stories</span></th><th><span>Points</span></th></tr>" +
                                                                                        "<tr><th><span>Accepted</span></th><th><span>" + accepted[0] + "</span></th><th><span>" + accepted[1] + "</span></th></tr>" +
                                                                                        "<tr><th><span>Delivered</span></th><th><span>" + delivered[0] + "</span></th><th><span>" + delivered[1] + "</span></th></tr>" +
                                                                                        "<tr><th><span>Finished</span></th><th><span>" + finished[0] + "</span></th><th><span>" + finished[1] + "</span></th></tr>" +
                                                                                        "<tr><th><span>Started</span></th><th><span>" + started[0] + "</span></th><th><span>" + started[1] + "</span></th></tr>" +
                                                                                        "<tr><th><span>Unstarted</span></th><th><span>" + unstarted[0] + "</span></th><th><span>" + unstarted[1] + "</span></th></tr>" +
                                                                                        "<tr><th><span>Iceboxed</span></th><th><span>" + iceboxed[0] + "</span></th><th><span>" + iceboxed[1] + "</span></th></tr></tbody></table></div>" +
                                                                                        "</div></div></div>");
}

@@ -555,21 +555,11 @@
 '%5D%22)
-.length === 5
+%5B0%5D
 ) %7B%0A
@@ -713,24 +713,65 @@
 r accepted =
+ $(%22span%5Bdata-aid='stories-count'%5D%22)%5B0%5D ?
  $(%22span%5Bdat
@@ -815,32 +815,40 @@
 nt.match(/%5Cd+/g)
+ : %5B0,0%5D
 ;%0A    var delive
@@ -848,24 +848,65 @@
  delivered =
+ $(%22span%5Bdata-aid='stories-count'%5D%22)%5B1%5D ?
  $(%22span%5Bdat
@@ -950,32 +950,40 @@
 nt.match(/%5Cd+/g)
+ : %5B0,0%5D
 ;%0A    var finish
@@ -982,24 +982,65 @@
 r finished =
+ $(%22span%5Bdata-aid='stories-count'%5D%22)%5B2%5D ?
  $(%22span%5Bdat
@@ -1084,32 +1084,40 @@
 nt.match(/%5Cd+/g)
+ : %5B0,0%5D
 ;%0A    var starte
@@ -1111,32 +1111,73 @@
    var started =
+ $(%22span%5Bdata-aid='stories-count'%5D%22)%5B3%5D ?
  $(%22span%5Bdata-ai
@@ -1217,32 +1217,40 @@
 nt.match(/%5Cd+/g)
+ : %5B0,0%5D
 ;%0A    var unstar
@@ -1246,32 +1246,73 @@
  var unstarted =
+ $(%22span%5Bdata-aid='stories-count'%5D%22)%5B4%5D ?
  $(%22span%5Bdata-ai
@@ -1360,16 +1360,24 @@
 (/%5Cd+/g)
+ : %5B0,0%5D
 ;%0A    va
@@ -1384,24 +1384,65 @@
 r iceboxed =
+ $(%22span%5Bdata-aid='stories-count'%5D%22)%5B5%5D ?
  $(%22span%5Bdat
@@ -1494,16 +1494,24 @@
 (/%5Cd+/g)
+ : %5B0,0%5D
 ;%0A%0A    $

Update PivotalTrackerReportEnhanced.js