本文介绍了如何加载一个表,从另一个网页,到一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的Greasemonkey / Tampermonkey脚本,使一些统计数据到一个数组。

I am creating a Greasemonkey/Tampermonkey script that puts some stats into an array.

使用JavaScript,我怎么会做这么一个页面加载一个URL(的),并创建与前两列数组(等级团队)?

Using JavaScript, how would I make it so a page loads a URL (football.fantasysports.yahoo.com/f1/326198/pointsagainst?pos=QB) in the background and creates an array with the first two columns (Rank and Team)?

我有在做这一切的背景问题,我$我将使用AJAX p $ psume。任何帮助将是AP preciated。

The problem I am having is doing all of this in the background, I presume I would be using AJAX. Any help would be appreciated.

推荐答案

有关的静态的页(如中的一个),使用 GM_xmlhtt prequest()的DOMParser 来提取你想要的元素。见下文。

For a static page (like the one you linked), use GM_xmlhttpRequest() and DOMParser to extract the elements you want. See below.

对于动态的页 ,使用的技术从How以获得一个AJAX获取请求等待页面返回响应之前要呈现

For a dynamic page, 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     http://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);
}

这篇关于如何加载一个表,从另一个网页,到一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 17:36