mirror of
https://github.com/BrenBroZAYT/ScratchYTOG.github.io.git
synced 2026-06-13 16:40:02 +00:00
529 lines
16 KiB
HTML
529 lines
16 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
|
|
<head>
|
|
<title>.sb downloader (Scratch Downloader By BrenBroZAYT)</title>
|
|
<meta charset="utf8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="referrer" content="no-referrer">
|
|
<style>
|
|
/* basic styles to make things look nicer */
|
|
body {
|
|
font-family: sans-serif;
|
|
width: 480px;
|
|
margin: 24px auto;
|
|
}
|
|
p {
|
|
margin: 8px 0 16px 0;
|
|
}
|
|
h1 {
|
|
font-size: 54px;
|
|
line-height: 72px;
|
|
margin: 0;
|
|
}
|
|
h2 {
|
|
font-size: 24px;
|
|
line-height: 32px;
|
|
margin: 16px 0 0 0;
|
|
}
|
|
h1, h2 {
|
|
font-weight: normal;
|
|
}
|
|
a {
|
|
color: #FFFF00;
|
|
text-decoration: underline;
|
|
cursor: pointer;
|
|
}
|
|
a:visited {
|
|
color: #73c;
|
|
}
|
|
a:active {
|
|
color: #03a;
|
|
}
|
|
code {
|
|
font-family: Menlo, Monaco, Consolas, Courier New, monospace;
|
|
background: #f1f1f1;
|
|
border-radius: 3px;
|
|
margin: 3px;
|
|
color: #000;
|
|
}
|
|
|
|
/* project ID input */
|
|
#project-select {
|
|
font-size: 24px;
|
|
line-height: 32px;
|
|
border: none;
|
|
width: 100%;
|
|
color: rgba(0, 0, 0, 0.4);
|
|
}
|
|
#project-select:focus {
|
|
color: rgba(0, 0, 0, 1);
|
|
}
|
|
#project-select:disabled {
|
|
background: white;
|
|
}
|
|
|
|
/* project type input */
|
|
#type-container {
|
|
width: 100%;
|
|
text-align: center;
|
|
}
|
|
|
|
:disabled {
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
/* progress bar */
|
|
#progress-bar {
|
|
position: relative;
|
|
width: 100%;
|
|
height: 35px;
|
|
margin-top: 10px;
|
|
}
|
|
#progress-bar-fill {
|
|
position: absolute;
|
|
height: 100%;
|
|
top: 0;
|
|
bottom: 0;
|
|
left: 0;
|
|
width: 0;
|
|
background-color: #cde;
|
|
transition: background-color 0.3s, width 0.3s;
|
|
}
|
|
#progress-bar-fill[state=error] {
|
|
background-color: #eaa;
|
|
}
|
|
#progress-bar-fill[state=success] {
|
|
background-color: #aea;
|
|
}
|
|
#progress-bar-text {
|
|
position: absolute;
|
|
top: 50%;
|
|
left: 5px;
|
|
transform: translateY(-50%);
|
|
z-index: 10;
|
|
}
|
|
|
|
/* asset list */
|
|
#asset-list-container {
|
|
margin-top: 10px;
|
|
}
|
|
#asset-list .name {
|
|
font-size: 10px;
|
|
}
|
|
#asset-list .preview {
|
|
max-width: 360px;
|
|
}
|
|
#asset-list {
|
|
text-align: center;
|
|
}
|
|
#asset-list:empty:after {
|
|
content: 'Nothing to preview';
|
|
font-style: italic;
|
|
}
|
|
#asset-list tr:hover td > * {
|
|
outline: 1px solid black;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<h1><code>.sb</code> downloader</h1>
|
|
<p><code>.sb</code> downloader downloads <a href="https://scratch.mit.edu/">Scratch</a> 1, 2, or 3 projects. Enter the project ID or URL then choose the format.</p>
|
|
|
|
<input id="project-select" value="https://scratch.mit.edu/projects/">
|
|
|
|
<div id="type-container">
|
|
<b>Download project as</b>
|
|
<button class="download-button" data-type="sb">.sb</button>
|
|
<button class="download-button" data-type="sb2">.sb2</button>
|
|
<button class="download-button" data-type="sb3">.sb3</button>
|
|
</div>
|
|
|
|
<div id="progress-bar" hidden>
|
|
<div id="progress-bar-fill"></div>
|
|
<div id="progress-bar-text"></div>
|
|
</div>
|
|
|
|
<div id="download-link"></div>
|
|
|
|
<h2>Asset Viewer</h2>
|
|
<label>
|
|
<input type="checkbox" id="asset-list-toggle"> Enable asset viewer
|
|
</label>
|
|
<div id="asset-list-container" hidden>
|
|
<i><small>Some projects cannot be previewed.</small></i>
|
|
<table width="100%">
|
|
<thead>
|
|
<tr>
|
|
<th width="50%">Name</th>
|
|
<th width="50%">Preview</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="asset-list">
|
|
<!-- javascript will insert the assets here -->
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<h2>Code</h2>
|
|
<p><code>.sb</code> downloader is <a href="https://github.com/forkphorus/sb-downloader">open source</a>.</p>
|
|
|
|
<h2>Credits</h2>
|
|
<p>Thanks to <a href="https://scratch.mit.edu/">Scratch</a> for providing a sufficient public API. The general look of the downloader is inspired by <a href="https://phosphorus.github.io/">phosphorus</a>. The <a href="https://stuk.github.io/jszip/">JSZip</a> library is used for creating zip archives.</p>
|
|
|
|
<script src="jszip.min.js"></script>
|
|
<script src="loader.js"></script>
|
|
<script>
|
|
(function() {
|
|
'use strict';
|
|
|
|
// Element references
|
|
|
|
const projectInput = document.querySelector('#project-select');
|
|
const progressBarFill = document.querySelector('#progress-bar-fill');
|
|
const progressBarText = document.querySelector('#progress-bar-text');
|
|
const progressBarContainer = document.querySelector('#progress-bar');
|
|
const assetTableContainer = document.querySelector('#asset-list-container');
|
|
const assetTable = document.querySelector('#asset-list');
|
|
const assetTableToggle = document.querySelector('#asset-list-toggle');
|
|
const downloadLinkEl = document.querySelector('#download-link');
|
|
const downloadButtons = document.getElementsByClassName('download-button');
|
|
|
|
// The last loaded project, if any.
|
|
let lastResult = null;
|
|
|
|
// Returns an object containing all the query string parameters
|
|
function getQuery(queryString) {
|
|
queryString = queryString || window.location.search;
|
|
const query = {};
|
|
const pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
|
|
for (var i = 0; i < pairs.length; i++) {
|
|
const pair = pairs[i].split('=');
|
|
const key = decodeURIComponent(pair[0]);
|
|
const value = decodeURIComponent(pair[1] || '');
|
|
// If either key or value is empty, omit it.
|
|
if (key && value) {
|
|
query[key] = value;
|
|
}
|
|
}
|
|
return query;
|
|
}
|
|
|
|
// Sets a query string value without reloading the page.
|
|
function setQuery(key, value) {
|
|
const query = getQuery();
|
|
query[key] = value;
|
|
let url = '?';
|
|
for (const key of Object.keys(query)) {
|
|
const value = query[key];
|
|
// omit parameters with empty keys or values
|
|
// allows '' as a value to remove a parameter
|
|
if (!key || !value) {
|
|
continue;
|
|
}
|
|
if (url.length > 1) {
|
|
url += '&';
|
|
}
|
|
url += encodeURIComponent(key) + '=' + encodeURIComponent(value);
|
|
}
|
|
history.replaceState({}, '', url);
|
|
}
|
|
|
|
// Project ID input
|
|
projectInput.addEventListener('focus', function() {
|
|
if (getId()) {
|
|
projectInput.select();
|
|
}
|
|
})
|
|
projectInput.addEventListener('input', function() {
|
|
const id = getId() || '';
|
|
projectInput.value = 'https://scratch.mit.edu/projects/' + id;
|
|
setButtonsEnabled(!!id);
|
|
resetAssets();
|
|
hideProgress();
|
|
removeDownloadLink();
|
|
});
|
|
function extractId(input) {
|
|
const match = projectInput.value.match(/\d+/);
|
|
return match ? match[0] : null;
|
|
}
|
|
function getId() {
|
|
return extractId(projectInput.value);
|
|
}
|
|
|
|
// Type input
|
|
for (const button of downloadButtons) {
|
|
button.addEventListener('click', function() {
|
|
loadInput(getId(), button.dataset.type);
|
|
});
|
|
}
|
|
|
|
function setInputEnabled(enabled) {
|
|
projectInput.disabled = !enabled;
|
|
}
|
|
function setButtonsEnabled(enabled) {
|
|
for (const el of downloadButtons) {
|
|
el.disabled = !enabled;
|
|
}
|
|
}
|
|
function disableInputs() {
|
|
setInputEnabled(false);
|
|
setButtonsEnabled(false);
|
|
}
|
|
function enableInputs() {
|
|
setInputEnabled(true);
|
|
setButtonsEnabled(true);
|
|
}
|
|
|
|
// Progress bar methods
|
|
|
|
// Sets the current progress. Expects a number between 0 and 1
|
|
function setProgress(progress) {
|
|
progressBarFill.style.width = (10 + progress * 90) + '%';
|
|
}
|
|
// Resets the progress bar
|
|
function resetProgress() {
|
|
setProgress(0);
|
|
setProgressState('Loading...', '');
|
|
}
|
|
// Shows the progress bar
|
|
function showProgress() {
|
|
progressBarContainer.hidden = false;
|
|
}
|
|
// Hides the progress bar
|
|
function hideProgress() {
|
|
progressBarContainer.hidden = true;
|
|
}
|
|
// Sets the text and 'state' of the progress bar (usually changes color)
|
|
function setProgressState(message, state) {
|
|
progressBarText.textContent = message;
|
|
progressBarFill.setAttribute('state', state || '');
|
|
}
|
|
|
|
// Install progress hooks
|
|
let finishedTasks = 0;
|
|
let totalTasks = 0;
|
|
function updateProgressBarHooks() {
|
|
setProgress(finishedTasks / totalTasks);
|
|
setProgressState('\u23f3 Downloading project files (' + finishedTasks + '/' + totalTasks + ')');
|
|
}
|
|
SBDL.progressHooks.start = function() {
|
|
finishedTasks = 0;
|
|
totalTasks = 0;
|
|
};
|
|
SBDL.progressHooks.newTask = function() {
|
|
totalTasks++;
|
|
updateProgressBarHooks();
|
|
};
|
|
SBDL.progressHooks.finishTask = function() {
|
|
finishedTasks++;
|
|
updateProgressBarHooks();
|
|
};
|
|
|
|
// Asset listing
|
|
assetTableToggle.addEventListener('change', function(e) {
|
|
toggleAssets(e.target.checked);
|
|
e.preventDefault();
|
|
});
|
|
// Toggles the asset viewer. Might load or reset the asset list.
|
|
function toggleAssets(enabled) {
|
|
if (enabled) {
|
|
assetTableContainer.hidden = false;
|
|
setQuery('assets', 'on');
|
|
if (lastResult && lastResult.files) {
|
|
displayAssets(lastResult.files);
|
|
}
|
|
} else {
|
|
assetTableContainer.hidden = true;
|
|
setQuery('assets', '');
|
|
resetAssets();
|
|
}
|
|
assetTableToggle.checked = enabled;
|
|
}
|
|
// Removes all existing assets in the list
|
|
function resetAssets() {
|
|
while (assetTable.firstChild) {
|
|
assetTable.removeChild(assetTable.firstChild);
|
|
}
|
|
}
|
|
// Displays a list of files
|
|
function displayAssets(files) {
|
|
resetAssets();
|
|
|
|
// Used to decode text in bufferToString()
|
|
const decoder = new TextDecoder('utf8');
|
|
|
|
function bufferToString(buffer) {
|
|
return decoder.decode(new Uint8Array(buffer));
|
|
}
|
|
|
|
function getPreview(file) {
|
|
const path = file.path;
|
|
const extension = path.split('.').pop();
|
|
|
|
const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'svg'];
|
|
const AUDIO_EXTENSIONS = ['mp3', 'wav'];
|
|
const TEXT_EXTENSIONS = ['json'];
|
|
|
|
// Returns an element indicating that this asset cannot be previewed.
|
|
function cannotPreview() {
|
|
const text = document.createElement('i');
|
|
text.textContent = 'cannot preview';
|
|
return text;
|
|
}
|
|
|
|
if (IMAGE_EXTENSIONS.includes(extension)) {
|
|
if (!(file.data instanceof ArrayBuffer)) {
|
|
return cannotPreview();
|
|
}
|
|
|
|
// Images are loaded by setting an <img> src to a Blob URL
|
|
const type = extension === 'svg' ? 'svg+xml' : extension;
|
|
const blob = new Blob([file.data], {type: 'image/' + type});
|
|
const url = URL.createObjectURL(blob);
|
|
const img = document.createElement('img');
|
|
img.src = url;
|
|
return img;
|
|
} else if (AUDIO_EXTENSIONS.includes(extension)) {
|
|
if (!(file.data instanceof ArrayBuffer)) {
|
|
return cannotPreview();
|
|
}
|
|
|
|
// Sounds are loaded by loading a Blob URL in an <audio> with controls enabled
|
|
const blob = new Blob([file.data]);
|
|
const url = URL.createObjectURL(blob);
|
|
const audio = document.createElement('audio');
|
|
audio.src = url;
|
|
// enabling controls ensures that it will be visible
|
|
audio.controls = true;
|
|
audio.autoplay = false;
|
|
return audio;
|
|
} else if (TEXT_EXTENSIONS.includes(extension)) {
|
|
// Text things are loaded using a readonly <textarea>
|
|
const textarea = document.createElement('textarea');
|
|
textarea.readOnly = true;
|
|
textarea.textContent = file.data;
|
|
return textarea;
|
|
}
|
|
|
|
return cannotPreview();
|
|
}
|
|
|
|
for (const file of files) {
|
|
const row = document.createElement('tr');
|
|
|
|
const nameCell = document.createElement('td');
|
|
const previewCell = document.createElement('td');
|
|
|
|
nameCell.classList.add('name');
|
|
nameCell.appendChild(document.createTextNode(file.path));
|
|
|
|
previewCell.classList.add('preview');
|
|
previewCell.appendChild(getPreview(file));
|
|
|
|
row.appendChild(nameCell);
|
|
row.appendChild(previewCell);
|
|
|
|
assetTable.appendChild(row);
|
|
}
|
|
}
|
|
|
|
// Loads the currently input project
|
|
function loadInput(id, type) {
|
|
setQuery('id', id);
|
|
setQuery('type', type);
|
|
|
|
removeDownloadLink();
|
|
disableInputs();
|
|
resetAssets();
|
|
resetProgress();
|
|
showProgress();
|
|
|
|
downloadProject(id, type)
|
|
.then(() => {
|
|
setProgressState('\u2705 Done', 'success');
|
|
enableInputs();
|
|
setProgress(1);
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
let error = '\u274c Project is not available as a .' + type;
|
|
if (err && err.message) {
|
|
if (err.probableType) {
|
|
error += ' (It is available as a .' + err.probableType + ')';
|
|
} else if (err.message.includes('404') && type === 'sb3') {
|
|
error += ' (Project does not exist)';
|
|
}
|
|
}
|
|
setProgressState(error, 'error');
|
|
enableInputs();
|
|
setProgress(1);
|
|
});
|
|
}
|
|
// Starts loading a project. Downloads it when complete.
|
|
function downloadProject(id, type) {
|
|
lastResult = null;
|
|
return SBDL.loadProject(id, type)
|
|
.then((r) => {
|
|
lastResult = r;
|
|
|
|
setProgressState('Creating archive...');
|
|
|
|
// Convert the result to a Blob so it's easier to download.
|
|
// The result can either give us a list of files to put in an archive, or an ArrayBuffer.
|
|
if (r.type === 'zip') {
|
|
return SBDL.createArchive(r.files, setProgress);
|
|
} else if (r.type === 'buffer') {
|
|
return new Blob([r.buffer]);
|
|
} else {
|
|
throw new Error('unknown type: ' + r.type);
|
|
}
|
|
})
|
|
.then((blob) => {
|
|
// Only display assets if there are some files to preview and they will be visible.
|
|
if (lastResult.files && !assetTableContainer.hidden) {
|
|
displayAssets(lastResult.files);
|
|
}
|
|
|
|
const url = URL.createObjectURL(blob);
|
|
const filename = lastResult.title + '.' + lastResult.extension;
|
|
const size = blob.size / 1024 / 1024;
|
|
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = filename;
|
|
a.textContent = 'Download ' + filename + ' (' + size.toFixed(2) + ' MiB)';
|
|
downloadLinkEl.appendChild(a);
|
|
a.click();
|
|
});
|
|
}
|
|
function removeDownloadLink() {
|
|
if (downloadLinkEl.firstChild) {
|
|
downloadLinkEl.removeChild(downloadLinkEl.firstChild);
|
|
}
|
|
}
|
|
|
|
// Load URL parameters
|
|
const searchQuery = getQuery();
|
|
if (searchQuery.id) {
|
|
projectInput.value = 'https://scratch.mit.edu/projects/' + searchQuery.id;
|
|
} else {
|
|
projectInput.focus();
|
|
setTimeout(function() {
|
|
projectInput.selectionStart = projectInput.selectionEnd = projectInput.value.length;
|
|
});
|
|
}
|
|
setButtonsEnabled(!!getId());
|
|
if (searchQuery.assets === 'on') {
|
|
toggleAssets(true);
|
|
}
|
|
if (searchQuery.id && searchQuery.type) {
|
|
loadInput(searchQuery.id, searchQuery.type);
|
|
}
|
|
}());
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|