使用的Greasemonkey和jQuery从页面截取JSON / AJAX的数据,并对其进行处理 [英] Using Greasemonkey and jQuery to intercept JSON/AJAX data from a page, and process it

查看:316
本文介绍了使用的Greasemonkey和jQuery从页面截取JSON / AJAX的数据,并对其进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

右,我也稍微得到了我的<一个答案href="http://stackoverflow.com/questions/6084293/getting-greasemonkey-to-inter$p$pt-jquery">$p$pvious问题。但是,我仍然告诉通用汽车的问题上哪儿去获取数据放入数组中的坐...

Right, I partly got an answer in my previous question. But, I still sit with the problem of telling GM where to go and fetch the data to put into the array...

在网页 http://www.trada.net/p_home.aspx ,如果我跑萤火控制台,我得到上述问题的数据,但它是不断变化的,更新每一秒。

On the web page http://www.trada.net/p_home.aspx, if I run firebug console, I get the data from the above mentioned question, but it is ever changing, updating every second.

这个数据我有一个想法如何把它放在一个数组,并从那里我会告诉GM做什么用的。我不能运行Firebug控制台的全部时间,我不知道如何让通用汽车公司获取由网站发送的数据的请求,这看起来像:的 http://www.trada.net/REST_Service/REST_Auction.svc/GetAuctionData?_=1306009003654 - 其中最后一部分每次更新变化

This data I've got an idea how to put it in an array, and from there I'll tell GM what to do with it. I cant run the firebug console the whole time, and I dont know how to get GM to fetch the data requests sent by the site, which look like : http://www.trada.net/REST_Service/REST_Auction.svc/GetAuctionData?_=1306009003654 -- where the last part changes with every update.

基本上通用汽车将获取数据的每一秒,看看是否有必要竞投任何拍卖中,然后如果有1赢了,单击会弹出,才能继续。

Basically Gm will fetch the data every second, see if there is a need to bid on any of the auctions, and then if any 1 is won, click a popup that appears, in order to continue.

推荐答案

由于目标网页使用jQuery的,您可以在JSON数据窃听轻松使用的ajaxSuccess()

Since the target page uses jQuery, you can eavesdrop on the JSON data easily using ajaxSuccess().

这个问题就变成了从页面的范围,数据获取到GM沙箱......这可以通过将数据放入一个特殊的页面节点完成。

The problem then becomes getting the data from the page's scope to the GM sandbox... That can be done by putting the data into a special page node.

从那里,它是使用<公正问题href="http://stackoverflow.com/questions/6084293/getting-greasemonkey-to-inter$p$pt-jquery/6084578#6084578">my其他(辉煌:D)。答案

全部放在一起,下面应该让你开始:

Putting it all together, the following should get you started.:

更新后的近4岁:的code以下,现在已经过时,由于许多变化在Firefox和Greasemonkey的。我不想重新设计它由于缺乏兴趣,也因为它并不适合大多数RL任务最好的办法计划。在大多数情况下,最强大,携带方便,可靠的方法仍然是智能轮询。请参见一个方便的工具为的例子。

Update after almost 4 years: The code below is now obsolete due to many changes in Firefox and Greasemonkey. I don't plan on re-engineering it due to lack of interest and also because it's not the best approach for most RL tasks. For most cases; the most robust, portable, and reliable method remains smart polling. See an example of a handy utility for that.

// ==UserScript==
// @name            _Fun with JSON
// @include         http://www.trada.net/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==


//--- Create a cell for transmitting the date from page scope to GM scope.
$('body'). prepend ('<div id="LatestJSON_Data"></div>');

var J_DataCell          = $('#LatestJSON_Data');


//--- Evesdrop on the page's AJAX calls and paste the data into our special div.
unsafeWindow.$('body').ajaxSuccess (
    function (event, requestData)
    {
        J_DataCell.text (requestData.responseText);
    }
);


//--- Listen for changes to the special div and parse the data.
J_DataCell.bind ('DOMSubtreeModified', ParseJSON_Data);


function ParseJSON_Data ()
{
    //--- Get the latest data from the special cell and parse it.
    var myJson              = J_DataCell.text ();
    var jsonObj             = $.parseJSON (myJson);

    //--- The JSON should return a 2-D array, named "d".
    var AuctionDataArray    = jsonObj.d;

    //--- Loop over each row in the array.
    $.each (
        AuctionDataArray,
        function (rowIndex, singleAuctionData) {

            //--- Print the 7th column.
            console.log ('Row: ' + (parseInt (rowIndex) + 1) + ' Column: 7  Value: ' + singleAuctionData[6]);
        }
    );
}


//--- Format our special cell with CSS.  Add "visibility: hidden;" or "display: none;", if desired.
GM_addStyle ( (<><![CDATA[
    #LatestJSON_Data
    {
        background:         gold;
        border:             3px ridge #0000DD;
        font-size:          10px;
        margin:             0 2em;
        padding:            1ex 1em;
        width:              94%;
        opacity:            0.8;
        overflow:           hidden;
        z-index:            666;
        position:           absolute;
        color:              black;
    }
]]></>).toString () );


需要注意的是这个问题的建议可能会违反网站的服务条款的和/或网站可以采取的对策。 (他们已经混淆的JS。)


Note that the question's proposal may violate the site's Terms of Service and/or the site can take countermeasures. (They already obfuscate their JS.)

这篇关于使用的Greasemonkey和jQuery从页面截取JSON / AJAX的数据,并对其进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