Joeviocoe / Plugin America

// ==UserScript==
// @name         Plugin America
// @namespace    
// @version      1.0
// @description  Show maintenance on main chart
// @include    http://www.pluginamerica.org/surveys/batteries/model-s/vehicles.php
// @copyright  2015+, Joeviocoe
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js
// @updateURL  https://openuserjs.org/install/Joeviocoe/Plugin_America.user.js
// @downloadURL  https://openuserjs.org/install/Joeviocoe/Plugin_America.user.js
// @grant        none
// @noframes
// ==/UserScript==

// Copyright (c) 2015, Joeviocoe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, 
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, 
//   this list of conditions and the following disclaimer in the documentation 
//   and/or other materials provided with the distribution.
// * Neither the name of Joeviocoe nor the names of its contributors 
//   may be used to endorse or promote products derived from this software 
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
//
/*jslint browser: true*/
/*global $, jQuery, alert*/

var batt_num = 0, chgr_num = 0, DU_num = 0;
var batt_vnum = 0, chgr_vnum = 0, DU_vnum = 0;
var batt = document.createElement("p"), chgr = document.createElement("p"), DU = document.createElement("p");
$("p:contains('vehicles reported so far')")[0].appendChild(batt);
$("p:contains('vehicles reported so far')")[0].appendChild(chgr);
$("p:contains('vehicles reported so far')")[0].appendChild(DU);

$('.stat_table').find('tr').each(function(){
    if ( $(this).hasClass("stat_header_row") ) {
        $(this).find('td').eq(8 ).after('<td>Batteries<br>Swapped</td>');
        $(this).find('td').eq(9 ).after('<td>Chargers<br>Swapped</td>');
        $(this).find('td').eq(10).after('<td>DUs<br>Swapped</td>');
    } else {
        var vid = $(this);
        var vid_num = vid.find('a').text();
        $.ajax({ url: "http://www.pluginamerica.org/surveys/batteries/model-s/vehicle.php?vid=" + vid_num, success: function(data) {  
            if ( data.indexOf("Battery Swapped</b></td><td>Yes") >= 0 ) {
                var this_batt_num = parseInt(data.split("Number of Battery Swaps</b></td><td>")[1].split("</td></tr>")[0]);
                $(vid).find('td').eq(8 ).after('<td>' + this_batt_num + '</td>'); 
                batt_num = batt_num + this_batt_num; batt_vnum++;
            } else { $(vid).find('td').eq(8 ).after('<td>-</td>'); }
            if ( data.indexOf("Charger Replaced</b></td><td>Yes") >= 0 ) {
                var this_chgr_num = parseInt(data.split("Number of Charger Swaps</b></td><td>")[1].split("</td></tr>")[0]);
                if ( this_chgr_num > 100 ) { this_chgr_num = this_chgr_num / 100; }
                $(vid).find('td').eq(9 ).after('<td>' + this_chgr_num + '</td>');
                chgr_num = chgr_num + this_chgr_num; chgr_vnum++;
            } else { $(vid).find('td').eq(9 ).after('<td>-</td>'); }
            if ( data.indexOf("Drive Unit Replaced</b></td><td>Yes") >= 0 ) {
                var this_DU_num = parseInt(data.split("Number of Drive Unit Swaps</b></td><td>")[1].split("</td></tr>")[0]);
                $(vid).find('td').eq(10).after('<td>' + this_DU_num + '</td>'); 
                DU_num = DU_num + this_DU_num; DU_vnum++;
            } else { $(vid).find('td').eq(10).after('<td>-</td>'); }
            Update();
        } 
               });
    }
});

function Update() {
    batt.innerHTML = "Batteries:<br>" + batt_num + " swapped from<br>" + batt_vnum + " vehicles<br>";
    chgr.innerHTML = "Chargers:<br>" + chgr_num + " swapped from<br>" + chgr_vnum + " vehicles<br>";
    DU.innerHTML = "Drive Units:<br>" + DU_num + " swapped from<br>" + DU_vnum + " vehicles<br>";
}