Advertisement
Guest User

Untitled

a guest
Oct 8th, 2023
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function handlePaste(event) {
  2.     // Get the clipboard data from the event
  3.     var clipboardData = event.clipboardData || window.clipboardData;
  4.     // Get the first item from the clipboard data
  5.     var item = clipboardData.items[0];
  6.     // Check if the item is an image
  7.     if (item && item.type.indexOf("image") !== -1) {
  8.         // Get the image file as a blob
  9.         var blob = item.getAsFile();
  10.         // Create a new file object with the same blob, name and type as the original file
  11.         var file = new File([blob], blob.name, {type: blob.type});
  12.         // Create a new form data object
  13.         var formData = new FormData();
  14.         // Append the file to the form data with the key "image"
  15.         formData.append("image", file);
  16.         // Create a new XMLHttpRequest object
  17.         var xhr = new XMLHttpRequest();
  18.         // Set the request method to POST and the URL to "/"
  19.         xhr.open("POST", "/");
  20.         // Set the onload function to handle the response
  21.         xhr.onload = function() {
  22.             // Check if the status is 200 OK
  23.             if (xhr.status === 200) {
  24.                 // Parse the response as JSON
  25.                 var data = JSON.parse(xhr.responseText);
  26.                 // Get the image URL from the data
  27.                 var url = data.url;
  28.                 // Create a new image element
  29.                 var img = document.createElement("img");
  30.                 // Set the src attribute to the image URL
  31.                 img.src = url;
  32.                 // Set the class attribute to "image"
  33.                 img.className = "image";
  34.                 // Create a new div element
  35.                 var div = document.createElement("div");
  36.                 // Set the class attribute to "url"
  37.                 div.className = "url";
  38.                 // Set the text content to the image URL
  39.                 div.textContent = url;
  40.                 // Get the container element by its ID
  41.                 var container = document.getElementById("container");
  42.                 // Append the image and the div elements to the container element
  43.                 container.appendChild(img);
  44.                 container.appendChild(div);
  45.             } else {
  46.                 // Alert an error message
  47.                 alert("Something went wrong!");
  48.             }
  49.         };
  50.         // Send the form data as the request body
  51.         xhr.send(formData);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement