Tiny, dependency-free helpers to normalize clipboard paste events across browsers — works with text, HTML, and SVG / image files.
- Works in Chromium, Safari, and Firefox
- Extracts:
- Real image files (
image/png,image/jpeg,image/svg+xml) - Inline
<svg>markup from pasted HTML or plain text - SVGs encoded as
data:image/svg+xml;... - Plain text / HTML clipboard content
- Real image files (
- No dependencies
- 100% test coverage (Mocha + Chai suite included)
npm install pastekit
# or
pnpm add pastekitimport { getPastedData } from 'pastekit';
document.addEventListener('paste', async (evt) => {
const { files, text, html } = await getPastedData(evt);
if (files.length) {
// Handle image/SVG upload
console.log('Pasted files:', files);
return;
}
if (text) {
console.log('Pasted text:', text);
} else if (html) {
console.log('Pasted HTML:', html);
}
});{
files: File[]; // any pasted image or reconstructed SVGs
text: string|null;
html: string|null;
}| Browser | Real image files | Inline SVG | Data-URL SVG | HTML/Text |
|---|---|---|---|---|
| Chrome/Edge | ✅ | ✅ | ✅ | ✅ |
| Safari | ✅ | ✅ | ✅ | ✅ |
| Firefox | ❌ (no file items on paste) | ✅ | ✅ | ✅ |
Reads from a native paste event and returns all available clipboard data.
Handles both Chromium’s DataTransferItem interface and Firefox’s getData() fallback.
npm testMocha + Chai tests cover:
- Real file pastes
- Inline SVG extraction
- Data-URL decoding
- HTML + text fallback (Firefox path)
MIT © ChatGPT (OpenAI) & Stephan Hoyer
- Uses only built-in browser APIs (
DOMParser,XMLSerializer, etc.) - For maximum security, sanitize any SVGs server-side before storing or rendering them.