CracKerMe / dev_code

基于业务代码的功能性抽离,非原创轮子 非常实用,更多tricks 请看博客
https://crackerme.github.io/
12 stars 1 forks source link

粘贴板更新 2024 #2

Open CracKerMe opened 2 weeks ago

CracKerMe commented 2 weeks ago
async function getClipboardContents() {
  try {
    const clipboardItems = await navigator.clipboard.read();
    for (const clipboardItem of clipboardItems) {
      for (const type of clipboardItem.types) {
        const blob = await clipboardItem.getType(type);
        if (blob.type.startsWith('image/')) {
          // 处理图片
          const imageUrl = URL.createObjectURL(blob);
          console.log('图片 URL:', imageUrl);
        } else if (blob.type.startsWith('text/')) {
          // 处理文本
          const text = await blob.text();
          console.log('文本内容:', text);
        } else {
          // 处理其他类型数据
          console.log('其他类型数据:', blob.type, blob);
        }
      }
    }
  } catch (err) {
    console.error('读取剪贴板内容时出错:', err);
  }
}

可以很好的获取 来自word等内容,以便于做富文本解析等

CracKerMe commented 2 weeks ago

注意 :

  1. 出于安全原因,只有在安全的环境下(例如 HTTPS)才能使用 Clipboard API
  2. read() 方法返回一个 Promise,它解析为一个 ClipboardItem 对象数组。每个 ClipboardItem 对象表示剪贴板中的一个项目,并包含不同格式的数据,例如文本、HTML、图像或文件。