JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q40506F
  • License MIT

An API to interact with the Scratch 2.0 website

Package Exports

  • scratch-api

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (scratch-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

scratch-api

A utility for interacting with the Scratch 2.0 website.

Installation

Install with npm:

npm install scratch-api

Or by cloning this repository:

git clone https://github.com/trumank/scratch-api.git

Examples

var Scratch = require('scratch-api');
Scratch.createUserSession(username, password, function(err, user) {
  if (err) return console.error(err);
  user.setBackpack([{
    type: 'script',
    name: '',
    scripts: [[['say:', 'Cheers!']]]
  }],
  function(err, res) {
    if (err) return console.error(err);
    console.log('Backpack set');
  });
});

API

Scratch

Scratch.UserSession

Scratch.CloudSession

Scratch

### Scratch.createUserSession(username, password, callback)

Created a new Scratch session by signing in with the given username and password.

Arguments

  • username - The Scratch account username (not case sensitive).
  • password - The Scratch account password.
  • callback(err, user) - A callback that is called with the resulting user or an error if one occurs.
### Scratch.getProject(projectId, callback)

Retrieves a JSON object of the given Scratch project. Equivalent to Scratch.UserSession.getProject but does not require being signed in.

Arguments

  • projectId - The project's ID.
  • callback(err, project) - A callback that is called with the resulting project or an error if one occurs.

UserSession

### Scratch.UserSession.getProject(projectId, callback)

Retrieves a JSON object of the given Scratch project.

Arguments

  • projectId - The project's ID.
  • callback(err, project) - A callback that is called with the resulting project or an error if one occurs.
### Scratch.UserSession.setProject(projectId, payload, callback)

Uploads the given payload object or string to the project with the given ID. The user must own the given project or the request will fail.

Arguments

  • projectId - The project's ID.
  • payload - A JSON object or string. If it is an object, it will be stringified before sent.
  • callback(err) - A callback that is called when it finishes and or an error occurs.
### Scratch.UserSession.getBackpack(callback)

Retrieves the signed in user's backpack as a JSON object.

Arguments

  • callback(err, payload) - A callback that is called with the returned backpack object or an error if one occurs.
### Scratch.UserSession.setBackpack(payload, callback)

Uploads the given payload to the user's backpack.

Arguments

  • payload - A JSON object or a string to be uploaded.
  • callback(err) - A callback that is called when the request finishes or an error occurs.
### Scratch.UserSession.cloud(projectId, callback)

Connects to a cloud variable session for the given project.

Arguments

  • projectId - The project's ID.
  • callback(err, cloudSession) - A callback that is called with the returned CloudSession object or if an error occurs.

Scratch.CloudSession

### Scratch.CloudSession.connect(callback)

Used to start a newly created CloudSession.

Arguments

  • callback - A callback that is called when a connection is made or an error occurs.
### Scratch.CloudSession.end()

Used to disconnect from the server and end the cloud session.

### Scratch.CloudSession.get(name)

Returns the value of a cloud variable or undefined if it does not exist.

Arguments

  • name - The variable name including the cloud (☁) symbol.
### Scratch.CloudSession.set(name, value)

Sets the variable with the given name to the given value.

Arguments

  • name - The variable name including the cloud (☁) symbol.
  • value - A new value.
### Scratch.CloudSession.variables

An object used as a hash table for all cloud variables. Variables can both read set directly via this object or through the corresponding get and set methods.

### Scratch.CloudSession Event: 'set'

Emitted when a cloud variable is changed.

Parameters

  • name - The variable name.
  • value - The variables new value.
### Scratch.CloudSession Event: 'end'

Emitted when the server closes the connection (should never happen unless the client breaks).