我現在知道how to load columns, of a table, from an external webpage。Userscript循環幾個HTTP請求併合並結果?
現在我想對擴大:
- 從多個頁面(由玩家持倉排名表)獲取表格數據。
- 將它合併到一個主表中。
This is the URL (http:...fantasysports.yahoo.com...pos=QB)該腳本當前提取。列是各個職位的團隊名稱和團隊等級。
我想讓它遍歷其他位置(即WR,RB,TE)。這是通過將URL的最後2個字母更改爲相應的值來完成的。然後我希望將所有這些數據放在一個數組中,其中第一列是團隊名稱,第二列是針對QB位置的排名#,第三列是針對WR位置的排名#,依此類推。
我的計劃是將GM_xmlhttpRequest
放在一個for
循環中,該循環遍歷不同的位置名稱。
我的代碼能夠爲職位顯示單獨的表格,但由於某些原因,他們沒有按順序排列。
我遇到的另一個問題是變量範圍。目前newStatTable
在parseResponse
函數中定義,但我無法從函數外部訪問它。我試圖從var newStatTable
刪除var
,使其成爲一個全球性的,但它沒有奏效。
這裏是我要尋找一個樣本輸出數組(注:我只是隨機選擇這些隊伍):
TeamName QB WR TE and so on...
--- -- -- --
Jacksonville Jaguars 1 6 28
Sanfrancisco 49ers 4 2 32
Seattle Seahawks 31 5 10
這裏是我的嘗試。
// ==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);
}
所以你的問題是 - **他們不是爲了**,對嗎? - 我假設你知道httpRequests是異步的,那麼你爲什麼期望它們是有序的呢? – Soren
當我說「不按順序」時,我的意思是WR數組顯示在QB數組之前。在for循環中,它不是爲了順序嗎? 此外,我更大的問題是,我試圖找到一種方法來結合所有的表,就像在我的示例數組輸出 – Bijan
您的for循環順序,但「GM_xmlhttpRequest」創建一個異步AJAX調用,並完成這些發生在for循環完成之後*,並且它們按您所調用的HTTP服務器決定完成它們的順序完成 - 即以任意順序完成。 – Soren