How to get blob data from blob:chrome-extension?

Viewed 245

I'm trying to use this in an image.src..

"blob:chrome-extension://enjilmhmplefekkpbciadkljojpakhmd/cb5a1692-5194-4a3d-afd4-85b27445de94"

That is retrieved from a js library I'm using that loads a local file with binary data.. However, I need to get the blob data to convert it to a dataurl (I think)..

How do I get the blob data using javascript? (I'm also using nw.js if that matters)

1 Answers

If you are using NW.js and there is an actual file, just use the Node.js File System module.

<img id="cat">
<script>
  const fs = window.nw.require('fs');
  const path = window.nw.require('path');

  function getImageURI (imagePath) {
    try {
      const imageData = fs.readFileSync(imagePath);
      return 'data:image/png;base64,' + imageData.toString('base64');
    } catch (error) {
      console.log('Error reading image', error);
    }
  }

  const catImage = path.join('.', 'some-folder', 'cat.png');
  const catURI = getImageURI(catImage);
  const cat = document.getElementById('cat');
  cat.src = catURI;
</script>
Related