You are on page 1of 6

// ==UserScript==

// @name Dota 2 Marketeer


// @namespace Dota2Marketeer
// @description Dota 2 Shopper
// @include http://steamcommunity.com/id/*
// @include *.steamcommunity.com/id/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @version 0.5
// @author Atsusa Kai (Karl Vincent Init)
// @grant none
// ==/UserScript==
var mURL = 'http://steamcommunity.com/market/listings/570/';
var marketeerDialog = {

m_iMarketSpy: null,
m_iSpyInterval: 8000,

m_bInitialized: false,
m_bCollapsed: false,

m_oMarketItems: null,
m_oItemsTable: null,

m_aItems: 0,

m_sStyles: function() {
var styles = "\
.clr { clear: both; height: 0; }\
#marketeer { width: 100%; min-width: 960px; padding: 10px 0; position: fixed; to
p: 0; left: 0; z-index: 9999; background: rgba( 0, 0, 0, 0.8 ); border-bottom: 2
px solid #777; }\
#marketeer h1.mHeader { margin-bottom: 5px; color: #ffffff; font-weight: bold; }
\
#marketeer a { color: rgba( 128, 207, 107, 0.8 ); }\
#marketeer a:hover { text-decoration: underline; }\
#marketeer h1.mHeader span { font-size: 11px; font-weight: normal; }\
#marketeer p.mDesc { font-size: 12px; margin: 0 0 10px; color: #cccccc; line-hei
ght: normal; }\
#marketeer .mDialog { width: 60%; min-width: 960px; margin: 0 auto; }\
#marketeer table#marketItems { width: 63%; height: 100%; float: left; background
: rgba( 0, 0, 0, 0.9 ); border-collapse: collapse; }\
#marketeer table#marketItems a { color: #ffffff; }\
#marketeer table#marketItems th,\
#marketeer table#marketItems td { height: 30px; vertical-align: middle; padding:
0 5px; }\
#marketeer table#marketItems th { background-color: rgba( 128, 207, 107, 0.7 );
color: #111111; }\
#marketeer table#marketItems td { background-color: rgba( 0, 0, 0, 0.7 ); }\
#marketeer table#marketItems td span.price { color: #ffffff; }\
#marketeer table#marketItems tr.trItem:hover td { background-color: #323232; }\
#marketeer table#marketItems tr#trTotal td { background-color: #444; }\
#marketeer textarea#itemsList { background-color: #323232; border: 2px solid #77
7; color: #eee; padding: 5px; width: 35%; float: right; resize: vertical; min-he
ight: 250px; margin: 0 0 5px; }\
#marketeer a#refreshList { float: right; color: #ffffff; clear: right; display:
block; background-color: rgba( 128, 207, 107, 0.7 ); padding: 3px 6px; border-ra
dius: 4px; }\
#marketeer a#refreshList:hover { background-color: rgba( 128, 207, 107, 1 ); }\
.white { color: #ffffff !important; }\
.green { color: #00cc00 !important; }\
.red { color: #cc1414 !important; }\
";
return styles;
},

Init: function() {
this.AppendStyle();
this.RenderDialog();
this.GetContent();
this.Reset();
this.HookEvents();
},

Reset: function() {

clearTimeout( this.m_iMarketSpy );

this.RenderItemsTable();

marketeerDialog.m_iMarketSpy = setTimeout( marketeerDialog.GetItems, 100
);

localStorage.setItem( 'itemslist', $J( '#itemsList' ).val() );
},

GetContent: function() {
if( typeof( Storage ) !== "undefined" ) {
$J( '#itemsList' ).val( localStorage.getItem( 'itemslist' ) );
}
},

HookEvents: function() {
var _this = this;
$J( 'a#refreshList' ).on( 'click', function(e) {
_this.Reset();
});
},

AppendStyle: function() {
var css = this.m_sStyles(),
head = document.getElementsByTagName('head')[0],
style = document.createElement('style');

style.type = 'text/css';

if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild( document.createTextNode(css) );
}

head.appendChild(style);
},

RenderDialog: function() {
var mainDiv = $J( '<div id="marketeer"></div>' ),
itemTable = $J( '<table id="marketItems" border="0" cellpadding="0
" cellspacing="0"></table>' ),
mainDialog = $J( '<div class="mDialog"></div>' ),
buttonRefresh = $J( '<a id="refreshList" href="javascript:void(0);">
Refresh</a>' ),
itemListTA = $J( '<textarea id="itemsList" placeholder="Place the e
xact display name of the items here. Each item is separated by a new line."></te
xtarea>' );

mainDialog.append( $( '<h1 class="mHeader">Dota 2 Item Marketeer v0.5 <s
pan>by <a target="_blank" href="https://www.facebook.com/kai.zard">Atsusa Kai<a/
></span></h1>' ) );
mainDialog.append( $( '<p class="mDesc">Monitors the lowest pric
es of the items on your list from the <a target="_blank" href="http://steamcommu
nity.com/market/">Community Market</a>.</p>' ) );
mainDialog.append( itemTable );

this.m_oMarketItems = itemTable;

mainDialog.append( itemListTA );
mainDialog.append( buttonRefresh );
mainDialog.append( $J( '<div class="clr"></div>' ) );

mainDiv.append( mainDialog );

this.m_oMarketItems = itemTable;

$J( 'body' ).append( mainDiv );
},

RenderItemsTable: function() {
var itemTableTR = '';

this.m_oMarketItems.html( '' );

itemTableTR = $J( '<tr id="trHead"></tr>' );
itemTableTR.append( $J( '<th class="tItemName">Item Name</th>' ) );
itemTableTR.append( $J( '<th class="tItemPrice">Price <span styl
e="font-weight: normal;">( Previous Price )</span></th>' ) );

this.m_oMarketItems.append( itemTableTR );

this.ListItems();
},

ListItems: function() {
this.m_aItems = $J( '#itemsList' ).val().trim().split("\n");

if ( ! this.m_aItems.length || $J( '#itemsList' ).val() == '' ) {
this.m_aItems = [];
return false;
}

var x = 0;

for ( x=0;x<this.m_aItems.length;x++ ) {
var itemTableTR = $J( '<tr id="trItem-' + x + '" class="trItem">
</tr>' ),
itemName = $J( '<td id="tdItemName"><a target="_blank" hr
ef="' + mURL + this.m_aItems[x] + '">' + this.m_aItems[x] + '</a></td>' ),
itemPrice = $J( '<td id="tdItemPrice"></td>' );

itemTableTR.append( itemName );
itemTableTR.append( itemPrice );

this.m_oMarketItems.append( itemTableTR );
}

},

GetItems: function() {

clearTimeout( marketeerDialog.m_iMarketSpy );

var _this = this,
total = 0,
totalPrev = 0,
itemPriceChanged = false;

$.each( marketeerDialog.m_aItems, function( i, el ) {

var url = mURL + marketeerDialog.m_aItems[i];

$J.post( url, function( response ) {

var html = $J( response ),
item = html.find( ".market_recent_listing_row" ).eq(0);

if ( item.length ) {
var img = item.find( ".market_listing_item_img_c
ontainer img" ).attr( "src" ),
name = item.find( ".market_listing_item_name"
).text(),
price = item.find( ".market_listing_price_with
_fee" ).text(),
priceNoFee = item.find( ".market_listing_price_with
out_fee" ).text(),
prevPrice = $J( '#trItem-' + i ).data( 'prevPrice' ),
priceN = 0,
prevPN = 0,
color = 'white';

prevPrice = ( prevPrice === undefined )
? -1 : prevPrice;
priceN = price.match(/\d+\.\d+/);

if ( prevPrice < 0 ) {
$J( '#trItem-' + i ).data( 'prev
Price', price );
prevPrice = price;
}

if ( prevPrice != "" ) {

prevPN = prevPrice.match(/\d+\.\
d+/);

if ( priceN ) {
priceN = priceN[0];
}

if ( prevPN ) {
prevPN = prevPN[0];
}

if ( priceN != prevPN ) {

if ( priceN > prevPN ) {
color = 'red';
} else {
color = 'green';
}
if ( $J( '#trItem-' + i
).data( 'price' ) != price ) {
$J( '#trItem-' +
i ).data( 'prevPrice', $J( '#trItem-' + i ).data( 'price' ) );
} else {
//$J( '#trItem-'
+ i ).data( 'prevPrice', price );
}
itemPriceChanged = true;

} else {
color = 'white';

}
}
total += ( parseFloat( priceN ) == "NaN"
) ? 0 : parseFloat( priceN );
totalPrev += ( parseFloat( prevPN ) == "
NaN" ) ? 0 : parseFloat( prevPN );
$J( '#trItem-' + i ).find( '#tdItemPrice
' ).html( '<span class="price ' + color + '">' + price + '</span>' + '(' + prevP
rice + ')' );
$J( '#trItem-' + i ).data( 'priceN', pri
ceN );
$J( '#trItem-' + i ).data( 'price', pric
e );
}

}).done( function() {
var color = marketeerDialog.GetPriceColor( total
, totalPrev );
$J( '#tdItemTotalPrice' ).html( '<strong class="' + color + '">$
' + parseFloat( Math.round(total * 100) / 100 ).toFixed( 2 ) + ' USD</strong> (
$ ' + parseFloat( Math.round(totalPrev * 100) / 100 ).toFixed( 2 ) + ' USD )' )
;
});
});
var color = marketeerDialog.GetPriceColor( total, totalPrev );
if ( ! $J( '#trTotal' ).length ) {
var itemTableTR = $J( '<tr id="trTotal"></tr>' ),
itemName = $J( '<td id="tdItemName"><strong>Total</strong
></td>' ),
itemPrice = $J( '<td id="tdItemTotalPrice"></td>' );

itemTableTR.append( itemName );
itemTableTR.append( itemPrice );
marketeerDialog.m_oMarketItems.append( itemTableTR );

$J( '#tdItemTotalPrice' ).html( '<strong class="' + color + '">$ ' +
parseFloat( Math.round(total * 100) / 100 ).toFixed( 2 ) + ' USD</strong> ( $ '
+ parseFloat( Math.round(totalPrev * 100) / 100 ).toFixed( 2 ) + ' USD )' );
} else {
$J( '#tdItemTotalPrice' ).html( '<strong class="' + color + '">$ ' +
parseFloat( Math.round(total * 100) / 100 ).toFixed( 2 ) + ' USD</strong> ( $ '
+ parseFloat( Math.round(totalPrev * 100) / 100 ).toFixed( 2 ) + ' USD )' );
}
marketeerDialog.m_iMarketSpy = setTimeout( marketeerDialog.GetIt
ems, marketeerDialog.m_iSpyInterval );
},
GetPriceColor: function( price1, price2 ) {
var color = 'white';
if ( price1 > price2 ) {
color = 'red';
} else if ( price1 < price2 ) {
color = 'green';
} else {
color = 'white';
}
return color;
}
};
$J(document).ready( function() {
marketeerDialog.Init();

});

You might also like