goatwasher / Ethtrader Price

// ==UserScript==
// @name        Ethtrader Price
// @namespace   ethtraderprice
// @author      Goatwasher’s fiancée
// @license     MIT
// @description Displays historic Ether price on /r/ethtrader
// @version     0.3
// @include     https://www.reddit.com/r/ethtrader/*
// @include     https://www.reddit.com/user/*
// @grant       GM_addStyle
// @require     http://code.jquery.com/jquery-1.12.4.min.js
// ==/UserScript==


// CSS styles
GM_addStyle( '.ether-price { display: inline-block; margin-left: .5em; font-weight: bold; color: #000; } .ether-price.edited:before { content: " / *" } .ether-price-development-pos { color: #29c14d; } .ether-price-development-neg { color: #ee2828; }');


( function() {

    'use strict';

    $( function() {

        // Get comment dates, filter oldest and newest, add to JSON url
        var timestampArray = [];

        $( '.tagline time' ).each( function() {

            var dateString = $( this ).attr( 'datetime' );
            dateString = dateString.split(' ').join('T');

            var dateUnix = new Date(dateString);
            dateUnix = dateUnix.getTime() / 1000;

            timestampArray.push( dateUnix );
        });

        var JSONstartDate = Math.min.apply( Math, timestampArray ) - 86700, // Get start date 24h before first comment in seconds [86400] + 300 period
            JSONendDate   = Math.max.apply( Math, timestampArray ),
            JSONurl       = 'https://poloniex.com/public?command=returnChartData&currencyPair=USDT_ETH&start=' + JSONstartDate + '&end=' + JSONendDate + '&period=300';

        console.log( JSONstartDate );
        console.log( JSONendDate );

        $.getJSON( JSONurl, function( data ) {

            // Loop through comment dates, convert to unix timestamp
            $( '.tagline time' ).each( function() {

                var dateString = $( this ).attr( 'datetime' );
                dateString = dateString.split(' ').join('T');

                var dateUnix = new Date(dateString);
                dateUnix = dateUnix.getTime() / 1000;

                // Get closest unix timestamp in JSON array
                var x = dateUnix,
                    difference = 0,
                    bestIndex = 0,
                    bestDifference = Infinity,
                    i, cur, JSONdate;

                for (i = 0; i < data.length; i++) {
                    cur = data[i];
                    JSONdate = cur['date'];
                    difference = Math.abs(x - JSONdate);
                    if (difference < bestDifference) {
                        bestDifference = difference;
                        bestIndex = i;
                    }
                }

                // Truncate price
                var weightedAverageRounded = data[bestIndex]['weightedAverage'].toFixed(2);
                var weightedAverageRounded24hBefore = data[bestIndex - 288]['weightedAverage'].toFixed(2);
                var percentageDifference = ( ( weightedAverageRounded - weightedAverageRounded24hBefore ) / weightedAverageRounded * 100 ).toFixed(2);

                if ( percentageDifference <= 0 ) {
                    var developmentClass = 'ether-price-development-neg';
                } else {
                    var developmentClass = 'ether-price-development-pos';
                }

                // Add price to HTML
                $( this ).parent().append( '<span class="ether-price">$' + Number( weightedAverageRounded ) + '<span> <span class="ether-price-development ' + developmentClass + '">' + percentageDifference + '%</span>' );

                // In case of edited comments add special class
                if ( $( this ).parent().children( '.ether-price' ).length > 1 ) {
                    $( this ).parent().children( '.ether-price' ).last().addClass( 'edited' );
                }

            });

        });

    });

})();