Skip to content
Advertisement

Convert Blob to binary string synchronously

I’m trying to put image in clipboard when user copies canvas selection:

canvas selection

So I thought the right way would be to convert canvas tu dataURL, dataURL to blob and blob to binary string.

Theoretically it should be possible to skip the blob, but I don’t know why.

So this is what I did:

JavaScript

But when the DataTransfer object is used out of the paste event thread the setData has no longer any chance to take effect.

How can I do the conversion in the same function thread?

Advertisement

Answer

Here is a hacky-way to get you synchronously from a blob to its bytes. I’m not sure how well this works for any binary data.

JavaScript

You should consider keeping it async but making it two-stage, though, as you may end up locking up the browser.

Additionally, you can skip Blobs entirely by including a binary-safe Base64 decoder, and you probably don’t need to go via Base64 AND Blob, just one of them.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement