-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.html
645 lines (586 loc) · 20.5 KB
/
index.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="referrer" content="no-referrer">
<link rel="canonical" href="https://forkphorus.github.io/sb-downloader/">
<meta name="description" content="Download projects from Scratch. Supports Scratch 1 (sb), Scratch 2 (sb2), and Scratch 3 (sb3). Includes JavaScript API and CLI.">
<title>.sb downloader</title>
<style>
main {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
width: 100%;
max-width: 568px;
margin: 24px auto;
}
h1 {
text-align: center;
font-size: 54px;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
background-image: linear-gradient(to right, #4C97FF, #d65cd6, #ff8c1a);
}
@media (prefers-color-scheme: dark) {
h1 {
background-image: linear-gradient(to right, #4cbfe6, #d65cd6, #ff8c1a);
}
}
h1, h2 {
font-weight: normal;
}
h1, h2, p {
margin: 16px 0;
}
noscript {
font-weight: bold;
}
@media (prefers-color-scheme: dark) {
:root {
color-scheme: dark;
background-color: #111;
color: #eee;
}
a {
color: #4af;
}
a:visited {
color: rgb(255, 153, 255);
}
a:active {
color: rgb(247, 80, 80);
}
}
.project-input-outer {
margin: 16px 0;
font-size: 26px;
line-height: 32px;
}
@media (max-width: 520px) {
.project-input-outer {
font-size: 22px;
}
}
@media (max-width: 450px) {
.project-input-outer {
font-size: 20px;
}
}
@media (max-width: 400px) {
.project-input-outer {
font-size: 18px;
}
}
.project-input {
border: none;
background: transparent;
font: inherit;
width: 100%;
opacity: 0.6;
box-sizing: border-box;
}
.project-input:focus {
opacity: 1;
}
.project-input {
color: inherit;
}
.download-buttons {
margin: 16px 0;
display: flex;
align-items: center;
flex-direction: column;
}
.download-button {
color: white;
border: none;
padding: 8px 16px;
font: inherit;
font-weight: bold;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
border-style: solid;
border-width: 1px;
background: #4C97FF;
border-color: #237eff;
}
.download-button:hover {
opacity: 0.9;
}
.download-button:active {
opacity: 0.8;
}
.download-icon {
width: 24px;
height: 24px;
margin-right: 8px;
/* Icon from https://fonts.google.com/icons?selected=Material%20Symbols%20Outlined%3Adownload%3AFILL%400%3Bwght%40400%3BGRAD%400%3Bopsz%4048 */
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><path fill="white" d="M11 40q-1.2 0-2.1-.9Q8 38.2 8 37v-7.15h3V37h26v-7.15h3V37q0 1.2-.9 2.1-.9.9-2.1.9Zm13-7.65-9.65-9.65 2.15-2.15 6 6V8h3v18.55l6-6 2.15 2.15Z"/></svg>');
}
.download-button small {
font-weight: normal;
}
.progress-outer {
margin: 8px 0;
position: relative;
width: 100%;
box-sizing: border-box;
}
.progress-inner {
color: rgb(0, 242, 255);
transition: .2s color;
}
.progress-outer[data-state="done"] .progress-inner {
color: rgb(30, 255, 0);
}
.progress-outer[data-state="error"] .progress-inner {
color: rgb(255, 0, 0);
}
.progress-inner, .progress-fill, .progress-border {
z-index: -1;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
box-sizing: border-box;
}
.progress-fill {
width: 10%;
transition: .2s width;
background-color: currentColor;
opacity: 0.35;
}
.progress-border {
border: 1px solid currentColor;
opacity: 0.8;
}
.progress-text {
height: 100%;
display: flex;
align-items: center;
padding: 2px 4px;
min-height: 35px;
}
.downloads-outer {
margin: 8px 0;
text-align: center;
}
.asset-viewer-option-outer, .asset-viewer-option-outer > label {
cursor: pointer;
display: block;
width: 100%;
}
.asset-viewer-outer {
margin: 16px 0;
border-collapse: collapse;
}
.asset-viewer-outer, .asset-viewer-outer td, .asset-viewer-outer th {
border: 1px solid rgba(0, 0, 0, 0.5);
margin: 0;
padding: 0;
}
@media (prefers-color-scheme: dark) {
.asset-viewer-outer, .asset-viewer-outer td, .asset-viewer-outer th {
border-color: rgba(255, 255, 255, 0.5);
}
}
.asset-viewer-id {
width: 85px;
min-width: 85px;
}
td.asset-viewer-id {
font-size: small;
text-align: center;
word-wrap: anywhere;
}
.asset-viewer-content {
box-sizing: border-box;
width: 480px;
height: 100%;
text-align: center;
}
.asset-viewer-content > * {
margin: 0;
padding: 0;
box-sizing: border-box;
max-width: 100%;
border: none;
}
.asset-viewer-content > textarea {
display: block;
min-width: 100%;
width: 100%;
height: 125px;
}
.asset-viewer-content > audio {
width: 100%;
display: block;
}
.asset-viewer-content > img {
max-height: 360px;
}
.asset-viewer-row:hover img {
outline: 1px solid currentColor;
}
[hidden] {
display: none;
}
</style>
</head>
<body>
<main>
<h1>.sb downloader</h1>
<noscript>
<p>This site requires JavaScript.</p>
</noscript>
<p>
.sb downloader downloads <a href="https://scratch.mit.edu/">Scratch</a> 1 (sb), Scratch 2 (sb2), or Scratch 3 (sb3) projects.
Just paste the project's ID or URL into the input below and press the download button.
</p>
<div class="project-input-outer">
<input class="project-input" value="https://scratch.mit.edu/projects/" spellcheck="false" autocomplete="off">
<script>
// The script down below will take a bit to load. We have this separate script so the ID's input
// will have the correct state immediately.
window.InitialState = (function() {
/** @param {HTMLInputElement} el */
const moveCursorToEnd = (el) => {
el.selectionStart = el.value.length;
el.selectionEnd = el.value.length;
};
const projectInput = document.querySelector('.project-input');
const initialUrlParams = new URLSearchParams(location.search);
const initialProjectId = initialUrlParams.get('id');
// ?type=sb, ?type=sb2, ?type=sb3 is an old parameter
const initialType = initialUrlParams.get('type');
if (/^\d+$/.test(initialProjectId)) {
// We have an initial project ID
projectInput.value += initialProjectId;
return true;
}
// No initial project ID
projectInput.focus();
moveCursorToEnd(projectInput);
return null;
})();
</script>
</div>
<div class="download-buttons">
<button class="download-button">
<div class="download-icon"></div>
<div>Download project</div>
</button>
</div>
<div class="progress-outer" hidden>
<div class="progress-inner">
<div class="progress-border"></div>
<div class="progress-fill"></div>
</div>
<div class="progress-text">⏳ Loading scripts...</div>
</div>
<script>
(function() {
const progressOuter = document.querySelector('.progress-outer');
if (window.InitialState) {
progressOuter.hidden = false;
}
})();
</script>
<div class="downloads-outer"></div>
<h2>Unshared projects</h2>
<p>
Unshared projects are no longer accessible using this tool due to Scratch API changes.
<a href="https://docs.turbowarp.org/unshared-projects">More information</a>.
</p>
<h2>Legacy version of projects</h2>
<p>In August 2023, Scratch disabled the API to download the legacy version of projects.</p>
<h2>Asset viewer</h2>
<p class="asset-viewer-placeholder">
Load a project first to use the asset viewer.
</p>
<p class="asset-viewer-option-outer" hidden>
<label>
<input type="checkbox" class="asset-viewer-option" autocomplete="off">
Enable asset viewer
</label>
</p>
<p class="asset-viewer-not-supported" hidden>
Scratch 1 projects are not supported in the asset viewer.
</p>
<table class="asset-viewer-outer" hidden>
<thead>
<tr>
<th class="asset-viewer-id">ID</th>
<th class="asset-viewer-content">Contents</th>
</tr>
</thead>
<tbody class="asset-viewer-inner"></tbody>
</table>
<h2>Code</h2>
<p>
.sb downloader is <a href="https://github.com/forkphorus/sb-downloader">open source</a>.
There is an easy-to-use API.
</p>
<h2>Credits</h2>
<p>
.sb downloader is of course powered by the <a href="https://scratch.mit.edu/">Scratch</a> API.
The <a href="https://stuk.github.io/jszip/">JSZip</a> library is used for creating zip archives.
</p>
<h2>Privacy</h2>
<p>
In order to access the project token and title, .sb downloader may send the project ID to a server under our control as it can't directly access certain Scratch APIs.
The ID may be recorded for up to 24 hours for caching purposes only.
</p>
</main>
<script type="module">
import JSZip from 'jszip';
import * as SBDL from './src/export-web.js';
window.SBDL = SBDL;
/**
* @param {string} text
* @returns {string|null}
*/
const extractProjectId = (text) => {
const match = text.match(/\d+/);
if (match) {
return match[0];
}
return null;
};
/**
* @param {URLSearchParams} params
*/
const replaceParameters = (params) => {
const stringified = params.toString();
// If there are no parameters, remove the query entirely
if (stringified) {
history.replaceState('', '', `?${stringified}`);
} else {
history.replaceState('', '', location.pathname);
}
};
const projectInput = document.querySelector('.project-input');
const downloadLatestButton = document.querySelector('.download-button');
const progressOuter = document.querySelector('.progress-outer');
const progressFill = document.querySelector('.progress-fill');
const progressText = document.querySelector('.progress-text');
const downloadsOuter = document.querySelector('.downloads-outer');
const assetViewerPlaceholder = document.querySelector('.asset-viewer-placeholder');
const assetViewerOptionOuter = document.querySelector('.asset-viewer-option-outer');
const assetViewerOption = document.querySelector('.asset-viewer-option');
const assetViewerNotSupported = document.querySelector('.asset-viewer-not-supported');
const assetViewerOuter = document.querySelector('.asset-viewer-outer');
const SCRATCH_PREFIX = 'https://scratch.mit.edu/projects/';
const PROGRESS_STATE_WORKING = 'working';
const PROGRESS_STATE_DONE = 'done';
const PROGRESS_STATE_ERROR = 'error';
const ASSET_VIEWER_PROJECT_TYPES = ['sb2', 'sb3'];
projectInput.addEventListener('input', (e) => {
removeDownloadLinks();
hideProgress();
resetAssetViewer();
cancelOngoingDownload();
replaceParameters(new URLSearchParams());
const projectId = extractProjectId(e.target.value);
e.target.value = projectId ? `${SCRATCH_PREFIX}${projectId}` : SCRATCH_PREFIX;
});
projectInput.addEventListener('focus', (e) => {
const projectId = extractProjectId(e.target.value);
if (projectId) {
e.target.select();
}
});
projectInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.target.blur();
downloadProject(false);
}
});
downloadLatestButton.addEventListener('click', () => {
downloadProject(false);
});
const showAssetViewer = async (project) => {
const TEXT_EXTENSIONS = ['json'];
const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'bmp', 'webp'];
const SOUND_EXTENSIONS = ['mp3', 'wav', 'ogg'];
removeAssetViewerItems();
assetViewerOuter.hidden = false;
const assetViewerInner = document.createElement('tbody');
assetViewerInner.className = 'asset-viewer-inner';
assetViewerOuter.appendChild(assetViewerInner);
const zip = await JSZip.loadAsync(project.arrayBuffer);
for (const filename of Object.keys(zip.files)) {
const rowElement = document.createElement('tr');
rowElement.className = 'asset-viewer-row';
const idElement = document.createElement('td');
idElement.textContent = filename;
idElement.className = 'asset-viewer-id';
const contentElement = document.createElement('td');
contentElement.className = 'asset-viewer-content';
rowElement.appendChild(idElement);
rowElement.appendChild(contentElement);
const extension = filename.split('.').pop();
if (TEXT_EXTENSIONS.includes(extension)) {
const text = await zip.file(filename).async('text');
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.readOnly = true;
contentElement.appendChild(textarea);
} else if (IMAGE_EXTENSIONS.includes(extension)) {
const base64 = await zip.file(filename).async('base64');
const type = extension === 'svg' ? 'image/svg+xml' : `image/${extension}`;
const image = document.createElement('img');
image.src = `data:${type};base64,${base64}`;
contentElement.appendChild(image);
} else if (SOUND_EXTENSIONS.includes(extension)) {
const base64 = await zip.file(filename).async('base64');
const audio = document.createElement('audio');
audio.src = `data:;base64,${base64}`;
audio.controls = true;
audio.autoplay = false;
contentElement.appendChild(audio);
} else {
const errorMessage = document.createElement('div');
errorMessage.textContent = `Unknown extension: ${extension}`;
}
assetViewerInner.appendChild(rowElement);
}
};
const removeAssetViewerItems = () => {
const assetViewerInner = document.querySelector('.asset-viewer-inner');
if (assetViewerInner) {
// If the asset viewer is still being created, this won't stop its CPU work, but the result
// won't be displayed.
assetViewerInner.remove();
}
assetViewerOuter.hidden = true;
};
const resetAssetViewer = () => {
assetViewerOption.checked = false;
assetViewerPlaceholder.hidden = false;
assetViewerOptionOuter.hidden = true;
assetViewerNotSupported.hidden = true;
removeAssetViewerItems();
};
assetViewerOption.addEventListener('change', (e) => {
if (e.target.checked) {
showAssetViewer(mostRecentProject);
} else {
removeAssetViewerItems();
}
});
const removeDownloadLinks = () => {
while (downloadsOuter.firstChild) {
const link = downloadsOuter.firstChild;
if (link.href) {
URL.revokeObjectURL(link.href);
}
link.remove();
}
};
const setProgress = (text, state, progress) => {
progressOuter.hidden = false;
progressFill.style.width = `${10 + progress * 90}%`;
progressOuter.dataset.state = state;
if (progressText.textContent !== text) {
progressText.textContent = text;
}
};
const resetProgress = () => {
setProgress('⏳ Downloading project metadata...', PROGRESS_STATE_WORKING, 0);
};
const hideProgress = () => {
progressOuter.hidden = true;
};
let mostRecentProject = null;
/** @type {AbortController} */
let globalAbortController = null;
const cancelOngoingDownload = () => {
if (globalAbortController) {
globalAbortController.abort();
globalAbortController = null;
}
};
const downloadProject = async () => {
const projectId = extractProjectId(projectInput.value);
if (!projectId) {
alert('Paste a project ID or URL into the input above first.');
return;
}
try {
cancelOngoingDownload();
const localAbortController = new AbortController();
globalAbortController = localAbortController;
const urlParams = new URLSearchParams();
urlParams.set('id', projectId);
replaceParameters(urlParams);
mostRecentProject = null;
resetAssetViewer();
removeDownloadLinks();
resetProgress();
const progressCallback = (type, loaded, total) => {
let text = type;
if (type === 'metadata') {
text = '⏳ Downloading project metadata...'
} else if (type === 'project') {
text = '⏳ Downloading project data...';
} else if (type === 'assets') {
text = `⏳ Downloading assets (${loaded}/${total})...`;
} else if (type === 'compress') {
text = '⏳ Compressing project...';
} else {
console.warn(`Unknown progress type: ${type}`);
}
setProgress(text, PROGRESS_STATE_WORKING, loaded / total);
};
const options = {
onProgress: progressCallback,
signal: localAbortController.signal
};
const project = await SBDL.downloadProjectFromID(projectId, options);
mostRecentProject = project;
const title = project.title ? `${project.title} (${projectId})` : projectId;
const type = project.type;
const name = `${title}.${type}`;
const blob = new Blob([project.arrayBuffer], {
type: `application/x.scratch.${type}`
});
const sizeMB = (blob.size / 1024 / 1024).toFixed(2);
const url = URL.createObjectURL(blob);
const downloadElement = document.createElement('a');
downloadElement.textContent = `Download ${name} (${sizeMB}MB)`;
downloadElement.download = name;
downloadElement.href = url;
downloadsOuter.appendChild(downloadElement);
downloadElement.click();
const assetViewerSupported = ASSET_VIEWER_PROJECT_TYPES.includes(type);
assetViewerPlaceholder.hidden = true;
assetViewerOptionOuter.hidden = !assetViewerSupported;
assetViewerNotSupported.hidden = assetViewerSupported;
setProgress('✅ Done', PROGRESS_STATE_DONE, 1);
globalAbortController = null;
} catch (e) {
console.error(e);
if (e && e.name === 'AbortError') {
return;
}
let errorMessage = '❌ ';
if (e && e.name === 'CanNotAccessProjectError') {
errorMessage += 'The project does not exist, the ID is invalid, or your network blocks turbowarp.org.';
} else {
errorMessage += e;
}
setProgress(errorMessage, PROGRESS_STATE_ERROR, 1);
}
};
if (window.InitialState) {
downloadProject();
}
</script>
</body>
</html>