Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

粘贴板更新 2024 #2

Open
CracKerMe opened this issue Jun 28, 2024 · 1 comment
Open

粘贴板更新 2024 #2

CracKerMe opened this issue Jun 28, 2024 · 1 comment

Comments

@CracKerMe
Copy link
Owner

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
Copy link
Owner Author

注意 :

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

@CracKerMe CracKerMe reopened this Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant