Ask coding questions

← Back to all posts
How to edit files/blobs in JavaScript and then export it
h
Coder2195Text (35)

The scratch GUI is a great GUI with its exporting and importing with sb3 files

I want to know how does scratch do it

According to stack overflow they say you can’t edit files

But how does scratch GUI do it

Comments
hotnewtop
Coder100 (17027)

Simple
after you obtain the file, convert it into a blob, and then:
https://developer.mozilla.org/en-US/docs/Web/API/Blob/text

and then:
https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL

or however else you find you want to do

Coder2195Text (35)

@Coder100 no I meant for example I have some text
How do I write that to a txt file and export it

Coder100 (17027)

@Coder2195Text you DON't write it to any file, you just make a new blob that can be downloaded

did you forget you can't make any files lol

Coder2195Text (35)

@Coder100 hmmm so how do I convert a string to a blob

Coder2195Text (35)

@Coder100 I take a look
I gtg anyways

notGilbert (78)

@Coder2195Text
you’ll need to serialize whatever you need to save into a string format, i find JSON.stringify really useful for that

then you can download the contents as a blob

alternatively if you only need to store it in the browser you can use IndexedDB or localStorage to store that JSON string

Coder100 (17027)

hmm did he not assert that the type was a string tho @notGilbert

tussiez (1515)

If you're using NodeJS:

const fs = require('fs');
let someTextFile = fs.readFileSync('file.txt','utf8'); // the file

// change the file
fs.writeFile('file.txt','Hey want a rickroll');
notGilbert (78)

you cant edit files directly on the disk, however, you can upload modify, and download

you can save the data into a JSON or a . file to export data

do you have a sb3 file so i can look inside of it?

Coder2195Text (35)

@notGilbert no I was giving an example
How does scratch modify files inside the page and then export it

notGilbert (78)

@Coder2195Text
scratch seems to use blobs to download the data, and using canvas to modify sprites (i think)

it doesnt modify any files, it just decodes it and reads and the downloads a serializable format when requested

notGilbert (78)

@Coder2195Text

what data are you trying to store / export?