-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.html
580 lines (486 loc) · 23.3 KB
/
test.html
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="/api/v1/dist/protobufSchema.js"></script>
<script src="/api/v1/dist/jszip.js"></script>
<script src="/api/v1/dist/pbf.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.0/FileSaver.min.js" integrity="sha512-csNcFYJniKjJxRWRV1R7fvnXrycHP6qDR21mgz1ZP55xY5d+aHLfo9/FcGDQLfn2IfngbAHd8LdfsagcCqgTcQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
<input id="username" type="text" placeholder="Username">
<input id="password" type="password" placeholder="Password">
<button onclick="createAccount()">Create Account</button>
<a href="/api/v1/users/createoauthaccountlocal?method=scratch">Create account with Scratch</a>
<a href="/api/v1/users/loginoauthaccountlocal?method=scratch">Login with Scratch</a>
<button onclick="addScratchMethod()">Add scratch login method</button>
<br><br>
<input id="vp-username" type="text" placeholder="Username">
<input id="vp-password" type="password" placeholder="Password">
<button onclick="verifyPasswordAndUsername()">Verify password</button>
<br><br>
<input type="text" id="usrname"> username <br>
<input type="text" id="token"> token <br>
<button onclick="verifyTokenAndUsername()">Verify token</button>
<br>
<input type="file" id="jsonConvertInput"/> json <br>
<button onclick="getProtobuf()">Convert to Protobuf</button>
<div>
<input type="file" id="assetsInput[]" multiple/> assets <br>
<input type="file" id="jsonInput"/> json <br>
<input type="file" id="thumbnailInput"/> thumbnail <br>
<input type="text" id="projectTitle" placeholder="Project Title"> <br>
<button onclick="uploadFileButton()">Upload</button>
</div>
<br>
<button onclick="getProjects()">Get Projects</button>
<br>
<input type="number" id="projectId" placeholder="Project ID">
<button onclick="getProject()">Get Project</button>
<br><br>
<!-- add password to oauth account -->
<input type="text" id="apoa-username"> username <br>
<input type="text" id="apoa-token"> token <br>
<button onclick="addPasswordToOAuthAccount()">Add password to OAuth account</button>
<script>
// protobuf object is "Project"
let token = "";
let username = "";
async function verifyTokenAndUsername() {
let usrnm = document.getElementById('usrname').value;
let tkn = document.getElementById('token').value;
let res = await fetch(`/api/v1/users/tokenlogin?username=${usrnm}&token=${tkn}`)
if (res.status === 200) {
alert("Token and username are valid");
} else {
alert("Token and username are invalid");
}
}
async function verifyPasswordAndUsername() {
let usrnm = document.getElementById('vp-username').value;
let password = document.getElementById('vp-password').value;
let res = await fetch(`/api/v1/users/passwordlogin?username=${usrnm}&password=${password}`)
if (res.status === 200) {
alert("Password and username are valid");
} else {
alert("Password and username are invalid");
}
}
function addScratchMethod() {
if (!token || !username) {
alert("Please create an account first");
return;
}
window.location.href = `/api/v1/users/addoauthmethodlocal?method=scratch&username=${username}&token=${token}`
}
function createAccount() {
username = document.getElementById('username').value;
let password = document.getElementById('password').value;
(async () => {
fetch("/api/v1/users/createAccount", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username: username,
password: password
})
})
.then(res => res.json())
.then(res => {
token = res.token;
console.log("created account");
})
})();
}
function uploadFileButton() {
let json = document.getElementById("jsonInput").files[0];
let thumbnail = document.getElementById("thumbnailInput").files[0];
let assets = document.getElementById("assetsInput[]").files;
let title = document.getElementById("projectTitle").value;
if (json && thumbnail && assets) {
uploadFile([json, thumbnail, ...assets], title);
} else {
alert("Please select all files");
}
}
function castToString(value) {
if (typeof value !== "object") {
return String(value);
} else {
return JSON.stringify(value);
}
}
const uploadFile = async (files, title) => {
if (!token) {
alert("Please create an account first");
return;
}
console.log("Uploading file...");
const API_ENDPOINT = `/api/v1/projects/uploadProject?username=${username}&token=${token}&title=${title}`;
const request = new XMLHttpRequest();
const formData = new FormData();
request.open("POST", API_ENDPOINT, true);
request.onload = () => {
console.log(request.response);
console.log("success probably");
};
for (let i = 0; i < files.length - 2; i++) {
formData.append("assets", files[i+2])
}
formData.append("jsonFile", files[0]);
formData.append("thumbnail", files[1]);
request.send(formData);
console.log("sent");
};
function jsonToProtobuf(json) {
let newjson = {
targets: [],
monitors: [],
extensionData: {},
extensions: json.extensions,
extensionURLs: {},
metaSemver: "",
metaVm: "",
metaAgent: ""
}
newjson.metaSemver = json.meta.semver;
newjson.metaVm = json.meta.vm;
newjson.metaAgent = json.meta.agent;
for (const target in json.targets) {
let newtarget = {
id: json.targets[target].id,
isStage: json.targets[target].isStage,
name: json.targets[target].name,
variables: {},
lists: {},
broadcasts: {},
customVars: [],
blocks: {},
comments: {},
currentCostume: json.targets[target].currentCostume,
costumes: [],
sounds: [],
volume: json.targets[target].volume,
layerOrder: json.targets[target].layerOrder,
x: json.targets[target].x,
y: json.targets[target].y,
size: json.targets[target].size,
direction: json.targets[target].direction,
draggable: json.targets[target].draggable,
rotationStyle: json.targets[target].rotationStyle,
tempo: json.targets[target].tempo,
videoTransparency: json.targets[target].videoTransparency,
videoState: json.targets[target].videoState,
textToSpeechLanguage: json.targets[target].textToSpeechLanguage,
visible: json.targets[target].visible,
}
// loop over the variables
for (const variable in json.targets[target].variables) {
newtarget.variables[variable] = {
name: json.targets[target].variables[variable][0],
value: castToString(json.targets[target].variables[variable][1])
}
}
// loop over the lists
for (const list in json.targets[target].lists) {
newtarget.lists[list] = {
name: list,
value: json.targets[target].lists[list].map(x => castToString(x))
}
}
// loop over the broadcasts
for (const broadcast in json.targets[target].broadcasts) {
newtarget.broadcasts[broadcast] = json.targets[target].broadcasts[broadcast];
}
// loop over the customVars
for (const customVar in json.targets[target].customVars) {
newtarget.customVars.push(json.targets[target].customVars[customVar]);
}
const blocks = json.targets[target].blocks;
// loop over the blocks
for (const block in blocks) {
newtarget.blocks[block] = {
opcode: blocks[block].opcode,
next: blocks[block].next,
parent: blocks[block].parent,
inputs: {},
fields: {},
shadow: blocks[block].shadow,
topLevel: blocks[block].topLevel,
x: blocks[block].x,
y: blocks[block].y
}
if (blocks[block].mutation) {
newtarget.blocks[block].mutation = {
tagName: blocks[block].mutation.tagName,
proccode: blocks[block].mutation.proccode,
argumentids: blocks[block].mutation.argumentids,
argumentnames: blocks[block].mutation.argumentnames,
argumentdefaults: blocks[block].mutation.argumentdefaults,
warp: blocks[block].mutation.warp,
_returns: blocks[block].mutation.returns,
edited: blocks[block].mutation.edited,
optype: blocks[block].mutation.optype,
color: blocks[block].mutation.color
}
}
// loop over the inputs
for (const input in blocks[block].inputs) {
newtarget.blocks[block].inputs[input] = JSON.stringify(blocks[block].inputs[input]);
}
// loop over the fields
for (const field in blocks[block].fields) {
newtarget.blocks[block].fields[field] = JSON.stringify(blocks[block].fields[field]);
}
}
// loop over the comments
for (const comment in json.targets[target].comments) {
newtarget.comments[comment] = {
blockId: json.targets[target].comments[comment].blockId,
x: json.targets[target].comments[comment].x,
y: json.targets[target].comments[comment].y,
width: json.targets[target].comments[comment].width,
height: json.targets[target].comments[comment].height,
minimized: json.targets[target].comments[comment].minimized,
text: json.targets[target].comments[comment].text
}
}
// loop over the costumes
for (const costume in json.targets[target].costumes) {
newtarget.costumes[costume] = {
assetId: json.targets[target].costumes[costume].assetId,
name: json.targets[target].costumes[costume].name,
bitmapResolution: json.targets[target].costumes[costume].bitmapResolution,
rotationCenterX: json.targets[target].costumes[costume].rotationCenterX,
rotationCenterY: json.targets[target].costumes[costume].rotationCenterY,
md5ext: json.targets[target].costumes[costume].md5ext,
dataFormat: json.targets[target].costumes[costume].dataFormat,
}
}
// loop over the sounds
for (const sound in json.targets[target].sounds) {
newtarget.sounds[sound] = {
assetId: json.targets[target].sounds[sound].assetId,
name: json.targets[target].sounds[sound].name,
dataFormat: json.targets[target].sounds[sound].dataFormat,
rate: json.targets[target].sounds[sound].rate,
sampleCount: json.targets[target].sounds[sound].sampleCount,
md5ext: json.targets[target].sounds[sound].md5ext
}
}
newjson.targets.push(newtarget);
}
// loop over the monitors
for (const monitor in json.monitors) {
newjson.monitors.push({
id: json.monitors[monitor].id,
mode: json.monitors[monitor].mode,
opcode: json.monitors[monitor].opcode,
params: json.monitors[monitor].params,
spriteName: json.monitors[monitor].spriteName,
value: String(json.monitors[monitor].value),
width: json.monitors[monitor].width,
height: json.monitors[monitor].height,
x: json.monitors[monitor].x,
y: json.monitors[monitor].y,
visible: json.monitors[monitor].visible,
sliderMin: json.monitors[monitor].sliderMin,
sliderMax: json.monitors[monitor].sliderMax,
isDiscrete: json.monitors[monitor].isDiscrete,
});
}
// loop over the extensionData
for (const extensionData in json.extensionData) {
newjson.extensionData[extensionData] = castToString(json.extensionData[extensionData]);
}
// loop over the extensionURLs
for (const extensionURL in json.extensionURLs) {
newjson.extensionURLs[extensionURL] = json.extensionURLs[extensionURL];
}
const pbf = new Pbf();
Project.write(newjson, pbf);
return pbf.finish();
}
function protobufToJson(buffer) {
const json = Project.read(buffer);
const newJson = {
targets: [],
monitors: [],
extensionData: {},
extensions: json.extensions,
extensionURLs: {},
meta: {
semver: json.metaSemver,
vm: json.metaVm,
agent: json.metaAgent || ""
}
};
for (const target of json.targets) {
let newTarget = {
isStage: target.isStage,
name: target.name,
variables: {},
lists: {},
broadcasts: {},
customVars: [],
blocks: {},
comments: {},
currentCostume: target.currentCostume,
costumes: [],
sounds: [],
id: target.id,
volume: target.volume,
layerOrder: target.layerOrder,
tempo: target.tempo,
videoTransparency: target.videoTransparency,
videoState: target.videoState,
textToSpeechLanguage: target.textToSpeechLanguage || null,
visible: target.visible,
x: target.x,
y: target.y,
size: target.size,
direction: target.direction,
draggable: target.draggable,
rotationStyle: target.rotationStyle
};
for (const variable in target.variables) {
newTarget.variables[variable] = [target.variables[variable].name, target.variables[variable].value];
}
for (const list in target.lists) {
newTarget.lists[list] = [target.lists[list].name, target.lists[list].value];
}
for (const broadcast in target.broadcasts) {
newTarget.broadcasts[broadcast] = target.broadcasts[broadcast];
}
for (const customVar in target.customVars) {
newTarget.customVars.push(target.customVars[customVar]);
}
for (const block in target.blocks) {
newTarget.blocks[block] = {
opcode: target.blocks[block].opcode,
next: target.blocks[block].next || null,
parent: target.blocks[block].parent || null,
inputs: {},
fields: {},
shadow: target.blocks[block].shadow,
topLevel: target.blocks[block].topLevel,
x: target.blocks[block].x,
y: target.blocks[block].y
}
if (target.blocks[block].mutation) {
newTarget.blocks[block].mutation = {
tagName: target.blocks[block].mutation.tagName,
proccode: target.blocks[block].mutation.proccode,
argumentids: target.blocks[block].mutation.argumentids,
argumentnames: target.blocks[block].mutation.argumentnames,
argumentdefaults: target.blocks[block].mutation.argumentdefaults,
warp: target.blocks[block].mutation.warp,
returns: target.blocks[block].mutation._returns,
edited: target.blocks[block].mutation.edited,
optype: target.blocks[block].mutation.optype,
color: target.blocks[block].mutation.color,
children: []
}
}
for (const input in target.blocks[block].inputs) {
newTarget.blocks[block].inputs[input] = JSON.parse(target.blocks[block].inputs[input]);
}
for (const field in target.blocks[block].fields) {
newTarget.blocks[block].fields[field] = JSON.parse(target.blocks[block].fields[field]);
}
}
for (const comment in target.comments) {
newTarget.comments[comment] = target.comments[comment];
}
for (const costume in target.costumes) {
newTarget.costumes[costume] = target.costumes[costume];
}
for (const sound in target.sounds) {
newTarget.sounds[sound] = target.sounds[sound];
}
newJson.targets.push(newTarget);
}
for (const monitor in json.monitors) {
let newMonitor = {
id: json.monitors[monitor].id,
mode: json.monitors[monitor].mode,
opcode: json.monitors[monitor].opcode,
params: json.monitors[monitor].params,
spriteName: json.monitors[monitor].spriteName || null,
value: json.monitors[monitor].value,
width: json.monitors[monitor].width,
height: json.monitors[monitor].height,
x: json.monitors[monitor].x,
y: json.monitors[monitor].y,
visible: json.monitors[monitor].visible,
sliderMin: json.monitors[monitor].sliderMin,
sliderMax: json.monitors[monitor].sliderMax,
isDiscrete: json.monitors[monitor].isDiscrete
}
newJson.monitors.push(newMonitor);
}
for (const extensionData in json.extensionData) {
newJson.extensionData[extensionData] = JSON.parse(json.extensionData[extensionData]);
}
for (const extensionURL in json.extensionURLs) {
newJson.extensionURLs[extensionURL] = json.extensionURLs[extensionURL];
}
return newJson;
}
async function getProject() {
// get the id of whatever project, get the protobuf, turn it to json, get the assets, zip it all up, then download
const projectId = document.getElementById("projectId").value;
let protobuf = await fetch(`/api/v1/projects/getProject?projectId=${projectId}&requestType=protobuf`)
.then(res => res.arrayBuffer())
.then(res => new Uint8Array(res))
.then(res => new Pbf(res));
let assets = await fetch(`/api/v1/projects/getProject?projectId=${projectId}&requestType=assets`)
.then(res => res.json());
let json = protobufToJson(protobuf);
let zip = new JSZip();
zip.file("project.json", JSON.stringify(json));
for (const asset of assets) {
zip.file(asset.name, asset.data);
}
zip.generateAsync({type:"blob"})
.then(function(content) {
saveAs(content, "project.pmp");
});
}
async function getProjects() {
let projects = await fetch("/api/v1/projects/getProjects")
.then(res => res.json());
console.log(projects.map(project => project.id));
}
function getProtobuf() {
let file = document.getElementById("jsonConvertInput").files[0];
if (file) {
let reader = new FileReader();
reader.onload = function(e) {
let json = JSON.parse(e.target.result);
let protobuf = jsonToProtobuf(json);
saveAs(new Blob([protobuf], {type: "application/octet-stream"}), "project.pb");
}
reader.readAsText(file);
} else {
alert("Please select a file");
}
}
function addPasswordToOAuthAccount() {
let usrnm = document.getElementById('apoa-username').value;
let tkn = document.getElementById('apoa-token').value;
window.location.href = `/api/v1/users/addpasswordtooauthlocal?method=scratch&username=${usrnm}&token=${tkn}`;
}
</script>
<style>
body {
background-color: #010101;
color: white;
}
</style>
</body>
</html>