我的博客
欢迎来到我的博客
bunny.icu

js读取剪切板

js读取剪切板

读取文本

document.body.addEventListener(
    'click',
    async (e) => {
        const text = await navigator.clipboard.readText();
        console.log(text);
    }
)

读取文件

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);
                console.log(URL.createObjectURL(blob));
            }
        }
    } catch (err) {
        console.error(err.name, err.message);
    }
}

getClipboardContents()

Reference

剪贴板操作Clipboard API 教程- 阮一峰的网络日志

版权声明


本作品系原创, 转载须遵循 CC BY-NC-ND 4.0 许可协议
本文标题:js读取剪切板
本文链接:https://www.bunny.icu/archives/1424

推荐文章

发表评论

textsms
account_circle
email

bunny.icu

js读取剪切板
读取文本 document.body.addEventListener( 'click', async (e) => { const text = await navigator.clipboard.readText(); console.log(text); } ) …
扫描二维码继续阅读
2020-11-12