Skip to content

Instantly share code, notes, and snippets.

@benjaminplee
Created August 2, 2010 19:20
Show Gist options
  • Save benjaminplee/505165 to your computer and use it in GitHub Desktop.
Save benjaminplee/505165 to your computer and use it in GitHub Desktop.
Adds context menu functions for loading and displaying contract prices
// ==UserScript==
// @name ContractPrice
// @namespace http://yardspoon.com
// @description Displays curret contract prices
// @include http://baseball.fantasysports.yahoo.com/b1/161295/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @require http://jquery-json.googlecode.com/files/jquery.json-2.2.min.js
// ==/UserScript==
function process_name_text(original) {
return original.replace(/[\[\]\s\d\$\-]/g,'')
}
function process_tx_price(text) {
text = text.toLowerCase().replace(/[\(\)\$\s]/g,'')
return (text == 'freeagents') ? 1 : Number(text.substr(7))
}
function loadDraftPrices() {
var prices = {}
$('.player').reverse().each(function(index, player) {
var player_element = $(player) // GM workaround
var name = process_name_text(player_element.text())
var draft_price = Number(player_element.next().text().replace(/\$/g, ''))
prices[name] = draft_price
})
set_prices(prices)
}
function loadTransactions() {
var prices = get_prices()
$('td.player').reverse().each(function(index, player_td) {
var player_td_element = $(player_td) // GM workaround
var player_name = process_name_text($(player_td_element.children()[0]).text())
var player_price = process_tx_price($($(player_td_element.next()).next()).text())
GM_log('Processing player: "' + player_name + '" with price: "' + player_price + '"')
prices[player_name] = player_price
})
set_prices(prices)
}
function show_stored_prices() {
var prices = get_json_prices()
GM_log(prices)
prompt('copy these', prices)
}
function clear_stored_prices() {
set_prices({})
}
function get_json_prices() {
return GM_getValue('ContractPrices', '{}')
}
function get_prices() {
return $.evalJSON(get_json_prices())
}
function set_prices(prices) {
set_json_prices($.toJSON(prices))
}
function set_json_prices(jsonPrices) {
GM_setValue('ContractPrices', jsonPrices)
GM_log('Saved Prices: ' + jsonPrices)
}
function display_contract_prices() {
var prices = get_prices()
$('a.name').each(function(index, element) {
var element_anchor = $(element)
var name = process_name_text(element_anchor.text())
var contract_price = prices[name] | 0
$('<span>&nbsp;&nbsp;[$' + contract_price + ']</span>').insertAfter(element_anchor)
})
}
function load_prices_JSON() {
var prices = prompt('enter prices here as JSON')
GM_log(prices)
set_json_prices(prices)
}
GM_registerMenuCommand('Load Draft Prices', loadDraftPrices)
GM_registerMenuCommand('Load Transactions', loadTransactions, 'm', 'control', 'm')
GM_registerMenuCommand('Log Saved Prices', show_stored_prices)
GM_registerMenuCommand('Clear Saved Prices', clear_stored_prices)
GM_registerMenuCommand('Display Contract Prices', display_contract_prices)
GM_registerMenuCommand('Load Contract Prices from JSON', load_prices_JSON)
$.fn.reverse = [].reverse
{"VíctorMartínez":27,"TroyTulowitzki":35,"DanUggla":17,"DustinMcGowan":2,"PhilHughes":2,"J.J.Hardy":1,"JoshWillingham":1,"MiltonBradley":1,"AlbertoCallaspo":1,"DavidAardsma":2,"MatLatos":1,"CarlosQuentin":4,"JakePeavy":20,"CarlosGonzález":1,"NickJohnson":7,"RickyNolasco":25,"ClaytonKershaw":24,"ShaneVictorino":13,"JoshJohnson":27,"JaysonWerth":24,"MarkReynolds":15,"ZackGreinke":38,"ClayBuchholz":7,"TimHudson":1,"TravisHafner":1,"AaronCook":1,"JoséGuillén":1,"DanielBard":1,"DallasBraden":1,"RyanFranklin":1,"ClintBarmes":1,"RyanDoumit":1,"GarrettJones":1,"ÁlexRíos":1,"DerekLowe":1,"MattGarza":12,"YovaniGallardo":28,"BillyButler":20,"JoséReyes":20,"MarkTeixeira":33,"EvanLongoria":35,"AlbertPujols":55,"RafaelRodríguez":1,"IvánRodríguez":1,"HenryRodríguez":1,"FernandoRodríguez":1,"WilkinRamírez":1,"RamónRamírez":1,"EdwarRamírez":1,"MaxRamirez":1,"DavidOrtiz":4,"JairJurrjens":13,"RaúlIbañez":13,"JorgeCantú":10,"PlácidoPolanco":7,"DavidDeJesús":9,"JoshBeckett":22,"JimmyRollins":26,"RyanZimmerman":29,"MattWieters":16,"JacobyEllsbury":31,"JustinUpton":32,"ChrisCarpenter":40,"PrinceFielder":40,"AdamDunn":22,"GeovanySoto":7,"RichHarden":1,"ChrisIannetta":1,"JorgeDeLaRosa":5,"MikeAdams":4,"ColbyLewis":1,"KrisMedlen":1,"ChadQualls":1,"ToddHelton":4,"JohnnyDamon":1,"JamesShields":3,"MarcoScutaro":12,"AndreEthier":13,"MaxScherzer":10,"NateMcLouth":12,"BrettAnderson":16,"ChoneFiggins":1,"KevinSlowey":16,"ChaseUtley":33,"ÁlexRodríguez":45,"HanleyRamírez":49,"BradHawpe":11,"AdamWainwright":28,"KyleLohse":1,"BradLidge":1,"MagglioOrdóñez":6,"JohnMaine":1,"JobaChamberlain":3,"ScottKazmir":5,"MarlonByrd":1,"RandyWolf":1,"KosukeFukudome":8,"J.A.Happ":1,"BronsonArroyo":1,"JustinDuchscherer":5,"DaisukeMatsuzaka":1,"IanStewart":9,"JackCust":1,"JustinMorneau":17,"DavidWright":34,"MiguelCabrera":38,"WandyRodríguez":25,"TommyHanson":25,"FranciscoLiriano":1,"CoreyHart":1,"LyleOverbay":1,"AlexeiRamírez":1,"MichaelWuertz":4,"AroldisChapman":20,"KevinKouzmanoff":1,"CodyRoss":1,"EdwinJackson":8,"SkipSchumaker":6,"KurtSuzuki":7,"CristianGuzmán":6,"JonLester":20,"ToriiHunter":18,"AdamLind":24,"BrandonPhillips":14,"CarlosZambrano":1,"CCSabathia":33,"JohanSantana":27,"BrianMcCann":22,"AsdrubalCabrera":16,"VernonWells":1,"JuanRivera":1,"PaulKonerko":1,"AlfredoAceves":1,"MarkBuehrle":1,"RyanRowlandSmith":1,"LeoNúñez":1,"JonathanSánchez":1,"AlexGordon":1,"RajaiDavis":1,"NolanReimold":1,"JoséLópez":1,"J.D.Drew":4,"BobbyAbreu":22,"MikeNapoli":1,"RoyHalladay":44,"TimLincecum":66,"JoeMauer":40,"JeffNiemann":1,"RandyWells":1,"ElvisAndrus":2,"BengieMolina":3,"JoakimSoria":11,"JamesLoney":11,"ChrisCoghlan":7,"AdamJones":14,"RickieWeeks":8,"AdriánBéltre":9,"JoséValverde":11,"ScottBaker":1,"NelsonCruz":14,"JeredWeaver":16,"JonathanBroxton":14,"AlfonsoSoriano":20,"B.J.Upton":11,"BrianRoberts":4,"MichaelYoung":15,"DerrekLee":24,"MattKemp":36,"MattHolliday":35,"JustinVerlander":28,"GradySizemore":2,"LanceBerkman":18,"GordonBeckham":1,"ShaunMarcum":1,"LukeGregerson":1,"MartínPrado":1,"EugenioVelez":1,"NyjerMorgan":1,"GeorgeSherrill":1,"OrlandoCabrera":1,"ErvinSantana":5,"HunterPence":1,"NeftaliFeliz":1,"ChadBillingsley":18,"MaicerIzturis":1,"JonathanPapelbon":1,"ColeHamels":25,"MiguelMontero":1,"ShinSooChoo":25,"BrianFuentes":1,"GavinFloyd":1,"JulioBorbón":8,"RyanLudwick":47,"JohnDanks":10,"JasonKubel":1,"BillyWagner":6,"CaseyBlake":1,"AndrewBailey":13,"HowieKendrick":1,"JasonBartlett":10,"MarianoRivera":14,"HustonStreet":7,"JhonnyPeralta":1,"UbaldoJiménez":24,"DenardSpan":1,"AaronHill":1,"KendryMorales":1,"JoeyVotto":19,"IchiroSuzuki":25,"AramisRamírez":1,"RussellMartin":1,"ChipperJones":12,"NickSwisher":7,"DanHaren":36,"JavierVázquez":21,"BusterPosey":1,"FrankFrancisco":1,"MikeCameron":3,"J.P.Howell":3,"CarlosBeltrán":14,"VladimirGuerrero":2,"A.J.Burnett":1,"HirokiKuroda":3,"KevinCorreia":1,"EdinsonVólquez":1,"YunelEscobar":8,"StephenDrew":1,"FélixHernández":35,"KevinYoukilis":27,"DustinPedroia":26,"JasonBay":25,"JohnLackey":15,"MarkDeRosa":14,"MannyRamírez":21,"RyanTheriot":1,"HidekiMatsui":1,"DaleThayer":1,"JesseLitsch":1,"RandyFlores":1,"MichaelCuddyer":12,"JasonVaritek":1,"CurtisGranderson":19,"CarlosPeña":16,"ChrisYoung":1,"MattCain":25,"RobinsonCanó":19,"JohnBaker":2,"CarlosLee":22,"BenSheets":10,"CliffLee":35,"DerekJeter":29,"StephenStrasburg":8,"RyanBraun":44,"NickMarkakis":27,"BenZobrist":27,"MichaelGonzález":1,"FranciscoCordero":1,"AdamLaRoche":5,"MattThornton":5,"RickPorcello":2,"FranciscoRodríguez":4,"RafaelSoriano":7,"JorgePosada":1,"RoyOswalt":12,"MiguelTejada":4,"JohnnyCueto":10,"DavidPrice":5,"BrandonWebb":9,"AaronHarang":8,"JoshHamilton":10,"AndrewMcCutchen":18,"PabloSandoval":21,"AdriánGonzález":25,"RafaelFurcal":3,"BrandonInge":1,"TedLilly":14,"BrianMatusz":1,"JayBruce":8,"YadierMolina":1,"BrendanRyan":1,"ErikBedard":5,"BrianWilson":1,"CarlosMármol":6,"MarkTeahen":1,"TakashiSaito":1,"MichaelBourn":1,"HeathBell":10,"RickAnkiel":1,"RyanDempster":13,"LastingsMilledge":2,"ColbyRasmus":9,"IanKinsler":32,"RyanHoward":35,"CarlCrawford":28,"SergioRomo":1,"TravisSnider":1,"JaimeGarcía":1,"KyleBlanks":1,"ChrisDavis":1,"DexterFowler":1,"KellyJohnson":1,"RafaelBetancourt":1,"TrevorHoffman":1,"SeanRodriguez":1,"ScottRolen":1,"C.J.Wilson":1,"JustinMasterson":1,"A.J.Pierzynski":1,"RyanMadson":1,"OrlandoHudson":1,"JoelPiñeiro":1,"CarlPavano":1,"NickBlackburn":1,"DaricBarton":1,"MikeAviles":1,"MattDaley":1,"OctavioDotel":1,"KenshinKawakami":1,"JoeBlanton":1,"BarryZito":1,"GabySánchez":1,"JasonHeyward":26,"RodBarajas":1,"CarlosVillanueva":1,"JohnLannan":1,"BradPenny":1,"JensenLewis":1,"JeffClement":1,"ScottFeldman":1,"MarkEllis":1,"ScottPodsednik":1,"ClaytonRichard":1,"ChaseHeadley":1,"JeffFrancoeur":1,"CaseyMcGehee":1,"MiguelOlivo":1,"AkiIwamura":1,"RickyRomero":1,"AndruwJones":1,"JuanUribe":1,"CameronMaybin":1,"JeremyBonderman":1,"MikePelfrey":1,"ÁlexGonzález":1,"TomGorzelanny":1,"SergioSantos":1,"TyWigginton":1,"JasonKendall":1,"JeremyHermida":1,"RyanSweeney":1,"TylerClippard":1,"JoséContreras":1,"JasonVargas":1,"AndyPettitte":1,"GioGonzález":1,"GrantBalfour":1,"SeanMarshall":1,"AaronRowand":1,"DougFister":1,"LivánHernández":1,"NickHundley":1,"FaustoCarmona":1,"MitchTalbot":1,"KevinGregg":1,"ChrisSnyder":1,"JoséBautista":1,"FranklinGutiérrez":1,"CliffPennington":1,"CarlosSilva":1,"DrewStubbs":1,"CarlosRuiz":10,"ErickAybar":1,"BrettCecil":1,"AustinJackson":1,"AubreyHuff":1,"ArthurRhodes":1,"AdamRosales":1,"DavidFreese":1,"JoelZumaya":1,"WadeLeBlanc":1,"ÉdgarRentería":1,"BrettGardner":1,"WadeDavis":1,"BrandonMorrow":1,"EvanMeek":1,"RandyWinn":1,"IanKennedy":1,"IkeDavis":1,"PaulMaholm":1,"HisanoriTakahashi":1,"JohnBuck":1,"StarlinCastro":1,"ÁngelPagán":1,"AustinKearns":1,"MattCapps":1,"RamónHernández":1,"JonnyGomes":1,"IanDesmond":1,"EricHinske":1,"YorvitTorrealba":1,"BrennanBoesch":1,"FranciscoCervelli":1,"AnibalSánchez":1,"JoaquínBenoit":1,"MikeLeake":1,"ClayHensley":1,"JhoulysChacin":1,"JonGarland":1,"EdwinEncarnación":1,"AndrésTorres":1,"WillVenable":1,"JeffFrancis":1,"JeffKeppinger":1,"DerekHolland":1,"TroyGlaus":1,"J.J.Putz":1,"BrettMyers":3,"JohnEly":1,"CarlosSantana":1,"JoeThatcher":1,"ArmandoGalarraga":1,"JohnJaso":1,"HongChihKuo":1,"TommyHunter":1,"MikeStanton":1,"RandyWilliams":1,"RyanSpilborghs":1,"TrevorCahill":1,"DelmonYoung":1,"R.A.Dickey":1,"JoseTabata":1,"FreddySánchez":1,"ManuelCorpas":1,"JakeArrieta":1,"MattBelisle":1,"BruceChen":1,"JasonHammel":1,"JasonCastro":3,"DarrenOliver":1,"CarlosGuillén":1,"JustinThomas":2,"RonnyPaulino":1,"WilsonBetemit":1,"KevinFrandsen":1,"AnthonyLerew":1,"MattLaPorta":1,"JaysonNix":1,"VicentePadilla":1,"JonathonNiese":1,"FelipeLópez":1,"ChrisNarveson":1,"DougDavis":1,"JeremyHellickson":1,"JonJay":1,"TravisWood":1,"JordanZimmermann":1,"NeilWalker":1,"JonnyVenters":1,"BarryEnright":1,"LukeScott":1,"TylerColvin":1,"ReidBrignac":1,"MadisonBumgarner":1,"ErnestoFrieri":1,"DomonicBrown":8,"ChrisJohnson":1,"PedroAlvarez":1,"BrettWallace":11,"JerryHairstonJr.":1,"JoshTomlin":1,"DanielHudson":1,"BrianDuensing":1,"JeremyGuthrie":1,"DustinMoseley":1,"J.P.Arencibia":1,"OmarInfante":1,"MikeMinor":3,"JuanPierre":1,"ChrisDenorfia":1,"MannyParra":1,"RyanRaburn":1,"LorenzoCain":1,"BryanBullington":1,"CocoCrisp":1,"JedLowrie":1,"MattJoyce":1,"JakeWestbrook":1,"LoganMorrison":1,"ChrisCapuano":1,"DesmondJennings":1,"BrandonLyon":1,"JimThome":1,"IvánNova":1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment