Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/local-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ Or delete everything stored in the namespace:
delete storage :: #0FBD8C
```

If you ever loose track of what variables you previously set in storage, you can use the below block. It will return an array of all variables (or 'keys') in storage.

```scratch
(all keys from storage :: #0FBD8C)
```

## Performance

The local storage extension is inevitably slower than regular variables.
Expand Down
14 changes: 14 additions & 0 deletions extensions/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}

const getNamespace = () =>
Scratch.vm.runtime.extensionStorage["localstorage"]?.namespace;

Check warning on line 16 in extensions/local-storage.js

View workflow job for this annotation

GitHub Actions / type-warnings

Type warning - may indicate a bug - ignore if no bug

Property 'namespace' does not exist on type 'JSONSerializable'. Property 'namespace' does not exist on type 'string'.

/**
* @param {string} newNamespace
Expand Down Expand Up @@ -199,6 +199,12 @@
},
},
},
{
opcode: "getAll",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("all keys from storage"),
disableMonitor: true,
},
{
opcode: "removeAll",
blockType: Scratch.BlockType.COMMAND,
Expand Down Expand Up @@ -258,6 +264,14 @@
saveToLocalStorage();
}

getAll() {
if (!validNamespace()) {
return "";
}

return JSON.stringify(Object.keys(namespaceValues));
}

removeAll() {
if (!validNamespace()) {
return "";
Expand Down