new-scratch3-api v3.0.4
new-scratch3-api
A fixed version of ErrorGamer2000's scratch3-api (that is maintained)
A remake of trumank's scratch-api optimized for scratch 3.0. It not only adds to the api, but is also composed mostly of promises to prevent callback hell. So, instead of
const Scratch = require("scratch-api");
Scratch.UserSession.create("<username>", "<password>", function (err, user) {
user.cloudSession("<project>", function (err, cloud) {
cloud.on("set", function (name, value) {
console.log(`${name} was set to ${value}`);
});
});
});
you can do
const Scratch = require("scratch3-api");
async function main() {
let session = await Scratch.UserSession.create("<username>", "<password>");
let cloud = await session.cloudSession("<project>");
cloud.on("set", function (name, value) {
console.log(`${name} was set to ${value}`);
});
}
main();
which is a lot easier to read. If, for some reason, you like callback hell, you can just use .then()
.
const Scratch = require("scratch3-api");
Scratch.UserSession.create("<username>", "<password>").then(function (session) {
session.cloudSession("<project>").then(function (cloud) {
cloud.on("set", function (name, value) {
console.log(`${name} was set to ${value}`);
});
});
});
Installation
Install with npm:
npm install scratch3-api
Or by cloning this repository:
git clone https://github.com/ErrorGamer2000/scratch3-api.git
API Documentation
Scratch
UserSession
Events
None.
async static create(username, password)
Creates and loads a new UserSession
with the given username
and password
.
username
- The Scratch account username (not case sensitive). Optional. If not provided user will be prompted.password
- The Scratch account password. Optional. If not provided user will be prompted.returns
a loadedScratch.Usersession
.
Note that
//Async
let session = await Scratch.UserSession.create("<username", "<password>");
is exactly the same as
//Async
let session = new Scratch.UserSession();
await session.load("<username", "<password>");
constructor()
Creates a new, unloaded, Scratch.Usersession
.
returns
an unloadedScratch.Usersession
.
async load(username, password)
Loads a UserSession
with the given username
and password
.
username
- The Scratch account username (not case sensitive). Optional. If not provided user will be prompted.password
- The Scratch account password. Optional. If not provided user will be prompted.returns
none.
async prompt()
(Deprecated)
Prompts the user for their username and password.
returns
an unloadedScratch.Usersession
.
This feature will lose support soon, instead use .load
with no parameters.
async verify()
Verifies the current session.
returns
true
offalse
async comments(options)
Adds a comment on the specified project, user, or studio.
options
- An object containing the comments options.project
,user
, orstudio
- The project, user, or studio to comment on. User must be a username and the others must be ids.parent
- The comment id to reply to. Optional.replyto
- The user id to address (@username ...). Optional.content
- The text of the comment to post.
returns
anhttps
request.
projects
- async
get(id)
Gets a project with the given id.id
- The id of the project to get.returns
Project
. (seeOther APIs
)
- async
getUserProjects(limit)
Gets up to 40 projects of the currently logged in user.limit
- A number from 1 to 40. The maximum amount of the user's projects to get.returns
anArray
ofProject
s.
async cloudSession(project)
Creates and connects a new CloudSession
with the UserSession
.
project
- The id of the project to connect to. Must be valid.returns
a connectedCloudSession
CloudSession
extends
EventEmitter
Events
reset
- TheWebSocket
connection was reset.set
- A cloud variable in the project was set.
static create(usersession, project)
Creates and connects a new CloudSession
with the given UserSession
.
usersession
- Required. TheUserSession
to create theCloudSession
with.project
- The id of the project to connect to. Must be valid.returns
a connectedCloudSession
.
constructor(usersession, project)
Creates a new, unconnected CloudSession
with the given UserSession
.
usersession
- Required. TheUserSession
to create theCloudSession
with.project
- The id of the project to connect to. Must be valid.returns
an unconnectedCloudSession
.
connect()
Connects an unconnected CloudSession
.
end()
Ends the current connection.
get(variable)
Gets the value of a cloud variable.
variable
- The name of the variable to get (including the☁
, seename
).returns
aString
containing the value of the variable.
set(variable, value)
Sets the value of a cloud variable.
variable
- The name of the variable to get (including the☁
, seename
).value
- The value to set the variable to. Can only be a number or a string that is a valid number.
numerify(string)
Turns the string input into a number, allowing you to send any value across the cloud variables. The compatible scratch engine is here
string
- The string to convert to an number.returns
aNumber
representing the numerified string.
stringify(number)
Turns the encoded number input and converts it back to a string.
number
- The number to convert back to a string.returns
aString
matching the origionally encoded string.
name(variable)
Automatically adds the ☁
to the variable name.
name
- The name of the variable (without☁
)returns
☁
+variable
.
Other APIs
Project
The Project
class reformats a response from Scratch's project api into an easy-to-use object.
Each Project
instance will be formatted like this:
stage
- ASprite
instance describing the project's stage.variables
- An object containing all of the variables in the project. To retreive the names of these variables, useObject.keys
.id
- AString
containing the variable's id. This is not really of much use unless you are working with Scratch Blocks.spriteOnly
- ABoolean
. True if the variable has 'this sprite only' checked.sprite
- AString
with the variable's parent sprite.null
ifspriteOnly
isfalse
.value
- AString
orArray
(if the variable is a list) containing the value of the variables. In the case of cloud variables this may not be accurate as this value is only saved when a project is saved.type
- AString
containing the type of the variable, eithervariable
orlist
.isCloud
- ABoolean
. True if the variable is a cloud variable.
spritenames
- AnArray
ofString
s containing the names of all of the sprites in the project.sprites
- AnArray
ofSprite
s.
Sprite
The Sprite
class reformats the sprite objects in Scratch's project api's responses.
Each Sprite
instance will be formatted like this:
variables
- SeeProject - variables
.lists
- SeeProject - variables
.broadcasts
- AnArray
ofString
s containing each of the project's broadcast messages.blocks
- AnArray
containing the blocks in theSprite
's workspace.comments
- AnArray
containing the comments in theSprite
's workspace.isStage
- ABoolean
. True if theSprite
is the stage.name
- AString
containing theSprite
's name.costumes
- An array of theSprite
's costumes.sounds
- An array of theSprite
's sounds.volume
- ANumber
containing theSprite
's volume.layer
- ANumber
containing theSprite
's layer in the project.