Loading...
Please waitLoading...
Please waitpnpm dlx uselab@latest add use-clipboard-guard
import { useClipboardGuard } from "@/hooks/use-clipboard-guard"
export function Component() {
const { copy, paste } = useClipboardGuard({
sanitizeFn: (text) => {
return text.replace(/[<>]/g, "")
},
onCopy: (sanitized) => {
console.log("Copied:", sanitized)
},
onPaste: (sanitized) => {
console.log("Pasted:", sanitized)
},
})
const handleCopy = async () => {
const success = await copy("Hello <script>alert('xss')</script>")
if (success) {
console.log("Text copied successfully")
}
}
const handlePaste = async () => {
const text = await paste()
if (text) {
console.log("Pasted text:", text)
}
}
return (
<div>
<button onClick={handleCopy}>Copy (Sanitized)</button>
<button onClick={handlePaste}>Paste (Sanitized)</button>
</div>
)
}useClipboardGuard(options?):
Options:
sanitizeFn: Custom function to sanitize clipboard text (default removes control characters).onCopy: Callback fired after successful copy with sanitized text.onPaste: Callback fired after successful paste with sanitized text.Security:
\x00-\x1F, \x7F-\x9F).Error Handling:
false if copy fails, null if paste fails.| Name | Type | Description | Default Value | Optional |
|---|---|---|---|---|
options | ClipboardGuardOptions | Configuration options for clipboard operations. | {} | Yes |
| Name | Type | Description |
|---|---|---|
sanitizeFn | (text: string) => string | Custom function to sanitize clipboard text. Default removes control characters. |
onCopy | (text: string) => void | Callback fired after successful copy with the sanitized text. |
onPaste | (text: string) => void | Callback fired after successful paste with the sanitized text. |
| Name | Type | Description |
|---|---|---|
copy | (text: string) => Promise<boolean> | Copies sanitized text to clipboard. Returns true on success, false on failure. |
paste | () => Promise<string | null> | Pastes and sanitizes text from clipboard. Returns sanitized text or null on failure. |
The clipboard guard automatically sanitizes text by removing < and > characters. Try copying text with these characters to see them removed.