Jump to content

Userscript that makes a GET request

Acorn Eyes

I'm trying to request data from the site I'm making a script for, this is the JSON for it. 

 

I need to retrieve just the Game State, and the rolls.roll

 

I'm absolutely lost on how to do this, I've tried numerous methods I found on stack overflow. Jquery just flat out doesn't work. 

 

How would I go about this?

 

 

 

Don't talk about stale memes.

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, Mr_KoKa said:

Do you JSON.parse it? Or you just tried to parse it yourself.

I can't even GET it in the first place.

Don't talk about stale memes.

Link to comment
Share on other sites

Link to post
Share on other sites

Oh, ok use $.ajax like this:

 

$.ajax({
  url: '/some/path/to/file.json',
  dataType: 'json',
  timeout: 5000, //5 sec timeout
  success: function(jsonResponse){
    jsonResponse.gameStatus //
    jsonResponse.rolls // your rolls array, it has .length attribute and you can access each roll by index eh. jsonResponse.rolls[0].roll
  },
  error: function(jqXHR, textStatus, errorThrown){
    //What if error has occured?
  }
}

 

I have nothing to test it with, so there may be some syntax errors.

 

You may get error when ajax request is in progress and you click a link on a page, so if you (re?)load page during ajax request it will return some kind of error, it used to be empty error, I don't if anything changed.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Mr_KoKa said:

Oh, ok use $.ajax like this:

 


$.ajax({
  url: '/some/path/to/file.json',
  dataType: 'json',
  timeout: 5000, //5 sec timeout
  success: function(jsonResponse){
    jsonResponse.gameStatus //
    jsonResponse.rolls // your rolls array, it has .length attribute and you can access each roll by index eh. jsonResponse.rolls[0].roll
  },
  error: function(jqXHR, textStatus, errorThrown){
    //What if error has occured?
  }
}

 

I have nothing to test it with, so there may be some syntax errors.

 

You may get error when ajax request is in progress and you click a link on a page, so if you (re?)load page during ajax request it will return some kind of error, it used to be empty error, I don't if anything changed.

jQuery is super fucky on the site.

.Ajax and .post/get are not recognized.

Don't talk about stale memes.

Link to comment
Share on other sites

Link to post
Share on other sites

Then you can use raw XMLHttpRequest:

 

var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', true); //True is for async request
req.onreadystatechange = function (aEvt) {
  if (req.readyState == 4) {
    if(req.status == 200) {
      var json = req.responseText;
      var obj = JSON.parse(json);
    
      obj.gameSatus //
      obj.rolls //
    } else {
      console.log("Error has occurred\n");
    }
  }
};
req.send(null);

I  got the example from https://developer.mozilla.org/pl/docs/XMLHttpRequest you can read there more.

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, Mr_KoKa said:

Then you can use raw XMLHttpRequest:

 


var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', true); //True is for async request
req.onreadystatechange = function (aEvt) {
  if (req.readyState == 4) {
    if(req.status == 200) {
      var json = req.responseText;
      var obj = JSON.parse(json);
    
      obj.gameSatus //
      obj.rolls //
    } else {
      console.log("Error has occurred\n");
    }
  }
};
req.send(null);

I  got the example from https://developer.mozilla.org/pl/docs/XMLHttpRequest you can read there more.

Ah fuck, you're the man.

Just an FYI, it would have been a lot easier on me if you included console.log(obj.gameState) and console.log(obj.rolls[x].roll)

 

Actually I might need some help.

 

Is there a better way to add each roll to an array than to use a for loop?

Don't talk about stale memes.

Link to comment
Share on other sites

Link to post
Share on other sites

Loop is fine.

var rolls = [];
for(var i = 0; i < obj.rolls.length; i++){
  rolls.push(obj.rolls[i].roll);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Mr_KoKa said:

Loop is fine.


var rolls = [];
for(var i = 0; i < obj.rolls.length; i++){
  rolls.push(obj.rolls[i].roll);
}

 

Weird, I just tried that method and it gives me incorrect values.

This is the one I wrote.

var rolls = [];
for(i=0; i < 9; i++) {
 rolls[i] = obj.rolls[i].roll;
}

 

Don't talk about stale memes.

Link to comment
Share on other sites

Link to post
Share on other sites

If rolls array will always have 9 elements then result will be the same for both codes.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Mr_KoKa said:

If rolls array will always have 9 elements then result will be the same for both codes.

I think rolls.push is what's giving me different results.

 

I also just noticed that sending a request through a client gives me a JSON output with far fewer rolls than what the console is giving me.

 

And the 10th roll is not the same between the two. But that's because the client version has the incorrect value.

 

By client I mean API Debugger Client

Don't talk about stale memes.

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know. I don't see what you see. There may be some data client is sending to server to receive different json response, fewer rolls or different batch, idk. You can try look at network tab in your browser dev tools.

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, Mr_KoKa said:

I don't know. I don't see what you see. There may be some data client is sending to server to receive different json response, fewer rolls or different batch, idk. You can try look at network tab in your browser dev tools.

Its not an issue if it works.

 

 

Don't talk about stale memes.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×