কিভাবে ছবি পেস্ট করবেন

হ্যারি থিওডলু
হ্যারি থিওডলু

আধুনিক উপায়

Async ক্লিপবোর্ড API ব্যবহার করে

ব্যবহারকারীর ক্লিপবোর্ড থেকে ইমেজগুলি প্রোগ্রাম্যাটিকভাবে পড়তে, অর্থাৎ, একটি বোতাম ক্লিক করার পরে, আপনি Async ক্লিপবোর্ড API- এর read() পদ্ধতি ব্যবহার করতে পারেন। যদি ক্লিপবোর্ড থেকে পড়ার অনুমতি এখনও মঞ্জুর না করা হয়, navigator.clipboard.read() এ কলটি পদ্ধতির প্রথম কলে এটির অনুরোধ করবে।

const pasteButton = document.querySelector('#paste-button');

pasteButton.addEventListener('click', async () => {
  try {
    const clipboardItems = await navigator.clipboard.read();
    for (const clipboardItem of clipboardItems) {
      const imageTypes = clipboardItem.types.find(type => type.startsWith('image/'))
      for (const imageType of imageTypes) {
        const blob = await clipboardItem.getType(imageType);
        // Do something with the image blob.
      }
    }
  } catch (err) {
    console.error(err.name, err.message);
  }
});

ব্রাউজার সমর্থন

  • 86
  • 79
  • 13.1

উৎস

ক্লাসিক উপায়

paste ইভেন্ট জন্য শুনুন

চিত্রগুলি পেস্ট করার ক্লাসিক উপায় হল (সিঙ্ক্রোনাস) ক্লিপবোর্ড API ব্যবহার করা, যা আপনাকে ডকুমেন্ট: paste ইভেন্টের ভিতরে clipboardData ক্ষেত্রে অ্যাক্সেস দেয়। যাইহোক, এই পদ্ধতিটি সীমাবদ্ধতার সাথে আসে, উদাহরণস্বরূপ কারণ এটি সিঙ্ক্রোনাস, প্রচুর পরিমাণে ডেটা আটকানো পৃষ্ঠাটিকে ব্লক করতে পারে।

document.addEventListener('paste', async (e) => {
  e.preventDefault();

  for (const clipboardItem of e.clipboardData.files) {
    if (clipboardItem.type.startsWith('image/')) {
      // Do something with the image file.
    }
  }
});

ব্রাউজার সমর্থন

  • 66
  • 79
  • 63
  • 13.1

উৎস

প্রগতিশীল বর্ধিতকরণ

যে ব্রাউজারগুলি Async ক্লিপবোর্ড API সমর্থন করে না, তাদের জন্য ব্যবহারকারীর ক্লিপবোর্ড প্রোগ্রামগতভাবে অ্যাক্সেস করা অসম্ভব (উদাহরণস্বরূপ, একটি বোতাম ক্লিকে)। এইভাবে একটি paste ইভেন্টে ব্যবহারকারীর ক্লিপবোর্ড অ্যাক্সেস করার জন্য, আপনি Async ক্লিপবোর্ড API ব্যবহার করতে পারেন এবং (সিঙ্ক্রোনাস) ক্লিপবোর্ড API-এ ফিরে যেতে পারেন।

navigator.clipboard.read থেকে আসা ClipboardItem অবজেক্টের একটি types ফিল্ড থাকে যা একটি অ্যারে, এবং File অবজেক্টের event.clipboardData.files থেকে একটি type ফিল্ড থাকে যা একটি স্ট্রিং। আপনি শর্তসাপেক্ষে ক্লিপবোর্ডে চিত্রগুলির জন্য প্রতিটি type বা types ক্ষেত্র পরীক্ষা করতে পারেন:

document.addEventListener('paste', async (e) => {
  e.preventDefault();
  const clipboardItems = typeof navigator?.clipboard?.read === 'function' ? await navigator.clipboard.read() : e.clipboardData.files;

  for (const clipboardItem of clipboardItems) {
    let blob;
    if (clipboardItem.type?.startsWith('image/')) {
      // For files from `e.clipboardData.files`.
      blob = clipboardItem
      // Do something with the blob.
    } else {
      // For files from `navigator.clipboard.read()`.
      const imageTypes = clipboardItem.types?.filter(type => type.startsWith('image/'))
      for (const imageType of imageTypes) {
        blob = await clipboardItem.getType(imageType);
        // Do something with the blob.
      }
    }
  }
});

আরও পড়া

  • MDN-এ ক্লিপবোর্ড API
  • web.dev-এ ক্লিপবোর্ড অ্যাক্সেস আনব্লক করা হচ্ছে

ডেমো

এইচটিএমএল

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="icon"
      href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🎉</text></svg>"
    />
    <title>How to paste images</title>
  </head>
  <body>
    <h1>How to paste images</h1>
    <p>Hit <kbd>⌘</kbd> + <kbd>v</kbd> (for macOS) or <kbd>ctrl</kbd> + <kbd>v</kbd>
      (for other operating systems) to paste images anywhere in this page.
    </p>
  </body>
</html>

সিএসএস


        :root {
  color-scheme: dark light;
}

html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

body {
  margin: 1rem;
  font-family: system-ui, sans-serif;
}

button {
  display: block;
}
        

জেএস


        document.addEventListener('paste', async (e) => {
  e.preventDefault();
  const clipboardItems = typeof navigator?.clipboard?.read === 'function' ? await navigator.clipboard.read() : e.clipboardData.files;

  for (const clipboardItem of clipboardItems) {
    let blob;
    if (clipboardItem.type?.startsWith('image/')) {
      // For files from `e.clipboardData.files`.
      blob = clipboardItem
      // Do something with the blob.
      appendImage(blob);
    } else {
      // For files from `navigator.clipboard.read()`.
      const imageTypes = clipboardItem.types?.filter(type => type.startsWith('image/'))
      for (const imageType of imageTypes) {
        blob = await clipboardItem.getType(imageType);
        // Do something with the blob.
        appendImage(blob);
      }
    }
  }
});

const appendImage = (blob) => {
  const img = document.createElement('img');
  img.src = URL.createObjectURL(blob);
  document.body.append(img);
};