1

I'm trying to get Greasemonkey to automate a site for me, and I don't want to extract the info one at a time. So I had a look around and discovered jQuery. Being very new to Greasemonkey scripts, I still find it a bit tough, but if someone could point me in the right direction it would help a lot. I want Greasemonkey to extract information in files that I presume are linked to jQuery. When I run Firebug on the page I get:

http://www.trada.net/javascript/jquery-1.4.2.min.js
http://www.trada.net/REST_Service/REST_Auction.svc/GetAuctionData?_=1306009003654

with this info inside:

{
    "d": [
        [ "", "", "y", "ZAR", "1", "49517", "8270, 8270, 8270, 7635, 8270", null,
          "1.34", "8270", "0:13:30", "", "12", "", "C", "30", null ],
        [ "", "", "y", "ZAR", "2", "49582", "6725, 6725, 7863, 9228", null,
          "***0.78***", "6725", "0:12:37", "", "5", "", "L", null, null ],
        [ "", "", "y", "ZAR", "3", "49058", "5153, 9216, 6058, 9216, 5153", null,
          "180.80", "5153", "0:00:59", "", "1100", "", "T", null, null ],
        [ "", "", "y", "ZAR", "4", "49581", "0051, 6692, 9555, 6692, 9555", null,
          "1.35", "0051", "0:00:14", "", "12", "", "T", null, null ],
        [ "", "", "y", "ZAR", "5", "49584", "6725, 6725, 9822", null,
          "0.93", "6725", "0:14:28", "", "5", "", "L", null, null ],
        [ "", "", "y", "ZAR", "6", "49583", "9822, 7863, 9228", null,
          "0.75", "9822", "0:15:05", "", "5", "", "L", null, null ],
        [ "", "", "y", "ZAR", "7", "49544", "0957, 0957, 0957, 0957, 0957", null,
          "10.00", "0957", "0:01:59", "", "55", "", "T", null, null ],
        [ "", "", "y", "ZAR", "8", "49575", "2110, 5661, 9295, 2110, 3809", null,
          "3.05", "2110", "0:00:13", "", "29", "", "T", null, null ],
        [ "", "", "y", "ZAR", "9", "49496", "7863, 5845, 7863, 7158, 7158", null,
          "2.41", "7863", "0:05:55", "", "10", "", "B", null, null ],
        [ "", "", "y", "ZAR", "10", "49524", "7863, 7863, 5845, 7863, 0764", null,
          "1.57", "7863", "0:05:49", "", "5", "", "B", null, null ],
        [ "", "", "y", "ZAR", "11", "49539", "7863, 7863, 0764, 2427, 2427", null,
          "1.92", "7863", "0:03:54", "", "10", "", "B", null, null ]
    ]
}

I can make sense of the info, and it is renewed every second, but how do I get Greasemonkey to interpret the info? Say for example I want each piece of information on line 4 to be read into its own variable:

"1.34", "8270", "0:13:30", "", "12", "", "C", "30", null

How do I get Greasemonkey to extract that info?

Thanks a lot.

Ludwig
  • 151
  • 5
  • 20
  • ok, this will help me a lot. just 1 more small obstacle... fire bug's console shows me the files, but firebug isnt running always, where do i tell greasemonkey to look for the data? – Ludwig May 22 '11 at 04:16
  • was this comment supposed to be for my answer or that "no.good" chap's? – Brock Adams May 22 '11 at 07:02

2 Answers2

4

That JSON contains a 2-D array, so think of accessing the data that way. For example, the given JSON returns d as an 11 by 17 array.

The rows are like:

["", "", "y", "ZAR", "1", "49517", "6458, 8270, 8270, 8270, 7635", null, "1.40", "6458", "0:13:30", "", "12", "", "C", "30", null]

I assume you can figure out what the columns are (not enough info provided for us to).

So, here's a complete GM script that lists all of the 7th columns to Firebug's console...

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

