浏览器剪切板兼容性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Paste</title>
</head>
<body>
  <textarea name="" id="" cols="30" rows="10"></textarea>
   <script>
      
     async function richPaste(clipboard, content) {
      console.log(clipboard)
      const items = await clipboard.read()
      console.log(items)
      if(Object.prototype.toString.call(items) !== "[object Array]"){
        for (const item of items.items) {
            if(item.type !== "text/rtf") {
                item.getAsString( str => {
                    document.write(str)
                })
            }
        }
      } else {
        for (const item of items) {
            for (const type of item.types) {
                item.getType(type).then( res => {
                    console.log(res)
                    res.text().then( str => {
                        console.log(str)
                        document.write(str)
                    })
                }).catch( err => {
                    console.log(err)
                })
            }
        }
      }
       
     }
 
      window.onload = function(){
        var clipboard = window.navigator.clipboard
        var content = document.getElementById("content")
        console.log(content)
        document.addEventListener("paste", function(e){
          console.log(e)
          richPaste(clipboard, content)
        })
      }
   </script>
 
   <div id="content"></div>
</body>
</html>

  

posted @   winyh  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2019-08-20 antd 用 customize-cra 方式引入 sass
2019-08-20 如何发布自定义的UI 组件库到 npmjs.com 并且编写 UI组件说明文档
2018-08-20 前端数据结构
2018-08-20 一台电脑配置多个SSH KEY,多个用户身份
点击右上角即可分享
微信分享提示