|
1 | | - (function(Scratch) { |
2 | | - 'use strict'; |
3 | | - |
4 | | - if (!Scratch.extensions.unsandboxed) { |
5 | | - throw new Error('This extension must run unsandboxed'); |
6 | | - } |
7 | | - |
8 | | - class StorageV2 { |
9 | | - constructor() { |
10 | | - this.currentServer = "https://storage-ext.penguinmod.com/"; |
11 | | - this.useGlobal = true; |
12 | | - this.waitingForResponse = false; |
13 | | - this.serverFailedResponse = false; |
14 | | - this.serverError = ""; |
15 | | - } |
16 | | - |
17 | | - getInfo() { |
18 | | - return { |
19 | | - id: 'P7SuperStorage', |
20 | | - name: 'Super Storage', |
21 | | - color1: '#31b3d4', |
22 | | - color2: '#179fc2', |
23 | | - docsURI: 'https://pooiod7.neocities.org/markdown/#/projects/scratch/extensions/other/markdown/SuperStorage', |
24 | | - blocks: [ |
25 | | - { blockType: Scratch.BlockType.LABEL, text: "Local Storage" }, |
26 | | - { |
27 | | - opcode: 'getValue', |
28 | | - text: 'get local [KEY]', |
29 | | - disableMonitor: true, |
30 | | - blockType: Scratch.BlockType.REPORTER, |
31 | | - arguments: { |
32 | | - KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" } |
33 | | - } |
34 | | - }, |
35 | | - { |
36 | | - opcode: 'setValue', |
37 | | - text: 'set local [KEY] to [VALUE]', |
38 | | - blockType: Scratch.BlockType.COMMAND, |
39 | | - arguments: { |
40 | | - KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" }, |
41 | | - VALUE: { type: Scratch.ArgumentType.STRING, defaultValue: "value" } |
42 | | - } |
43 | | - }, |
44 | | - { |
45 | | - opcode: 'deleteValue', |
46 | | - text: 'delete local [KEY]', |
47 | | - blockType: Scratch.BlockType.COMMAND, |
48 | | - arguments: { KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" } } |
49 | | - }, |
50 | | - { |
51 | | - opcode: 'getKeys', |
52 | | - text: 'get all local stored names', |
53 | | - disableMonitor: true, |
54 | | - blockType: Scratch.BlockType.REPORTER |
55 | | - }, |
56 | | - { |
57 | | - blockType: Scratch.BlockType.LABEL, |
58 | | - text: "Server Storage" |
59 | | - }, |
60 | | - { |
61 | | - opcode: 'waitingForConnection', |
62 | | - text: 'waiting for server to respond?', |
63 | | - disableMonitor: true, |
64 | | - blockType: Scratch.BlockType.BOOLEAN |
65 | | - }, |
66 | | - { |
67 | | - opcode: 'connectionFailed', |
68 | | - text: 'server failed to respond?', |
69 | | - disableMonitor: true, |
70 | | - blockType: Scratch.BlockType.BOOLEAN |
71 | | - }, |
72 | | - { |
73 | | - opcode: 'serverErrorOutput', |
74 | | - text: 'server error', |
75 | | - disableMonitor: false, |
76 | | - blockType: Scratch.BlockType.REPORTER |
77 | | - }, |
78 | | - "---", |
79 | | - { |
80 | | - opcode: 'getServerValue', |
81 | | - text: 'get server [KEY]', |
82 | | - disableMonitor: true, |
83 | | - blockType: Scratch.BlockType.REPORTER, |
84 | | - arguments: { KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" } } |
85 | | - }, |
86 | | - { |
87 | | - opcode: 'setServerValue', |
88 | | - text: 'set server [KEY] to [VALUE]', |
89 | | - blockType: Scratch.BlockType.COMMAND, |
90 | | - arguments: { |
91 | | - KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" }, |
92 | | - VALUE: { type: Scratch.ArgumentType.STRING, defaultValue: "value" } |
93 | | - } |
94 | | - }, |
95 | | - { |
96 | | - opcode: 'deleteServerValue', |
97 | | - text: 'delete server [KEY]', |
98 | | - blockType: Scratch.BlockType.COMMAND, |
99 | | - arguments: { KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" } } |
100 | | - } |
101 | | - ] |
102 | | - }; |
103 | | - } |
104 | | - |
105 | | - getPrefix() { |
106 | | - return `P7_PROJECTSTORAGE_`; |
107 | | - } |
108 | | - |
109 | | - getAllKeys() { |
110 | | - return Object.keys(localStorage).filter(key => key.startsWith(this.getPrefix())).map(key => key.replace(this.getPrefix(), "")); |
111 | | - } |
112 | | - |
113 | | - runPenguinWebRequest(url, options, ifFailReturn) { |
114 | | - this.waitingForResponse = true; |
115 | | - this.serverFailedResponse = false; |
116 | | - this.serverError = ""; |
117 | | - |
118 | | - return fetch(url, options) |
119 | | - .then(response => response.ok ? response.text() : Promise.reject(response.text())) |
120 | | - .then(text => { |
121 | | - this.waitingForResponse = false; |
122 | | - return text; |
123 | | - }) |
124 | | - .catch(err => { |
125 | | - this.waitingForResponse = false; |
126 | | - this.serverFailedResponse = true; |
127 | | - this.serverError = err; |
128 | | - return ifFailReturn; |
129 | | - }); |
130 | | - } |
131 | | - |
132 | | - getKeys() { |
133 | | - return JSON.stringify(this.getAllKeys()); |
134 | | - } |
135 | | - |
136 | | - getValue(args) { |
137 | | - return localStorage.getItem(this.getPrefix() + args.KEY) || ""; |
138 | | - } |
139 | | - |
140 | | - setValue(args) { |
141 | | - localStorage.setItem(this.getPrefix() + args.KEY, args.VALUE); |
142 | | - } |
143 | | - |
144 | | - deleteValue(args) { |
145 | | - localStorage.removeItem(this.getPrefix() + args.KEY); |
146 | | - } |
147 | | - |
148 | | - waitingForConnection() { |
149 | | - return this.waitingForResponse; |
150 | | - } |
151 | | - |
152 | | - connectionFailed() { |
153 | | - return this.serverFailedResponse; |
154 | | - } |
155 | | - |
156 | | - serverErrorOutput() { |
157 | | - return this.serverError; |
158 | | - } |
159 | | - |
160 | | - getServerValue(args) { |
161 | | - return this.runPenguinWebRequest(`${this.currentServer}get?key=${args.KEY}`, null, ""); |
162 | | - } |
163 | | - |
164 | | - setServerValue(args) { |
165 | | - return this.runPenguinWebRequest(`${this.currentServer}set?key=${args.KEY}`, { |
166 | | - method: "POST", |
167 | | - headers: { "Content-Type": "application/json" }, |
168 | | - body: JSON.stringify({ "value": args.VALUE }) |
169 | | - }); |
170 | | - } |
171 | | - |
172 | | - deleteServerValue(args) { |
173 | | - return this.runPenguinWebRequest(`${this.currentServer}delete?key=${args.KEY}`, { method: "DELETE" }); |
174 | | - } |
175 | | - } |
176 | | - |
177 | | - Scratch.extensions.register(new StorageV2()); |
178 | | -})(Scratch); |
| 1 | + (function(Scratch) { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + if (!Scratch.extensions.unsandboxed) { |
| 5 | + throw new Error('This extension must run unsandboxed'); |
| 6 | + } |
| 7 | + |
| 8 | + class StorageV2 { |
| 9 | + constructor() { |
| 10 | + this.currentServer = "https://storage-ext.penguinmod.com/"; |
| 11 | + this.useGlobal = true; |
| 12 | + this.waitingForResponse = false; |
| 13 | + this.serverFailedResponse = false; |
| 14 | + this.serverError = ""; |
| 15 | + } |
| 16 | + |
| 17 | + getInfo() { |
| 18 | + return { |
| 19 | + id: 'P7SuperStorage', |
| 20 | + name: 'Super Storage', |
| 21 | + color1: '#31b3d4', |
| 22 | + color2: '#179fc2', |
| 23 | + docsURI: 'https://pooiod7.neocities.org/markdown/#/projects/scratch/extensions/other/markdown/SuperStorage', |
| 24 | + blocks: [ |
| 25 | + { blockType: Scratch.BlockType.LABEL, text: "Local Storage" }, |
| 26 | + { |
| 27 | + opcode: 'getValue', |
| 28 | + text: 'get local [KEY]', |
| 29 | + disableMonitor: true, |
| 30 | + blockType: Scratch.BlockType.REPORTER, |
| 31 | + arguments: { |
| 32 | + KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" } |
| 33 | + } |
| 34 | + }, |
| 35 | + { |
| 36 | + opcode: 'setValue', |
| 37 | + text: 'set local [KEY] to [VALUE]', |
| 38 | + blockType: Scratch.BlockType.COMMAND, |
| 39 | + arguments: { |
| 40 | + KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" }, |
| 41 | + VALUE: { type: Scratch.ArgumentType.STRING, defaultValue: "value" } |
| 42 | + } |
| 43 | + }, |
| 44 | + { |
| 45 | + opcode: 'deleteValue', |
| 46 | + text: 'delete local [KEY]', |
| 47 | + blockType: Scratch.BlockType.COMMAND, |
| 48 | + arguments: { KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" } } |
| 49 | + }, |
| 50 | + { |
| 51 | + opcode: 'getKeys', |
| 52 | + text: 'get all local stored names', |
| 53 | + disableMonitor: true, |
| 54 | + blockType: Scratch.BlockType.REPORTER |
| 55 | + }, |
| 56 | + { |
| 57 | + blockType: Scratch.BlockType.LABEL, |
| 58 | + text: "Server Storage" |
| 59 | + }, |
| 60 | + { |
| 61 | + opcode: 'waitingForConnection', |
| 62 | + text: 'waiting for server to respond?', |
| 63 | + disableMonitor: true, |
| 64 | + blockType: Scratch.BlockType.BOOLEAN |
| 65 | + }, |
| 66 | + { |
| 67 | + opcode: 'connectionFailed', |
| 68 | + text: 'server failed to respond?', |
| 69 | + disableMonitor: true, |
| 70 | + blockType: Scratch.BlockType.BOOLEAN |
| 71 | + }, |
| 72 | + { |
| 73 | + opcode: 'serverErrorOutput', |
| 74 | + text: 'server error', |
| 75 | + disableMonitor: false, |
| 76 | + blockType: Scratch.BlockType.REPORTER |
| 77 | + }, |
| 78 | + "---", |
| 79 | + { |
| 80 | + opcode: 'getServerValue', |
| 81 | + text: 'get server [KEY]', |
| 82 | + disableMonitor: true, |
| 83 | + blockType: Scratch.BlockType.REPORTER, |
| 84 | + arguments: { KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" } } |
| 85 | + }, |
| 86 | + { |
| 87 | + opcode: 'setServerValue', |
| 88 | + text: 'set server [KEY] to [VALUE]', |
| 89 | + blockType: Scratch.BlockType.COMMAND, |
| 90 | + arguments: { |
| 91 | + KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" }, |
| 92 | + VALUE: { type: Scratch.ArgumentType.STRING, defaultValue: "value" } |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + opcode: 'deleteServerValue', |
| 97 | + text: 'delete server [KEY]', |
| 98 | + blockType: Scratch.BlockType.COMMAND, |
| 99 | + arguments: { KEY: { type: Scratch.ArgumentType.STRING, defaultValue: "key" } } |
| 100 | + } |
| 101 | + ] |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + getPrefix() { |
| 106 | + return `P7_PROJECTSTORAGE_`; |
| 107 | + } |
| 108 | + |
| 109 | + getAllKeys() { |
| 110 | + return Object.keys(localStorage).filter(key => key.startsWith(this.getPrefix())).map(key => key.replace(this.getPrefix(), "")); |
| 111 | + } |
| 112 | + |
| 113 | + runPenguinWebRequest(url, options, ifFailReturn) { |
| 114 | + this.waitingForResponse = true; |
| 115 | + this.serverFailedResponse = false; |
| 116 | + this.serverError = ""; |
| 117 | + |
| 118 | + return fetch(url, options) |
| 119 | + .then(response => response.ok ? response.text() : Promise.reject(response.text())) |
| 120 | + .then(text => { |
| 121 | + this.waitingForResponse = false; |
| 122 | + return text; |
| 123 | + }) |
| 124 | + .catch(err => { |
| 125 | + this.waitingForResponse = false; |
| 126 | + this.serverFailedResponse = true; |
| 127 | + this.serverError = err; |
| 128 | + return ifFailReturn; |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + getKeys() { |
| 133 | + return JSON.stringify(this.getAllKeys()); |
| 134 | + } |
| 135 | + |
| 136 | + getValue(args) { |
| 137 | + return localStorage.getItem(this.getPrefix() + args.KEY) || ""; |
| 138 | + } |
| 139 | + |
| 140 | + setValue(args) { |
| 141 | + localStorage.setItem(this.getPrefix() + args.KEY, args.VALUE); |
| 142 | + } |
| 143 | + |
| 144 | + deleteValue(args) { |
| 145 | + localStorage.removeItem(this.getPrefix() + args.KEY); |
| 146 | + } |
| 147 | + |
| 148 | + waitingForConnection() { |
| 149 | + return this.waitingForResponse; |
| 150 | + } |
| 151 | + |
| 152 | + connectionFailed() { |
| 153 | + return this.serverFailedResponse; |
| 154 | + } |
| 155 | + |
| 156 | + serverErrorOutput() { |
| 157 | + return this.serverError; |
| 158 | + } |
| 159 | + |
| 160 | + getServerValue(args) { |
| 161 | + return this.runPenguinWebRequest(`${this.currentServer}get?key=${args.KEY}`, null, ""); |
| 162 | + } |
| 163 | + |
| 164 | + setServerValue(args) { |
| 165 | + return this.runPenguinWebRequest(`${this.currentServer}set?key=${args.KEY}`, { |
| 166 | + method: "POST", |
| 167 | + headers: { "Content-Type": "application/json" }, |
| 168 | + body: JSON.stringify({ "value": args.VALUE }) |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + deleteServerValue(args) { |
| 173 | + return this.runPenguinWebRequest(`${this.currentServer}delete?key=${args.KEY}`, { method: "DELETE" }); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + Scratch.extensions.register(new StorageV2()); |
| 178 | +})(Scratch); |
0 commit comments