前端复制与粘帖Demo

Demo

前端html测试代码如下

复制文本:<input id="copy-text" type="text">
粘帖文本:<input id="paste-text" type="text">
<br/>
<button οnclick="copyTextToClipboard()">复制文本</button>
<button οnclick="pasteTextFromClipboard()">粘帖文本</button>
<br/>
复制图片:<input type="file" id="copy-img" accept="image/*"/>
粘帖图片:<img id="paste-img" src="#" alt="没有粘贴图片">
<br/>
<button οnclick="copyImageToClipboard()">复制图片</button>
<button οnclick="pasteImageFromClipboard()">粘帖图片</button>

一、文本的复制与粘帖

    // 复制文本
    function copyTextToClipboard() {
        var text = document.getElementById('copy-text').value;
        if (navigator.clipboard) {
            // 浏览器支持navigator.clipboard API
            navigator.clipboard.writeText(text)
                .then(function () {
                    alert('已复制文本到粘贴板');
                })
                .catch(function (err) {
                    console.error('复制文本到粘贴板发送异常', err);
                });
        } else {
            // (IE)浏览器不支持navigator.clipboard API
            const textArea = document.createElement("textarea");
            textArea.value = text;

            document.body.appendChild(textArea);
            textArea.select();

            try {
                document.execCommand('copy');
                console.log('(非navigator.clipboard)已复制文本到粘贴板');
            } catch (err) {
                console.error('(非navigator.clipboard)复制文本到粘贴板发送异常', err);
            }

            document.body.removeChild(textArea);
        }
    }

    // 粘帖文本
    function pasteTextFromClipboard() {
        if (navigator.clipboard) {
            // 浏览器支持navigator.clipboard API
            navigator.clipboard.readText()
                .then(function (clipboardText) {
                    document.getElementById('paste-text').value = clipboardText;
                    console.log('从粘贴板获取到文本:', clipboardText);
                })
                .catch(function (err) {
                    console.error('从粘贴板获取文本发送异常', err);
                });
        } else {
            // (IE)浏览器不支持navigator.clipboard API
            const textArea = document.createElement("textarea");
            document.body.appendChild(textArea);

            textArea.focus();

            try {
                document.execCommand('paste');
                const clipboardText = textArea.value;
                document.getElementById('paste-text').value = clipboardText;
                console.log('(非navigator.clipboard)从粘贴板获取到文本:', clipboardText);
            } catch (err) {
                console.error('(非navigator.clipboard)从粘贴板获取文本发送异常', err);
            }

            document.body.removeChild(textArea);
        }
    }


二、图片的复制与粘贴

图片的复制与粘帖需要依赖navigator.clipboard,IE不支持

    // 复制图片到粘贴板
    function copyImageToClipboard() {
        const img = new Image();
        img.crossOrigin = 'Anonymous';

        img.onload = function () {
            const canvas = document.createElement('canvas');
            canvas.width = img.width;
            canvas.height = img.height;

            const ctx = canvas.getContext('2d');
            ctx.drawImage(img, 0, 0);

            canvas.toBlob(function (blob) {
                const item = new ClipboardItem({'image/png': blob});
                navigator.clipboard.write([item]).then(function () {
                    alert('复制图片到粘贴板');
                }).catch(function (err) {
                    console.error('复制图片到粘贴板出现异常', err);
                });
            }, 'image/png');
        };

        img.src = URL.createObjectURL(document.getElementById('copy-img').files[0]);

    }

    // 粘帖图片
    function pasteImageFromClipboard() {
        navigator.clipboard.read().then(function (clipboardItems) {
            var item = clipboardItems[0];
            var type = item.types[0];
            if (type.indexOf("image") > -1) {
                item.getType(type).then(function (blob) {
                    document.getElementById('paste-img').src = URL.createObjectURL(blob);
                });
            }
        }).catch(function (err) {
                console.error('从粘帖板获取图片发送异常', err)
            }
        );
    }



  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值