1

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...

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.

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.

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.

Community
  • 1
  • 1
Ludwig
  • 151
  • 5
  • 20

1 Answers1

1

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

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.

From there, it's just a matter of using my other (brilliant :D ) answer.

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

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 () );


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.)

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • @ Brock. thanks. u make it look so easy. it would hav taken me at least 2 weeks to figure this out on myself. do you hav any suggestions for pdf courses i can get on Gm java script tutorials. the beginners guides i quite get the hang of it, but this is getting a bit tricky. I last did programming on school in Turbo pascal, it fasinated me then, but i decided do do farming, so it went a bit out of date in the last 16 years.:P thanks again. from here i will or should get my bearings. – Ludwig May 23 '11 at 16:37
  • For tutorials, you need a good understanding of JavaScript, see [What's the best JS tutorial](http://stackoverflow.com/questions/646032/whats-the-best-javascript-tutorial/646039#646039). And, a good understanding of [the DOM](https://developer.mozilla.org/en/Gecko_DOM_Reference). Then for Greasemonkey, most of what you need to know is: [GM API Reference](http://wiki.greasespot.net/Greasemonkey_Manual:API) and [GM Getting Started](http://commons.oreilly.com/wiki/index.php/Greasemonkey_Hacks/Getting_Started) -- especially the Pitfalls section. – Brock Adams May 23 '11 at 18:40
  • Also, [learn jQuery](http://stackoverflow.com/questions/5179174/jquery-tutorial-and-trick-web-sites/5179294#5179294) from the start. It's the easiest, most-robust way to manipulate pages with JS. – Brock Adams May 23 '11 at 18:59
  • @ Brock. @function ParseJSON_Data; sub function function (rowIndex, singleAuctionData)...after the console log, can i take singleAuctionData[] with whatever part of the array i want and assign it with parseInt to integer variables, with which i do calculations or if statements, like i tried...but failed in [link](http://stackoverflow.com/questions/6180536) – Ludwig May 31 '11 at 20:29
  • @Ludwig: Yes, you can do that in principle. I haven't looked at your other question in detail yet and can only spare a few minutes at a time right now. I'll give it a good look-see in a day or so -- if someone else hasn't beat me to it. – Brock Adams May 31 '11 at 23:03
  • @brock: this script, why doesnt it work in chrome? is there anything i need to modify to let it work in chrome? some of the other scripts does work in it except this one. What would the reason be?...thought that both browsers make use of he same javascripting...? thanks. – Ludwig Jun 18 '11 at 04:10
  • @Ludwig: Chrome native userscripts do not support all Greasemonkey functionality. Install [Tampermonkey](https://chrome.google.com/webstore/detail/dhdgffkkebhmkfjojejmpbldmpobfkfo) and more GM scripts will work. This script will probably work under Tampermonkey, but I don't support Chrome. – Brock Adams Jun 18 '11 at 07:26
  • @ brock: ok thanks. installed itand tried it. Yes, it works partially, eg, it loads your frame with the info... but i'm un-able to click on any buttons..:(, either manually or via script. every time i click, it reloads the page. might have to take it up on tampermonkey forum. thanks in anycase. – Ludwig Jun 18 '11 at 17:00
  • Inside a userscript, an overwrite of ajaxSuccess would just be inside the sandbox, yes? In other words, assuming you don't alter the DOM in ajaxSuccess, the website has no way of knowing if a userscript is listening to AJAX requests? Assigning requestData.responseText to a variable in the userscript doesn't seem to be accessible by the page's console, and the toString of $('body').ajaxSuccess when run in the pages console doesn't return the function contents set by a userscript. – Edge Mar 05 '15 at 02:16
  • @Edge: No, for it to work at all, the overwrite has to be in the page scope and the page could theoretically detect it. Also, note that GM and Firefox have changed radically since this answer was posted. This code is now obsolete. – Brock Adams Mar 05 '15 at 02:31
  • http://pastebin.com/B59HnAt9 This code seems to work.Now, the page could detect this I assume by either compromising the eval function or by sniffing out unusual page variables. Is there a way to stop the page from detecting this form of request listening using another method? I'd imagine they're more likely to look for DOM modifications. You also mention the code being obsolete - is there a better method now? – Edge Mar 05 '15 at 08:42
  • @Edge, I've seen a lot of pages look for javascript modifications and/or extensions such as Firebug. I have not seen a *real* webpage check for DOM modifications yet. As for stopping a page from detecting your AJAX intercept, it's seldom necessary, and not always hard. You just have to analyze the page's JS and disable or replace it if necessary. These are all topics for other questions -- many of which have been already answered here on SO. – Brock Adams Mar 05 '15 at 08:51