An Introduction to Hacking Scratch
Scratch projects are saved in a file format known as SB3, standing for Scratch Blocks 3. Don't let the name fool you, though; this file stores not only the blocks in the project, but also the sprites, costumes, sounds, comments, variables, lists... basically everything, except the instructions and notes & credits.
The main way of editing such files is through the official Scratch 3.0 editor, but these SB3 files can be edited externally ─ and can be manipulated to a degree where the creators of Scratch did not intend. This process is generally known as 'hacking' ─ when you have edited a project using an external program, you've 'hacked' it.
Today I will show you how to hack a Scratch project: we'll create a red hat block. Let's get to it!
Step 1: Download a new project
Click the Create button on the top of the Scratch page or go to https://scratch.mit.edu/projects/editor/. Add any block you like to the scripts area: we will transform this into a red block. Now download the project by clicking the File tab and then Save to your computer, as shown below.
Step 2: Getting ready to hack
As you will have seen, a file has been downloaded to your computer. It is an SB3 file, and we're going to hack it. Depending on your operating system, the following steps may be a little different, but should roughly be the same.
SB3 files are compressed to use up less storage space on the Scratch servers. They are compressed in the same way ZIP files are, so what we'll have to do is 'unzip' the file. Your operating system probably won't realise you can unzip it, so you'll have to rename the file to replace 'sb3' with 'zip'. Then unzip the file or extract it.
As you can see, there are several files that were zipped: the costumes, sounds, and another file: 'project.json'. This is the file that interests us ─ if we edit it, we will be able to create hacked blocks and use unintended features. We'll use an online editor called JSON Editor Online (JSON stands for JavaScript Object Notation, and is how data is stored in JavaScript, the programming language Scratch 3.0 is written in. However, there's no need to know JavaScript to hack Scratch ─ a visual editor like the one we'll use will be enough).
JSON Editor Online allows you to edit two files at once ─ we'll just use one for now. On the left tab, hover over the folder icon and click Open from disk. Select the 'project.json' file we talked about, and it will be opened in the editor.
Step 3: Let's get hacking!
You will see some triangles that you can click to open and close certain parts of the project data: we want to change a block, so we have to open the targets section (targets is the same as sprites, but also including the stage). You will see two entries: the first (labelled 0) is the stage, and the second (labelled 1) is the cat. Open the last one. You will see quite a few values: name, isStage, variables... what we're interested in is the blocks section: open it. Here there is only one entry. You'll see a random sequence of letters, numbers and characters; this is the block ID, and every block has it's own unique ID. Finally, open that section. You should see something like this (the block ID will be different for you, though, since it is generated randomly):- opcode: the type of block it is. The first word is the category, and the rest, the name of the block (e.g. motion_movesteps)
- next: the block ID of the block under this one. The value is null if there is no block under.
- parent: the block ID of the block above or containing this one. The value is null if there is no block above or containing it.
- inputs: contains information on each text, number, boolean or 'round dropdown' input.
- fields: contains information on each 'square dropdown'.
- shadow: used for round dropdowns, we'll see about this later.
- topLevel: whether the block is the first of the script.
Either way, what interests us is the opcode property. In Scratch 3.0, if the opcode of a block does not exist, the block will turn red. Replace the value of opcode (highlighted in yellow in the image above) for something random like carrots. It doesn't matter, as long as it's not the name of a real block!
Step 4: Finishing steps
You've now downloaded the SB3 file, unzipped it, and hacked the JSON file. Now, we need to see these changes in the editor. This is what we have to do: first, replace the old JSON with the new hacked one we made, second, zip the new JSON and other files, and third, upload this to Scratch.
- Download the new JSON by hovering over the third icon and clicking Save to disk.
- Create a folder and put the new project.json and all the other files (the images and sounds) in.
- Select all the items and right click. On the operating system I use, there is an option called 'Zip selection'. If you have it, click it, and it will zip them. Otherwise, you may have to use an online zipper such as ezyzip.
- Once you have the zipped file, rename it to replace 'zip' with 'sb3' (notice how we're essentially doing the same as in step 2 but in reverse).
- Finally, go to Scratch, click File and then Load from your computer. Select the SB3 file you just renamed, and you're done.
You can always ask me for help on my profile if it didn't turn out or if I wasn't clear enough.
This lesson was long because I had to explain each step you have to make before and after hacking ─ but in future tutorials, I'll concentrate only on the fun part: hacking! What should I create a tutorial about next? Tell me on my profile! That's all for now, keep Scratching!
~ qucchia