For a static page (like the one you linked), use GM_xmlhttpRequest()
and DOMParser
to extract the elements you want. See below.
For a dynamic page(AJAX driven), use the techniques from How to get an AJAX get-request to wait for the page to be rendered before returning a response?
Here's a complete script showing how to extract info from a third party page and make it into an array variable:
// ==UserScript==
// @name _Grab stuff of a *static*, third-party web site.
// @include https://stackoverflow.com/questions/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant GM_xmlhttpRequest
// ==/UserScript==
GM_xmlhttpRequest ( {
method: "GET",
url: "http://football.fantasysports.yahoo.com/f1/326198/pointsagainst?pos=QB&ntid=",
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 ();
/* IMPORTANT!
1) For older browsers, see
https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
for a work-around.
2) jQuery.parseHTML() and similar is bad because it causes images, etc., to be loaded.
*/
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();
return [ [teamRank, teamName] ];
} ).get ();
/*-- newStatTable, is a 2-D array like:
[ [1, "Team A"],
[2, "Team B"],
[3, "Team C"],
//etc...
]
*/
console.log (newStatTable);
//alert (newStatTable);
}