I now know how to load columns, of a table, from an external webpage.
Now I want to expand on that and:
- Fetch tabular data from several pages (rankings by player position).
- Merge it into one master table.
This is the URL (http:...fantasysports.yahoo.com...pos=QB) that the script currently fetches. The columns are the team name and the team's rank for the various positions.
I want to have it iterate over other positions (i.e. WR, RB, TE). This is done by just changing the last 2 letters of the URL to its respective value. I then want to have all this data in a single array where first column is the team name, second column is the ranking # for the QB position, third column is ranking # for WR position, and so on.
My plan was to put the GM_xmlhttpRequest
in a for
loop that iterates over the different position names.
My code is able to display separate tables for the positions but for some reason they are not in order.
Another issue I ran into was the variable scope. Currently newStatTable
is defined inside the parseResponse
function but I am unable to access it from outside the function. I tried removing var
from var newStatTable
to make it a global but it did not work.
Here is a sample output array that I am looking for (Note: I just randomly selected these ranks):
TeamName QB WR TE and so on...
--- -- -- --
Jacksonville Jaguars 1 6 28
Sanfrancisco 49ers 4 2 32
Seattle Seahawks 31 5 10
Here is my attempt.
// ==UserScript==
// @name _Grab stuff of a *static*, third-party web site.
// @include http://football.fantasysports.yahoo.com/*
// @include https://football.fantasysports.yahoo.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant GM_xmlhttpRequest
// ==/UserScript==
pos = ["QB", "WR", "RB", "TE", "K", "DEF"];
for (x in pos) {
GM_xmlhttpRequest ( {
method: "GET",
url: "http://football.fantasysports.yahoo.com/f1/326198/pointsagainst?pos=" + pos[x],
onload: parseResponse,
onerror: function (e) { console.error ('**** error ', e); },
onabort: function (e) { console.error ('**** abort ', e); },
ontimeout: function (e) { console.error ('**** timeout ', e); }
} );
}
function parseResponse (response) {
var parser = new DOMParser ();
var ajaxDoc = parser.parseFromString (response.responseText, "text/html");
var statRows = ajaxDoc.querySelectorAll ("#statTable0 > tbody > tr");
var newStatTable = $(statRows).map ( function () {
var tblRow = $(this);
var teamRank = parseInt (tblRow.find (".rank-indicator").text().trim(), 10);
var teamName = tblRow.find ("td:eq(1)").text().trim().split(" vs")[0];
return [ [teamName, teamRank] ]; //Return Teamname, Rank #
} ).get ();
console.log (newStatTable);
}