var myJson              = '{"d":[["","","y","ZAR","1","49517","6458, 8270, 8270, 8270, 7635",null,"1.40","6458","0:13:30","","12","","C","30",null],["y","-00:00","y","ZAR","2","49593","6458, 6458, 6458, 6458, 6458",null,"2.92","6458","0:13:37","","12","","L","12","Ve4mYdrvkkQMKxBH1\/1VMtDTCDQBRspg5jB8jjY08zg="],["","","y","ZAR","3","49058","7456, 9216, 6458, 5153, 7456",null,"194.40","7456","0:00:31","","1100","","T",null,null],["","","y","ZAR","4","49597","2935, 6554",null,"1.22","2935","0:01:16","","12","","T",null,null],["","","y","ZAR","5","49590","4440, 0518, 5343, 2625, 4848",null,"0.95","4440","0:15:58","","5","","L",null,null],["","","y","ZAR","6","49591","4848, 4440, 4440, 0518, 2625",null,"1.81","4848","0:16:05","","12","","L",null,null],["","","y","ZAR","7","49595","6458",null,"5.55","6458","0:04:13","","55","","T",null,null],["","","y","ZAR","8","49596","",null,"2.90","NONE","0:04:35","","29","","T",null,null],["","","y","ZAR","9","49496","6458, 2427, 2427, 7863, 5845",null,"2.56","6458","0:06:07","","10","","B",null,null],["","","y","ZAR","10","49524","6458, 2427, 7863, 7863, 5845",null,"1.67","6458","0:06:00","","5","","B",null,null],["","","y","ZAR","11","49539","6458, 2427, 7863, 7863, 0764",null,"2.02","6458","0:04:25","","10","","B",null,null]]}'
var jsonObj             = $.parseJSON (myJson);

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

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

        //--- Print the 7th column.
        console.log ('Row: ' + (parseInt (rowIndex) + 1) + ' Column: 7  Value: ' + singleAuctionData[6]);
    }
);
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • @ brock adams, thanks for your input. I'm still very new at this, but will play around with it for a few days, and if i still dont get it to do what i want to, ill come back to you. I've still got the small obstacle to get GM to find the data. firbug gives it to me in the console log, but it isnt running always. how do i instruct GM to go and find the data in http://www.trada.net/REST_Service/REST_Auction.svc/GetAuctionData?_=1306009003654 which is updated with a new number every second? – Ludwig May 22 '11 at 20:59
  • @ Brock Adams, concerning the question to get gm to find the data, i take it will change in your line `var myJson = '{"d":[["","","y","ZAR","1","49517",.......`, then also concerning your script, i understand what it should do, but `// @require http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js`, shouldnt it be `http://www.trada.net/javascript/jquery-1.4.2.min.js` ?, cause that is what the web page refers to in the fire bug console. ` – Ludwig May 22 '11 at 21:07
  • @Ludwig, Using Google's copy of jQuery is convenient and more reliable and will work for ALL scripts. *Trada.net* might have even adulterated their copy. Version 1.5.1 is a good choice, although later versions should work too.   There is no conflict between GM's copy of jQuery and the page's. ... ... As for your script getting the data, that can be quite complicated (IE, **a separate question**). It would be best to link to the exact page and say what you plan to do. But, in general, you can find and overwrite or attach to the ajax requests, or you can monitor the page for changes. – Brock Adams May 22 '11 at 22:53
  • @ Brock. your 2d array... it is a text/string array, right? then ive just got another question concerning this array, how do i pull differnt segmants at a time? for example, if i want to assign a new text variable the value of "0.78" which i find in the array in 2nd block value 12. or do i need to create a question again? By the way, your code works perfect, but i still can figure out how, ... gues i need more practice on json and jquery. – Ludwig May 24 '11 at 20:01
  • @Ludwig, Yes, opening another question is best; it's difficult to resolve any but simple issues by using comments. Anyway, "my" array is not a string, I merely started with the sample JSON (which is a string representation of JS structures). The string is gone/irrelevant once the `parseJSON` call is made. ... by "`2nd block value 12`", do you mean the 13th column of the second row in the array? You'd get that with `arrayOfAuctions[1][12]`. Remember, you are dealing with 11 auctions at a time, so you probably want to loop through them. In the loop, that value is `singleAuctionData[12]`. – Brock Adams May 24 '11 at 22:05
  • PS, edited the answer to reflect information gleaned from later questions. – Brock Adams May 24 '11 at 22:06
  • @ Brock. oh yeah, found my mistiake. forgot it started to count @ 0. thanks for this tip. btw, is there any way to get an debugger for GM? guesss the sandbox makes it difficult. I'm trying to put alert s with info to keep track where in the script i am, but most of my alerts dont show. should i create an specific window for them? like you did for the output of the json array? – Ludwig May 24 '11 at 22:14
  • @Ludwig, Just use `console.log()` instead of `alert()`. ... There is no GM debugger but there are strategies. Search StackOverflow; this question has come up before. (Or open a new question, if your search ensures you it won't be closed as a duplicate.) – Brock Adams May 24 '11 at 22:57
  • @ Brock Thanks for all your help. I'm still struggling with debugging, especially showing the value of one of arrays. lets keep to `arrayOfAuctions[1][12]` which i want to point to the value "0.78" `console.log(arrayOfAuctions[1][12])`, but cant find anything in the firbug console on debug. ive tried `alert(arrayOfAuctions[1][12])`... no result, triedit in unsafewindow... nothing.changed from runnng it in the function to global vars... nothing. must i put it as a new question? ive tried [link](http://stackoverflow.com/questions/3490062) – Ludwig May 26 '11 at 19:56
  • @Ludwig, "0.78" is at `arrayOfAuctions[1][11]`. Both that `console.log` and that `alert` should work just fine but show "6725" for the sample data of this question. ... Please open a new question. Give: (a) what you've got, (b) what you want, (c) an exact method for seeing or replicating any errors. – Brock Adams May 27 '11 at 00:04
  • @ brock .Thanls for all your help concerning this issue. it took me a while to realise Java / Gm is case sensitive, unlike pascal that i'm used too...:) sot "Alert('I')" and "alert('I') is not the same thing. the same with console log, and the rest of vars etc. starting to get the hang of it now. thanks again:) – Ludwig Jun 09 '11 at 18:44
1

What you're looking at is JSON - JavaScript Object Notation - it's a lightweight data-interchange format with the very neat advantage that it's a subset of JavaScript so a JSON string (once parsed) is usable as a JavaScript object.

jQuery has nothing to do with JSON strings (except that it can generate and parse them) - it is a JavaScript library that makes it easier to do a lot of things but nothing that you couldn't do in plain JavaScript.

  1. To work with JSON, you'll need a JSON parser to convert the JSON string representation into an object.

    a. If you're using jQuery, you can check out the parseJSON() function. Alternatively, it might make more sense to use the getJSON() when getting the data - this way jQuery will automatically fetch and parse the JSON response. As noted on the documentation page, this is really a convenience method for ajax() with the appropriate parameters.

    b. On the other hand, if you're not using jQuery (and jQuery being included in the website's page doesn't count), you could make use of the json-2 library for JavaScript.

    c. You could rely on a browser's native JSON handling capabilities (not recommended)

    d. As RobG notes in the comment below, there's always eval(). You might want to read this answer and then this answer to see why and why not "using eval() is a bad idea".

  2. For the structure you've posted, if you format the object literal, you'll see that it's an object with a property named d which is an array. The contents of that array are arrays themselves. So, once the JSON string has been parsed into an object,you could do this:

    var data = parseJSON(jsonString); //get a JavaScript objet
    for(var i = 0; i < data.d.length; i++){
        var currEntry = data.d[i];
        for(var j = 0; j < currEntry.length; j++){
            var currVal = currEntry[j];
            console.log(currVal);
        }
    }
    

    If you're using jQuery, you could use $.each()

    var data = parseJSON(jsonString);
    $.each(data.d, function(index, currEntry){
        $.each(currEntry, function(index, currVal){
            console.log(currVal);
        }
    });
    

    In each iteration of the outer loop, currEntry will give you a reference to an array inside d. You can then iterate over that array to get the values for each line. If you already know which 'line' you want, you could also index into it directly (beware of non-existent indices though). data.d[4][10] will give you "ZAR".

Community
  • 1
  • 1
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
  • While it's convenient to use various functions for converting JSON to an object, it isn't necessary. JSON can be converted to a javascript object simply by: `var data = eval('(' + string + ')');` which will work in every script-capable browser in use. – RobG May 22 '11 at 04:07
  • @RobG Well, this is embarrassing :/ You're right, of course. Adding it to the answer. – no.good.at.coding May 22 '11 at 05:06