-
Notifications
You must be signed in to change notification settings - Fork 0
/
localstorage.js
172 lines (165 loc) · 4.76 KB
/
localstorage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
(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 validNamespace = () => {
const valid = !!namespace;
if (!valid) {
alert('Local Storage extension: project must run the "set storage namespace ID" block before it can use other blocks');
}
return valid;
};
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 (namespace && event.key === getFullStorageKey() && event.storageArea === localStorage) {
Scratch.vm.runtime.startHats('localstorage_whenChanged');
}
});
class LocalStorage {
getInfo() {
return {
id: 'localstorage',
name: 'Local Storage',
docsURI: "https://extensions.turbowarp.org/local-storage.html",
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.EVENT,
text: 'when another window changes storage',
isEdgeActivated: false
}
],
};
}
setProjectId({ ID }) {
namespace = Scratch.Cast.toString(ID);
}
get({ KEY }) {
if (!validNamespace()) {
return '';
}
const storage = readFromStorage();
KEY = Scratch.Cast.toString(KEY);
if (!Object.prototype.hasOwnProperty.call(storage, KEY)) {
return '';
}
return storage[KEY];
}
set({ KEY, VALUE }) {
if (!validNamespace()) {
return '';
}
const storage = readFromStorage();
storage[Scratch.Cast.toString(KEY)] = VALUE;
saveToLocalStorage(storage);
}
remove({ KEY }) {
if (!validNamespace()) {
return '';
}
const storage = readFromStorage();
delete storage[Scratch.Cast.toString(KEY)];
saveToLocalStorage(storage);
}
removeAll() {
if (!validNamespace()) {
return '';
}
saveToLocalStorage({});
}
}
Scratch.extensions.register(new LocalStorage());
})(Scratch);