概述Clipboard 支持 HTML 类型。本文示例读写 `text/html` 并进行基础过滤。能力与读写const supportsWrite = !!(navigator.clipboard && navigator.clipboard.write);

const supportsRead = !!(navigator.clipboard && navigator.clipboard.read);

function sanitize(html) { return html.replace(/<script[\s\S]*?<\/script>/gi, '').replace(/on\w+=\"[^\"]*\"/gi, ''); }

async function writeHTML(html) {

if (!supportsWrite) return;

const safe = sanitize(html);

const item = new ClipboardItem({ 'text/html': new Blob([safe], { type: 'text/html' }) });

await navigator.clipboard.write([item]);

}

async function readHTML() {

if (!supportsRead) return '';

const items = await navigator.clipboard.read();

for (const it of items) { if (it.types.includes('text/html')) { const b = await it.getType('text/html'); return await b.text(); } }

return '';

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部