概述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 ''; }

发表评论 取消回复