-
Notifications
You must be signed in to change notification settings - Fork 2
/
methods.js
422 lines (349 loc) · 10.2 KB
/
methods.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Import JSZip
import "../lib/jszip.min.js";
const JSZip = window.JSZip;
delete window.JSZip;
// Vue methods
let methods = {};
methods.showAboutScreen = function() {
if (!this.isHomePage) return;
this.clearFiles();
this.projectReady = -2;
this.idInputVal = "";
this.projTitle = "About";
document.title = "Project Explorer";
};
methods.loadURL = async function(url) {
this.fileMode = false;
this.clearFiles();
if (!this.allowProjectLoads) return;
url = decodeURIComponent(url);
if (url === "" || url === "#") {
// Load about page if no hash is specified
this.showAboutScreen();
return;
}
this.projTitle = "Project";
document.title = "Project - Project Explorer";
// In case we already have a project loaded
this.projectReady = 0;
// If the URL does not contain a number, it is instantly not a valid project
let id = url.match(/[-\d]+/);
if (id) {
id = id[0];
}
if (id < 1) {
if (id === null) {
this.projectMessage = url + " does not seem to contain a number.";
} else {
if (id < 0) {
this.projectMessage = "...project IDs can't be negative?";
} else if (id * 1 === 0){
this.projectMessage = "Project IDs cannot be 0.";
} else {
// Just in case
this.projectMessage = url + " does not seem to be a valid project URL or ID.";
}
}
return;
}
// Loading message
this.projectMessage = "Getting project...";
let projTitle, token, originalToken;
try {
const apiResp = await fetch(`https://trampoline.turbowarp.org/proxy/projects/${id}`);
if (apiResp.ok) {
const apiJson = await apiResp.json();
projTitle = apiJson.title;
originalToken = apiJson.project_token;
}
} catch(e) {}
// Allow specify token through a URL parameter
token = new URLSearchParams(location.search.substring(1)).get("token")
|| new URLSearchParams(location.hash.substring(location.hash.indexOf("?"))).get("token")
|| new URLSearchParams(location.hash).get("token")
|| originalToken;
let response = await fetch(`https://projects.scratch.mit.edu/${id}?${
new URLSearchParams({token}).toString()
}`);
let t = await response.text();
if (projTitle !== undefined) {
this.projTitle = projTitle;
document.title = projTitle + " - Project Explorer";
}
if (!response.ok) {
// Try again with the original token, just in case
response = await fetch(`https://projects.scratch.mit.edu/${id}?${
new URLSearchParams({originalToken}).toString()
}`);
t = await response.text();
if (!response.ok) {
this.projectMessage = `Project ID ${id} was either not found or is unshared.`;
if (token !== originalToken) {
this.projectMessage += ` The project token you specified might have expired.`;
}
return;
}
}
this.loadFile(t);
};
// Clears all loaded files.
methods.clearFiles = function() {
for (const name in this.files) {
this.clearFile(name);
}
}
methods.clearFile = function(name) {
if (!this.files[name]) return;
URL.revokeObjectURL(this.files[name]);
}
methods.loadFile = async function(file) {
this.clearFiles();
let PROJECT = null;
this.fileMode = false;
let text;
if (file instanceof Blob) {
text = await file.text();
} else {
text = file;
}
try {
PROJECT = JSON.parse(text);
} catch(e) {
if (text.startsWith("PK")) {
try {
// This file is either an SB3, or SB2 or some other zip -
// enable file mode and pass its project.json
// to PROJECT
this.fileMode = true;
this.projectMessage = "Loading zip...";
const zip = await JSZip.loadAsync(await file.arrayBuffer());
let fileCount = Object.keys(zip.files).length;
let filesLoaded = 0;
this.projectMessage = `Loading zip... (${filesLoaded}/${fileCount})`;
for (const filename in zip.files) {
const entry = zip.files[filename];
if (entry.dir) {
return;
};
this.clearFile(entry.name);
if (entry.name === "project.json") {
try {
PROJECT = JSON.parse(await entry.async("string"));
} catch(e) {}
} else {
// Create a File so we can specify the MIME type for SVGs to work
const file = new File([await entry.async("arraybuffer")], entry.name, {
type: entry.name.endsWith(".svg") ? "image/svg+xml" : "",
});
this.files[entry.name] =
URL.createObjectURL(file);
}
this.projectMessage = `Loading zip... (${++filesLoaded}/${fileCount})`;
}
if (!PROJECT) {
this.projectMessage =
"This zip file is missing a project.json - is it even an SB3?";
return;
}
} catch(e) {
console.error("error loading zip:", e);
this.projectMessage = "Error loading zip: " + e.toString();
return;
}
} else {
if (file.startsWith("ScratchV")) {
this.projectMessage = "Scratch 1.x projects are not supported.";
} else {
this.projectMessage =
"This project doesn't seem to be a valid project.json or zip file.";
}
return;
}
}
if (PROJECT.code) {
console.log("the error which is sent in json for some reason")
this.projectMessage = "Project not found.";
return;
}
if (PROJECT.objName) {
this.projectMessage = "Scratch 2.0 projects are not supported yet.";
return;
}
if (!PROJECT.targets) {
this.projectMessage = "This file doesn't seem to be a valid SB3 project.json.";
return;
}
this.projectMessage = "Loading project...";
// Ready up project metadata
this.project.meta = {};
this.project.meta.scratchVer = PROJECT.meta.semver;
this.project.meta.vmVer = PROJECT.meta.vm;
this.project.meta.browserOS = PROJECT.meta.agent;
this.project.blockCount = 0;
this.project.commentCount = 0;
// Why not
this.project.original = PROJECT;
this.project.sprites = [];
// Add all sprites to our project
for (const TARGETIDX in PROJECT.targets) {
const TARGET = PROJECT.targets[TARGETIDX];
const SPRITE = {};
SPRITE.name = TARGET.name;
SPRITE.id = TARGETIDX;
// Common variables
SPRITE.isStage = TARGET.isStage;
if (!TARGET.isStage) {
SPRITE.visible = TARGET.visible;
SPRITE.visibleMsg = SPRITE.visible ? "Yes" : "No";
SPRITE.x = TARGET.x;
SPRITE.y = TARGET.y;
SPRITE.size = TARGET.size;
SPRITE.direction = TARGET.direction;
SPRITE.draggable = TARGET.draggabble;
SPRITE.draggableMsg = SPRITE.draggable ? "Yes" : "No";
SPRITE.rotStyle = TARGET.rotationStyle;
}
SPRITE.costume = TARGET.currentCostume;
// Code
SPRITE.comments = TARGET.comments;
SPRITE.blocks = TARGET.blocks;
// Misc variables
SPRITE.volume = TARGET.volume;
SPRITE.layer = TARGET.layerOrder;
if (TARGET.isStage) {
SPRITE.tempo = TARGET.tempo;
// Video
SPRITE.vTrans = TARGET.videoTransparency;
SPRITE.vState = TARGET.videoState;
// Also broadcasts too
SPRITE.broadcasts = TARGET.broadcasts;
}
const msgComment = Object.values(SPRITE.comments).find((c)=>c.text.startsWith("PrExMsg\n"))
if (msgComment) {
SPRITE.msg = msgComment.text.substring(8);
}
if (TARGET.isStage) {
const gMsgComment = Object.values(SPRITE.comments).find((c)=>c.text.startsWith("PrExGeneralMsg\n"))
if (gMsgComment) {
this.project.generalMsg = gMsgComment.text.substring(15);
}
}
// Store count
SPRITE.blockCount = Object.keys(SPRITE.blocks).length;
SPRITE.commentCount = Object.keys(SPRITE.comments).length;
// Add to the totals as well
this.project.blockCount += SPRITE.blockCount;
this.project.commentCount += SPRITE.commentCount;
const ASSETS_URL = "https://assets.scratch.mit.edu/get_image/.%2E/";
// Costumes
SPRITE.costumes = [];
for (const COSTIDX in TARGET.costumes) {
const COST = TARGET.costumes[COSTIDX];
if (this.fileMode) {
if (!this.files[COST.md5ext]) {
this.projectMessage =
`Error in sprite "${
SPRITE.name
}": file for costume "${
COST.name
}" (${COST.md5ext}) is missing`;
return;
}
}
SPRITE.costumes.push({
name: COST.name,
md5: COST.md5ext,
format: COST.dataFormat,
isSound: false,
formatMsg:
`${
COST.dataFormat === "svg" ? "Vector" : "Bitmap"
}, ${
COST.dataFormat.toUpperCase()
}`,
url: this.fileMode ? this.files[COST.md5ext] : `${ASSETS_URL}${COST.md5ext}`,
id: COSTIDX
});
}
// Sounds
SPRITE.sounds = [];
for (const SNDIDX in TARGET.sounds) {
const SND = TARGET.sounds[SNDIDX];
if (this.fileMode) {
if (!this.files[SND.md5ext]) {
this.projectMessage =
`Error in sprite "${
SPRITE.name
}": file for sound "${
SND.name
}" (${SND.md5ext}) is missing`;
return;
}
}
SPRITE.sounds.push({
name: SND.name,
md5: SND.md5ext,
format: SND.dataFormat,
isSound: true,
formatMsg: SND.dataFormat.toUpperCase(),
url: this.fileMode ? this.files[SND.md5ext] : `${ASSETS_URL}${SND.md5ext}`,
id: SNDIDX
});
}
this.project.sprites.push(SPRITE);
}
// Finally, we are done, now we can show
// Mimic Scratch behavior: If there is at least 1 non-stage sprite, show that by default
if (this.project.sprites.length > 0) {
this.selectedSprite = 1;
} else {
this.selectedSprite = 0;
}
this.projectMessage = "Done!";
this.projectReady = 1;
}
methods.init = function() {
const loadHash = (h) => {
this.ignoreHashChange = false;
let hashText = h.substring(1);
this.idInputVal = decodeURIComponent(hashText);
this.loadURL(hashText);
}
window.addEventListener("hashchange", () => {
if (this.ignoreHashChange) {
this.ignoreHashChange = false;
return;
}
loadHash(location.hash);
}, true);
if (window.location.hash && window.location.hash !== "#file") {
loadHash(window.location.hash);
} else {
this.idInputVal = "";
methods.showAboutScreen();
}
};
methods.setHash = function(hash) {
this.ignoreHashChange = false;
window.location.hash = "#" + encodeURIComponent(hash);
};
methods.filePicker = function() {
const input = document.getElementById("upload-input");
if (!input) return;
input.click();
};
methods.uploadFile = function() {
const input = document.getElementById("upload-input");
if (!input) return;
this.ignoreHashChange = true;
location.hash = "#file";
this.idInputVal = "";
const file = input.files[0];
input.value = null;
this.projectReady = 0;
this.projectMessage = "Loading file...";
this.projTitle = file.name;
document.title = file.name + " - Project Explorer";
this.loadFile(file);
};
export default methods;