npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

scratch-nodejs

v1.0.0

Published

An API to interact with the Scratch 2.0 website

Downloads

5

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

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.

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

static getProjects(username, callback)

Retrieves a list of all public projects belonging to given user.

  • username - Username of owner
  • callback(err, projects)

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) - alias getProject

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)

getProjects(callback) - alias getProjects

getAllProject(callback)

Gets a list of all projects (shared and unshared) belonging to the user.

Note: This does not share the same format as getProjects as it uses the internal API.

  • callback(err, projects)

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)

getEmail(callback)

Get the email of the current user

  • callback(err, payload)

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).