This extension allows you to use various JavaScript features in Scratch!
To use ScratchJS, create a bookmarklet with the following code.
(For non-technical users: Make a bookmark and set the URL to this code.)
javascript: (async () => {
try {
const r = await fetch(
"https://raw.githubusercontent.com/Ironbill25/JavaScript-For-Scratch/refs/heads/main/scratchjs.js",
{ cache: "no-cache" }
);
if (!r.ok) throw new Error("Fetch failed: " + r.status);
const scriptText = await r.text();
const s = document.createElement("script");
s.textContent = scriptText;
document.body.appendChild(s);
console.log("Loaded");
} catch (e) {
console.error("Failed to load script:", e);
}
})();Go to the Scratch editor and click the bookmarklet to load the extension.
When ScratchJS loads, you will see a warning modal indicating that this extension has access to advanced features. Only use projects with ScratchJS from creators you trust, as projects can potentially perform dangerous operations.
Type: Reporter
Returns: Base raised to the power of exponent
Parameters:
base(number, default: 2) - Base numberexponent(number, default: 3) - Exponent
Example: 2 ^ 3 returns 8
Type: Reporter
Returns: Value constrained within min and max range
Parameters:
value(number, default: 15) - Value to clampmin(number, default: 0) - Minimum valuemax(number, default: 10) - Maximum value
Example: Clamp 15 between 0 and 10 returns 10
Type: Reporter
Returns: Number rounded to specified decimal places
Parameters:
number(number, default: 3.14159) - Number to rounddecimals(number, default: 2) - Number of decimal places
Example: Round 3.14159 to 2 decimal places returns 3.14
Type: Reporter
Returns: Percentage calculation
Parameters:
part(number, default: 25) - Percentage valuewhole(number, default: 100) - Whole value
Example: 25% of 100 returns 25
Type: Reporter
Returns: Value incremented by 1
Parameters:
value(number, default: 5) - Value to increment
Example: 5++ returns 6
Type: Reporter
Returns: Value decremented by 1
Parameters:
value(number, default: 5) - Value to decrement
Example: 5-- returns 4
Type: Boolean
Returns: True if value1 is greater than or equal to value2
Parameters:
value1(number, default: 5) - First valuevalue2(number, default: 5) - Second value
Type: Boolean
Returns: True if value1 is less than or equal to value2
Parameters:
value1(number, default: 5) - First valuevalue2(number, default: 5) - Second value
Type: Boolean
Returns: True if values are not equal
Parameters:
value1(number, default: 5) - First valuevalue2(number, default: 5) - Second value
Type: Boolean
Returns: Always returns true
Type: Boolean
Returns: Always returns false
Type: Reporter
Returns: Text with all occurrences of string replaced
Parameters:
text(string, default: "Hello World") - Original textstring(string, default: "World") - String to replacereplace(string, default: "Scratch") - Replacement string
Example: Replace all "World" in "Hello World" with "Scratch" returns "Hello Scratch"
Type: Reporter
Returns: Portion of text between specified positions (1-based indexing)
Parameters:
text(string, default: "Hello World") - Original textstart(number, default: 1) - Start position (1-based)end(number, default: 6) - End position (1-based)
Example: Get substring of "Hello World" from 1 to 6 returns "Hello"
Type: Reporter
Returns: Text reversed character by character
Parameters:
text(string, default: "Hello World") - Text to reverse
Example: Reverse string "Hello" returns "olleH"
Type: Reporter
Returns: Text converted to specified case
Parameters:
text(string, default: "Hello World") - Text to convertcaseType(menu) - Case conversion type:UPPERCASE- Convert to all uppercaselowercase- Convert to all lowercase
Example: Convert "hello world" to case UPPERCASE returns "HELLO WORLD"
Type: Reporter
Returns: Newline character (\n)
Type: Reporter
Returns: Tab character (\t)
Type: Reporter
Returns: Current date/time in specified format
Options:
date and time- Full date and time stringdate only- Current date (locale format)time only- Current time (locale format)timestamp- Unix timestamp in milliseconds
Example: current date and time returns "3/17/2026, 8:06:00 PM"
Type: Reporter
Returns: The current Scratch project ID from the URL
Example: If URL is https://scratch.mit.edu/projects/123456/, returns "123456"
Type: Reporter
Returns: Various system and browser information
Options:
operating system- OS platform (e.g., "Win32")browser- Browser user agent stringlanguage- Browser language (e.g., "en-US")time zone- User's timezone (e.g., "America/New_York")screen width- Screen width in pixelsscreen height- Screen height in pixelswindow width- Browser window width in pixelswindow height- Browser window height in pixelsdevice pixel ratio- Device pixel ratio
Example: Get info on the operating system returns "Win32"
Type: Command
Action: Executes the given JavaScript code
Parameters:
code(string, default: "alert('Hello World!')") - JavaScript code to execute
Warning: This block can execute any JavaScript code and should be used with caution.
Type: Reporter
Returns: The return value of the executed JavaScript code
Parameters:
code(string, default: "6473 / 84") - JavaScript code to execute and return value from
Example: JS| Get return value of "Math.random()" returns a random number between 0 and 1
Type: Command
Action: Creates or updates a JavaScript variable
Parameters:
name(string, default: "window.example") - Variable nameval(string, default: "Hello World!") - Value to assign
Example: JS| Set variable "window.myVar" to "test" creates a global variable
Type: Command
Action: Opens the specified URL in a new browser tab
Parameters:
url(string, default: "https://example.com") - URL to open
Example: JS| Open site "https://scratch.mit.edu" opens Scratch in a new tab
Type: Command
Action: Opens the current project in Turbowarp
Type: Command
Action: Reloads the current page
Type: Command
Action: Downloads a text file with the specified name and contents
Parameters:
name(string, default: "example.txt") - Filenamecontents(string, default: "Hello World!") - File contents
Example: JS| Save file "data.txt" with contents "Hello from Scratch!" downloads a text file
Type: Hat
Triggers: When the condition becomes true
Parameters:
condit(boolean) - Condition to monitor
Example: when "mouse down?" is true triggers when mouse is pressed
Type: Conditional
Action: Loop control block that increments i and checks if i <= value
Parameters:
value(string, default: "10") - Maximum value for loop
Use: Works with the i reporter and set i blocks for custom loops
Type: Reporter
Returns: Current value of loop counter i
Type: Command
Action: Sets the loop counter i to specified value
Parameters:
value(number, default: 0) - Value to set i to
Type: Reporter
Returns: Mouse X coordinate (works even when mouse is outside stage)
Type: Reporter
Returns: Mouse Y coordinate (works even when mouse is outside stage)
Type: Boolean
Returns: True if mouse button is pressed (works even when mouse is outside stage)
Type: Boolean
Returns: Boolean value converted from text
Parameters:
bool(string, default: "true") - Text to convert to boolean
Accepts: "true", "1", "True", or any non-empty string (except "0", "false", "False") returns true
Type: Reporter
Returns: Text representation of boolean value
Parameters:
bool(boolean) - Boolean to convert to text
Example: Converts true to "true" and false to "false"
Type: Reporter
Returns: The input argument unchanged
Parameters:
arg1(string, default: "Hello") - Input value
Use: Useful for type conversion or passing values through blocks
Type: Reporter
Returns: arg2 if arg1 is true, otherwise arg3
Parameters:
arg1(boolean) - Conditionarg2(string, default: "Hello") - Value if truearg3(string, default: "World") - Value if false
Example: if "mouse down?" then "Pressed" else "Released" returns "Pressed" when mouse is down
Type: Reporter
Returns: Empty array as JSON string "[]"
Type: Reporter
Returns: Array with value appended to end
Parameters:
value(string, default: "Hello") - Value to appendarray(string, default: "[]") - Array as JSON string
Example: Append "World" to array "[\"Hello\"]" returns "["Hello","World"]"
Type: Reporter
Returns: Item at specified index (1-based)
Parameters:
index(number, default: 1) - Index (1-based)array(string, default: "[]") - Array as JSON string
Example: Get 2 from array "[\"Apple\",\"Banana\"]" returns "Banana"
Type: Reporter
Returns: Array with value inserted at specified position
Parameters:
value(string, default: "Hello") - Value to insertindex(number, default: 1) - Index position (1-based)array(string, default: "["Apple"]") - Array as JSON string
Type: Reporter
Returns: Array with item at index replaced
Parameters:
index(number, default: 1) - Index to replace (1-based)array(string, default: "["Apple"]") - Array as JSON stringvalue(string, default: "Banana") - New value
Type: Reporter
Returns: Array with item at specified index removed
Parameters:
index(number, default: 1) - Index to remove (1-based)array(string, default: "["Apple"]") - Array as JSON string
Type: Reporter
Returns: Two arrays combined into one
Parameters:
array1(string, default: "["Hello"]") - First arrayarray2(string, default: "["World"]") - Second array
Type: Reporter
Returns: Number of items in array
Parameters:
array(string, default: "["Apple", "Banana"]") - Array as JSON string
Type: Boolean
Returns: True if array contains the specified value
Parameters:
array(string, default: "["Apple", "Banana"]") - Array as JSON stringvalue(string, default: "Carrot") - Value to search for
Type: Reporter
Returns: Index of first occurrence of value (1-based, returns 0 if not found)
Parameters:
value(string, default: "Hello") - Value to findarray(string, default: "["Apple"]") - Array as JSON string
Type: Reporter
Returns: String split into array by delimiter
Parameters:
string(string, default: "Hello, World") - String to splitdelimiter(string, default: ",") - Delimiter character
Example: Split "Hello, World" by "," into array returns "["Hello"," World"]"
Type: Reporter
Returns: Array elements joined into string with delimiter
Parameters:
array(string, default: "["Hello", "World"]") - Array as JSON stringdelimiter(string, default: ",") - Delimiter character
Example: Join array "[\"Hello\",\"World\"]" with "," returns "Hello,World"
Type: Reporter
Returns: Empty object as JSON string "{}"
Type: Reporter
Returns: Object with key set to value
Parameters:
key(string, default: "name") - Property keyobject(string, default: "{}") - Object as JSON stringvalue(string, default: "John") - Value to set
Example: Set "name" in object "{}" to "John" returns "{"name":"John"}"
Type: Reporter
Returns: Value of specified key from object
Parameters:
key(string, default: "name") - Property keyobject(string, default: "{"name": "John"}") - Object as JSON string
Example: Get "name" from object "{\"name\": \"John\"}" returns "John"
Type: Reporter
Returns: Object with specified key removed
Parameters:
key(string, default: "name") - Property key to deleteobject(string, default: "{"name": "John"}") - Object as JSON string
Type: Boolean
Returns: True if object contains the specified key
Parameters:
object(string, default: "{"name": "John"}") - Object as JSON stringkey(string, default: "name") - Key to check
Type: Reporter
Returns: Array of object's keys as JSON string
Parameters:
object(string, default: "{"name": "John"}") - Object as JSON string
Example: Keys of object "{\"name\": \"John\"}" (array) returns "["name"]"
Type: Reporter
Returns: Array of object's values as JSON string
Parameters:
object(string, default: "{"name": "John"}") - Object as JSON string
Example: Values of object "{\"name\": \"John\"}" (array) returns "["John"]"
Type: Reporter
Returns: Array of [key, value] pairs as JSON string
Parameters:
object(string, default: "{"name": "John"}") - Object as JSON string
Example: Entries of object "{\"name\": \"John\"}" (array) returns "[["name","John"]]"
Type: Reporter
Returns: Number of keys in object
Parameters:
object(string, default: "{"name": "John"}") - Object as JSON string
Type: Reporter
Returns: Value at nested path in object
Parameters:
path(string, default: "["name"]") - Path as JSON arrayobject(string, default: "{"name": "John"}") - Object as JSON string
Example: Get path "[\"name\"]" from object "{\"name\": \"John\"}" returns "John"
Type: Reporter
Returns: Object with nested path set to value
Parameters:
path(string, default: "["name"]") - Path as JSON arrayobject(string, default: "{}") - Object as JSON stringvalue(string, default: "John") - Value to set
Example: Set path "[\"name\"]" in object "{}" to "John" returns "{"name":"John"}"
- REPORTER: Returns a value, can be used in round inputs
- COMMAND: Performs an action, can be stacked
- HAT: Event-triggered block, runs code below when condition is met
- BOOLEAN: Returns true/false, can be used in boolean inputs
- string: Accepts any text or reporter block
- number: Accepts numbers or reporter blocks
- Boolean: Accepts only boolean blocks
- menu: Dropdown menu with predefined options
ScratchJS provides powerful JavaScript execution capabilities. Users should:
- Only use projects from trusted creators
- Be cautious with blocks that execute arbitrary JavaScript
- Understand that file operations and web access are available
ScratchJS is built using the Scratch extension system. The extension:
- Waits for the Scratch VM to be available
- Creates a new ScratchJS extension instance
- Registers all blocks with the Scratch runtime
- Provides JavaScript execution environment
For more information on creating Scratch extensions, see extensiontutorial.md.