Skip to content

Instantly share code, notes, and snippets.

@granthenke
Created September 8, 2016 16:19
Show Gist options
  • Save granthenke/3cd55f3d708a4ce60f02708419e380da to your computer and use it in GitHub Desktop.
Save granthenke/3cd55f3d708a4ce60f02708419e380da to your computer and use it in GitHub Desktop.
Yahoo! Pro Football Pickem AutoFill 2016: A user script that adds a button to automatically pick the favorited teams based on pick distribution
// ==UserScript==
// @name Yahoo! Pro Football Pickem AutoFill 2016
// @namespace https://gist.github.com/granthenke/
// @version 0.1
// @description Adds a button to automatically pick the favorited teams based on pick distribution
// @author Grant Henke
// @match https://football.fantasysports.yahoo.com/pickem/*/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// Function to populate favorite details object from table contents
function getFavoriteDetails(favoritesRow) {
return {
name: favoritesRow.getElementsByClassName("l")[0].getElementsByTagName("a")[0].innerHTML,
rate: favoritesRow.getElementsByClassName("number")[0].innerHTML.slice(0,-1), // Remove the last % char
isHome: favoritesRow.getElementsByClassName("l")[0].innerHTML.slice(0,1) == "@",
isFavorite: favoritesRow.classList.contains("pick-preferred")
};
}
// Add a rank based on the highest pick rate
function addRank(favoriteDetailsArray) {
// Get ranks for each pick
var ranks = new Array(favoriteDetailsArray.length);
for (var i = 0; i < ranks.length; i++) ranks[i] = i;
ranks.sort(function (a, b) { return favoriteDetailsArray[b].maxRate - favoriteDetailsArray[a].maxRate; });
// Set the rank on each pick
for (var j = 0; j < ranks.length; j++) favoriteDetailsArray[ranks[j]].rank = j;
}
// Function to set the pick
function setPick(pickRow, favoriteDetails) {
var radioCell = (favoriteDetails.left.isFavorite) ? pickRow.cells[2] : pickRow.cells[4];
var radioInput = radioCell.getElementsByTagName("input")[0];
radioInput.checked = true;
}
// Function to set the confidence
function setConfidence(confidenceRow, favoriteDetails, numPicks) {
var selectInput = confidenceRow.cells[7].getElementsByTagName("select")[0];
selectInput.selectedIndex = numPicks - favoriteDetails.rank;
}
// Function to calculate an estimated score based on pick distribution
// Divides the rate by 3.5 and rounds to the closest number divisible by 7
// Ex: 100 = 28, 75 = 21, 50 = 14, 25 = 7
function estimateScore(pickRate) {
var reductionFactor = 3.5;
var divisibleBy = 7;
var reduced = Math.round(pickRate / reductionFactor);
var rounded = Math.round(reduced / divisibleBy) * divisibleBy;
return rounded;
}
// Function to set the "Tiebreaker 1" based on the rate
function setTieBreaker1(tiebreaker1Row, favoriteDetails) {
var awayDetails = (favoriteDetails.left.isHome) ? favoriteDetails.right : favoriteDetails.left;
var awayScore = estimateScore(awayDetails.rate);
var awayInput = tiebreaker1Row.cells[1].getElementsByTagName("input")[0];
awayInput.value = awayScore;
var homeDetails = (favoriteDetails.left.isHome) ? favoriteDetails.left : favoriteDetails.right;
var homeScore = estimateScore(homeDetails.rate);
var homeInput = tiebreaker1Row.cells[3].getElementsByTagName("input")[0];
homeInput.value = homeScore;
}
// Get select value by html content
function getSelectIndexByContent(select, content) {
for (var i=0; i < select.length; i++){
if (select.options[i].text == content) {
return i;
}
}
return null;
}
// Actual auto fill function called by the button
function doAutoFill() {
// Lookup the "Pick Distribution" favorites table
var favoritesTable = document.getElementById("sidebar-pickdistribution").getElementsByClassName("yspNflPickGroupPickTable")[0].tBodies[0];
// Populate the a favoriteDetailsArray based on the favoritesTable
// The favoritesTable has 2 rows per pick (left and right)
var numPicks = Math.floor(favoritesTable.rows.length / 2);
var favoriteDetailsArray = new Array(numPicks);
for (var i = 0; i < favoriteDetailsArray.length; i++) {
var pick = {
left: getFavoriteDetails(favoritesTable.rows[i*2]),
right: getFavoriteDetails(favoritesTable.rows[i*2+1]),
};
pick.maxRate = Math.max(pick.left.rate, pick.right.rate);
favoriteDetailsArray[i] = pick;
}
addRank(favoriteDetailsArray);
// Set the picks and confidence based on the collected favorites details
var pickTableBody = document.getElementById("ysf-picks-table").tBodies[0];
for (var j = 0; j < pickTableBody.rows.length; j++) {
setPick(pickTableBody.rows[j], favoriteDetailsArray[j]);
setConfidence(pickTableBody.rows[j], favoriteDetailsArray[j], numPicks);
}
// Set all the "Tiebreaker 1" values if they exist
var tiebreaker1Div = document.getElementById("tiebreaker-1");
if (tiebreaker1Div) {
var tiebreaker1Table = tiebreaker1Div.getElementsByClassName("game-table")[0].tBodies[0];
for (var k = 0; k < tiebreaker1Table.rows.length; k++) {
setTieBreaker1(tiebreaker1Table.rows[k], favoriteDetailsArray[numPicks - k - 1]);
}
}
// Set all the "Tiebreaker 2" values if they exist
var tiebreaker2Div = document.getElementById("tiebreaker-2");
if (tiebreaker2Div) {
var tiebreaker2Table = tiebreaker2Div.getElementsByClassName("game-table")[0].tBodies[0];
var favoriteDetailsByRank = favoriteDetailsArray.sort(function (a, b) { return b.maxRate - a.maxRate;});
// Pick the most based on the highest rank
var mostTeamDetails = (favoriteDetailsByRank[0].left.isFavorite) ? favoriteDetailsByRank[0].left : favoriteDetailsByRank[0].right;
var mostSelect = tiebreaker2Table.rows[0].cells[1].getElementsByTagName("select")[0];
var mostIndex = getSelectIndexByContent(mostSelect, mostTeamDetails.name);
mostSelect.selectedIndex = mostIndex;
// Pick the least based on the lowset rank (opponent of the highest)
var leastTeamDetails = (favoriteDetailsByRank[0].left.isFavorite) ? favoriteDetailsByRank[0].right : favoriteDetailsByRank[0].left;
var lestSelect = tiebreaker2Table.rows[1].cells[1].getElementsByTagName("select")[0];
var lestIndex = getSelectIndexByContent(lestSelect, leastTeamDetails.name);
lestSelect.selectedIndex = lestIndex;
}
}
// Function to append a button with an action
function addButton(toNode, text, onclick) {
var element = document.createElement("input");
element.setAttribute("type", "button");
element.setAttribute("value", text);
element.addEventListener("click", onclick);
//element.setAttribute("class", "ysf-cta ysf-cta-small ysf-cta-auto");
toNode.appendChild(element);
}
// Add the auto fill button to the page
var buttonBar = document.getElementsByClassName("ysf-savecancel-buttonbar")[0].getElementsByTagName("p")[0];
addButton(buttonBar, "Auto-Fill", doAutoFill);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment