Giter Club home page Giter Club logo

scratch-api's Introduction

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

Sets the user's backpack to a single script.

var Scratch = require('scratch-api');
Scratch.UserSession.load(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');
  });
});

Prints all of the cloud variables for the given project.

var Scratch = require('scratch-api');

Scratch.UserSession.load(function(err, user) {
  user.cloudSession(<project>, function(err, cloud) {
    cloud.on('set', function(name, value) {
      console.log(name, value);
    });
  });
});

See Also

This scratch-api module is setup to be easily added too and extended. If you need to make certain requests that are not present it should be easy to add them. The Scratch Wiki has some pretty extensive documentation.

If you are feeling Pythonic today, check out Dylan Beswick's very similar module for Python.

API

Scratch

Scratch.UserSession

Scratch.CloudSession

Scratch

static getProject(projectId, callback)

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

  • projectId - The project's ID.
  • callback(err, project)

UserSession

static create(username, password, callback)

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

  • username - The Scratch account username (not case sensitive).
  • password - The Scratch account password.
  • callback(err, user)

static prompt(callback)

Creates a new Scratch session by prompting for the username and password via the command line.

  • callback(err, user)

static load(callback)

Attempts to create a user from a saved .scratchSession file. If one is not found, prompt is used instead and a .scratchSession file is created.

  • callback(err, user)

verify(callback)

Verifies that the user session is fresh and is ready to be used.

  • callback(err, valid)

getProject(projectId, callback)

Retrieves a JSON object of the given Scratch project.

  • projectId - The project's ID.
  • callback(err, project)

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.

  • projectId - The project's ID.
  • payload - A JSON object or string. If it is an object, it will be stringified before sent.
  • callback(err)

getBackpack(callback)

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

  • callback(err, payload)

setBackpack(payload, callback)

Uploads the given payload to the user's backpack.

  • payload - A JSON object or a string to be uploaded.
  • callback(err)

addComment(options, callback)

Comments on a project, profile, or studio.

  • options - A JSON object containing options.
    • project, user, or studio: The function checks (in that order) for these values. The user must be a username to post to, and all 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.
  • callback(err)

cloudSession(projectId, callback)

Connects to a cloud variable session for the given project.

  • projectId - The project's ID.
  • callback(err, cloudSession)

Scratch.CloudSession

end()

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

get(name)

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

  • name - The variable name including the cloud (โ˜) symbol.

set(name, value)

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

  • name - The variable name including the cloud (โ˜) symbol.
  • value - A new value.

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.

Event: 'set'

Emitted when a cloud variable is changed.

  • name - The variable name.
  • value - The variables new value.

Event: 'end'

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

scratch-api's People

Contributors

trumank avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.