在JavaScript中,可以使用Clipboard API来获取复制的数据。

  1. 首先,使用navigator.clipboard.readText()方法来读取剪贴板中的文本数据。
navigator.clipboard.readText()
  .then(text => {
    console.log(text);
  })
  .catch(err => {
    console.error('Failed to read clipboard contents: ', err);
  });
  1. 如果剪贴板中的数据是HTML格式,可以使用navigator.clipboard.read()方法来读取。
navigator.clipboard.read()
  .then(data => {
    for (const item of data) {
      if (item.types.includes('text/html')) {
        item.getType('text/html')
          .then(html => {
            console.log(html);
          })
          .catch(err => {
            console.error('Failed to read HTML data from clipboard: ', err);
          });
      }
    }
  })
  .catch(err => {
    console.error('Failed to read clipboard contents: ', err);
  });

注意:Clipboard API可能不支持所有的浏览器,因此在使用之前最好先检查浏览器的兼容性

js 获取复制的数据

原文地址: https://www.cveoy.top/t/topic/ieLF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录