Untitled

 avatar
unknown
plain_text
a month ago
2.4 kB
2
Indexable
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Picture App</title>
  <style>
    body {
      font-family: sans-serif;
      margin: 20px;
    }
    img {
      max-width: 100%;
      max-height: 300px;
      display: block;
      margin-top: 16px;
    }
  </style>
</head>
<body>
  <h1>Picture App</h1>
  <input type="text" id="urlInput" placeholder="Enter image URL" style="width: 80%;">
  <button id="downloadBtn">Download Image</button>
  <br>
  <img id="image" alt="Downloaded image will appear here">
  <br>
  <button id="shareBtn">Share Image</button>

  <script>
    const urlInput = document.getElementById('urlInput');
    const downloadBtn = document.getElementById('downloadBtn');
    const shareBtn = document.getElementById('shareBtn');
    const image = document.getElementById('image');
    let imageBlob = null;

    // Download the image from the entered URL and display it.
    downloadBtn.addEventListener('click', async () => {
      const url = urlInput.value.trim();
      if (!url) {
        alert("Please enter an image URL.");
        return;
      }
      try {
        const response = await fetch(url);
        if (!response.ok) {
          throw new Error("Network response was not ok");
        }
        imageBlob = await response.blob();
        const objectURL = URL.createObjectURL(imageBlob);
        image.src = objectURL;
      } catch (error) {
        console.error("Error downloading image:", error);
        alert("Failed to download image. Please check the URL.");
      }
    });

    // Share the downloaded image using the Web Share API.
    shareBtn.addEventListener('click', async () => {
      if (!imageBlob) {
        alert("No image available to share. Please download an image first.");
        return;
      }
      if (navigator.share && navigator.canShare && navigator.canShare({ files: [] })) {
        const file = new File([imageBlob], 'shared_image.png', { type: imageBlob.type });
        try {
          await navigator.share({
            files: [file],
            title: "Shared Image",
            text: "Check out this image!",
          });
        } catch (error) {
          console.error("Error sharing image:", error);
          alert("Error sharing image.");
        }
      } else {
        alert("Web Share API is not supported in this browser.");
      }
    });
  </script>
</body>
</html>
Editor is loading...
Leave a Comment