Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local storage extension #55

Merged
merged 13 commits into from
May 15, 2023
163 changes: 163 additions & 0 deletions extensions/local-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
(function (Scratch) {
'use strict';

if (!Scratch.extensions.unsandboxed) {
throw new Error('Local Storage must be run unsandboxed');
}

const PREFIX = 'extensions.turbowarp.org/local-storage:';
let namespace = '';
const getFullStorageKey = () => `${PREFIX}${namespace}`;

const readFromStorage = () => {
try {
// localStorage could throw if unsupported
const data = localStorage.getItem(getFullStorageKey());
if (data) {
// JSON.parse could throw if data is invalid
const parsed = JSON.parse(data);
if (parsed && parsed.data) {
// Remove invalid values from the JSON
const processed = {};
for (const [key, value] of Object.entries(parsed.data)) {
if (typeof value === 'number' || typeof value === 'string' || typeof value === 'boolean') {
processed[key] = value;
}
}
return processed;
}
}
} catch (error) {
console.error('error reading from local storage', error);
}
return {};
};

const saveToLocalStorage = (data) => {
try {
if (Object.keys(data).length > 0) {
localStorage.setItem(getFullStorageKey(), JSON.stringify({
time: Math.round(Date.now() / 1000),
data
}));
} else {
localStorage.removeItem(getFullStorageKey());
}
} catch (error) {
console.error('error saving to locacl storage', error);
}
};

window.addEventListener('storage', (event) => {
if (event.key === getFullStorageKey() && event.storageArea === localStorage) {
Scratch.vm.runtime.startHats('localstorage_whenChanged');
}
});

class LocalStorage {
getInfo() {
return {
id: 'localstorage',
name: 'Local Storage',
blocks: [
{
opcode: 'setProjectId',
blockType: Scratch.BlockType.COMMAND,
text: 'set storage namespace ID to [ID]',
arguments: {
ID: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'project title'
}
}
},
{
opcode: 'get',
blockType: Scratch.BlockType.REPORTER,
text: 'get key [KEY]',
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'score',
},
},
},
{
opcode: 'set',
blockType: Scratch.BlockType.COMMAND,
text: 'set key [KEY] to [VALUE]',
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'score',
},
VALUE: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1000',
},
},
},
{
opcode: 'remove',
blockType: Scratch.BlockType.COMMAND,
text: 'delete key [KEY]',
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'score'
},
},
},
{
opcode: 'removeAll',
blockType: Scratch.BlockType.COMMAND,
text: 'delete all keys'
},
{
opcode: 'whenChanged',
blockType: Scratch.BlockType.HAT,
text: 'when another window changes storage',
isEdgeActivated: false
}
],
};
}
setProjectId({ ID }) {
namespace = Scratch.Cast.toString(ID);
}
get({ KEY }) {
if (!namespace) {
return 'must use "set storage namespace ID to ..." block first';
}
const storage = readFromStorage();
KEY = Scratch.Cast.toString(KEY);
if (Object.prototype.hasOwnProperty.call(storage, KEY)) {
return storage[KEY];
}
return 'key does not exist';
}
set({ KEY, VALUE }) {
if (!namespace) {
return;
}
const storage = readFromStorage();
storage[Scratch.Cast.toString(KEY)] = VALUE;
saveToLocalStorage(storage);
}
remove({ KEY }) {
if (!namespace) {
return;
}
const storage = readFromStorage();
delete storage[Scratch.Cast.toString(KEY)];
saveToLocalStorage(storage);
}
removeAll() {
if (!namespace) {
return;
}
saveToLocalStorage({});
}
}
Scratch.extensions.register(new LocalStorage());
})(Scratch);
6 changes: 6 additions & 0 deletions website/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@
<p>Manipulate characters and text. Originally created by <a href="https://scratch.mit.edu/users/CST1229/">CST1229</a>.</p>
</div>

<div class="extension">
<%- banner('local-storage') %>
<h3>Local Storage</h3>
<p>Store data persistently.</p>
</div>

<div class="extension">
<%- banner('clouddata-ping') %>
<h3>Ping Cloud Data</h3>
Expand Down