Finally a working costume from URL block

First costume from url block that actually allows you to set the image to your costume and do everything with
test infinity craft script pic

what dose it do

Get a costume from an image url. The reason they it's the "First costume from url block that actually allows you to set the image to your costume and do everything with" is because of an annoying browser thing called CORS, which I don't really want to get into right now.

so pretty much this avoids the issue of cors

Great! However, it seems a lot slower than the original block, but that's understandable.

Where is that original block ?

yeah. but this actually works unlinke the original one

The "original block" is a snap extension function

If you put a url in the block and click on it, you'd see that it does report a costume, and you can get the width and height. However the reason why they're saying that their block "actually works" is because you can't get the pixels from the costume returned by the original snap extension (it raises an error), but their block fixes that and allows you to get the pixels.

i cant import the block into snap

? no video

shows for me

instead of importing the block, it says:

maybe you have an outdated browser. Snap! Build Your Own Blocks

Its up to date. I use firefox

Can i see the apps script for this?

ya sure!

function doGet(e) {
  const imageUrl = e.parameter.url;
  if (!imageUrl) {
    return ContentService.createTextOutput("Please provide a 'url' parameter in the request.");
  }

  try {

    Logger.log("Fetching image from URL: " + imageUrl);

    const response = UrlFetchApp.fetch(imageUrl, { muteHttpExceptions: true });

    if (response.getResponseCode() !== 200) {
      Logger.log("Failed to fetch image. Response code: " + response.getResponseCode());
      return ContentService.createTextOutput("Failed to fetch the image. Please check the URL.");
    }

    const imageBlob = response.getBlob();
    Logger.log("Image fetched successfully.");

    const mimeType = imageBlob.getContentType();
    Logger.log("Detected MIME type: " + mimeType);

    const base64Image = Utilities.base64Encode(imageBlob.getBytes());
    Logger.log("Base64-encoded image preview: " + base64Image.substring(0, 100) + "...");

    const dataUrl = `data:${mimeType};base64,${base64Image}`;
    return ContentService.createTextOutput(dataUrl).setMimeType(ContentService.MimeType.TEXT);
    
  } catch (error) {
    Logger.log("Error: " + error.message);
    return ContentService.createTextOutput("Failed to fetch or encode the image.");
  }
}

Here is a project with the block in it

https://snap.berkeley.edu/snap/snap.html#present:Username=cymplecy&ProjectName=costuneFromURL&editMode

Just tried it out on an image from random to get the pixels and it worked great :slight_smile:

whereas the extension didn't

I sent that link above

with firefox, you have to download the image first. it says that in the error dialog.

for me it loads forever.

This very occasionally happens to me on Chrome: I just have to click on the picture and import it from the larger version. The only time this has happened to me is when the picture is expandable, and even then very rarely. However, the picture in the original post is not expandable, so I can't offer you any advice.