Discuss Scratch

LankyBox01
Scratcher
1000+ posts

Get Cloud Data Value using Javascript

I need to get the value of a certain cloud data variable using javascript. How do i do this?
Chiroyce
Scratcher
1000+ posts

Get Cloud Data Value using Javascript

Certain value? Well you need the sessionID, then do a handshake with wss://clouddata.scratch.mit.edu with the sessionID, the data is

 {"method":"handshake","user":"<username>","project_id":"<project_ID>"} 

Here is an example.
 {"method":"handshake","user":"Chiroyce","project_id":"488029945"} 

Then the response will be like this
 {"method":"set","project_id":"<project_id>","name":"☁ <var_name>","value":"<value>"} 

Here is an example.
 {"method":"set","project_id":"488029945","name":"☁ Cloud","value":"000"} 

You might get many responses, depending upon the number of Cloud Vars.

Last edited by Chiroyce (May 1, 2021 12:16:06)

LankyBox01
Scratcher
1000+ posts

Get Cloud Data Value using Javascript

Chiroyce wrote:

Certain value? Well you need the sessionID, then do a handshake with wss://clouddata.scratch.mit.edu with the sessionID, the data is

 {"method":"handshake","user":"<username>","project_id":"<project_ID>"} 

Here is an example.
 {"method":"handshake","user":"Chiroyce","project_id":"488029945"} 

Then the response will be like this
 {"method":"set","project_id":"<project_id>","name":"☁ <var_name>","value":"<value>"} 

Here is an example.
 {"method":"set","project_id":"488029945","name":"☁ Cloud","value":"000"} 

You might get many responses, depending upon the number of Cloud Vars.
What?
Explain.
I don't understand.

I'm new to js
9gr
Scratcher
1000+ posts

Get Cloud Data Value using Javascript

if you dont want to use websockets, use

GET https://mv-ezproxy-com.ezproxyberklee.flo.org/logs?projectid=<PROJECT ID>&offset=0&limit=<LOG LIMIT>

var projectid = 0; // replace 0 with your projectid
fetch(`https://clouddata.scratch.mit.edu/logs?projectid=${projectid}&offset=0&limit=40`)
.then(res => res.json())
.then(res => {
  console.log(res[0]) // should log the latest cloud variable commit in the console
})

Last edited by 9gr (May 1, 2021 12:33:39)

LankyBox01
Scratcher
1000+ posts

Get Cloud Data Value using Javascript

9gr wrote:

if you dont want to use websockets, use

GET https://mv-ezproxy-com.ezproxyberklee.flo.org/logs?projectid=<PROJECT ID>&offset=0&limit=<LOG LIMIT>

-snip-
Thanks, it works perfectly.
But how do i get the values from the returned string? Like “user” or “name” or “value”.

Edit: Nevermind, i just add .user at the end of res.

Last edited by LankyBox01 (May 1, 2021 12:45:40)

Chiroyce
Scratcher
1000+ posts

Get Cloud Data Value using Javascript

LankyBox01 wrote:

What?
Explain.
I don't understand.

I'm new to js
basically it gets the latest value of all cloud variables, but @9gr's only gets the last variable that was set.
For example there are 2 cloud vars, “a” and “b”
A is set to 99
b is set to 33

if you run 9gr's code, you'll only get b's value, and not a as it was set before b. But if you use the websockets way, then you will get the values of a and b.

const socket = new WebSocket("wss://clouddata.scratch.mit.edu/")
socket.addEventListener("open", () => {
  socket.send(`${JSON.stringify({name: "handshake", user: Chiroyce, project_id: 493371185})}\n`)
})
Replace “Chiroyce” with your username, replace “493371185” with your project id, this is better but it'll only show it in the console. I got the code from @uwv in this forum post.

Last edited by Chiroyce (May 1, 2021 12:51:57)

LankyBox01
Scratcher
1000+ posts

Get Cloud Data Value using Javascript

Chiroyce wrote:

LankyBox01 wrote:

What?
Explain.
I don't understand.

I'm new to js
basically it gets the latest value of all cloud variables, but @9gr's only gets the last variable that was set.
For example there are 2 cloud vars, “a” and “b”
A is set to 99
b is set to 33

if you run 9gr's code, you'll only get b's value, and not a as it was set before b. But if you use the websockets way, then you will get the values of a and b.

const socket = new WebSocket("wss://clouddata.scratch.mit.edu/")
socket.addEventListener("open", () => {
  socket.send(`${JSON.stringify({name: "handshake", user: Chiroyce, project_id: 493371185})}\n`)
})
Replace “Chiroyce” with your username, replace “493371185” with your project id, this is better but it'll only show it in the console. I got the code from @uwv in this forum post.
My username is not defined. But i only have one cloud variable in the project so 9gr's code is fine. Thanks
Harakou
Scratcher
1000+ posts

Get Cloud Data Value using Javascript

Closed by request of owner.

Powered by DjangoBB