clip-sanitizer: resilient event loop with xset display wait, printf safety, and fleet-wide systemd integration
This commit is contained in:
+27
-12
@@ -1,17 +1,32 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
# clip-sanitizer — Fleet-wide clipboard sanitizer (nukes NBSP \xc2\xa0 and ZWSP \xe2\x80\x8b)
|
||||||
|
# Runs as systemd user service and autostart on t520, bazina, and boxC
|
||||||
|
|
||||||
while clipnotify; do
|
export DISPLAY="${DISPLAY:-:0}"
|
||||||
# Grab what was just copied
|
|
||||||
current_content=$(xclip -selection clipboard -o 2>/dev/null)
|
# Wait for X server display to become ready
|
||||||
|
while ! xset q >/dev/null 2>&1; do
|
||||||
if [[ -n "$current_content" ]]; then
|
sleep 2
|
||||||
# Nuke NBSP (c2 a0) and ZWSP (e2 80 8b)
|
done
|
||||||
sanitized=$(echo -n "$current_content" | sed 's/\xc2\xa0/ /g; s/\xe2\x80\x8b//g')
|
|
||||||
|
# Main event loop (resilient against clipnotify transient exits)
|
||||||
# Guard: Only write back if a cheesy space was actually destroyed
|
while true; do
|
||||||
# This prevents an infinite event loop
|
if ! clipnotify 2>/dev/null; then
|
||||||
if [[ "$current_content" != "$sanitized" ]]; then
|
sleep 1
|
||||||
echo -n "$sanitized" | xclip -selection clipboard -i
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Grab clipboard content safely
|
||||||
|
if current_content=$(xclip -selection clipboard -o 2>/dev/null); then
|
||||||
|
if [[ -n "$current_content" ]]; then
|
||||||
|
# Nuke NBSP (c2 a0 -> standard space) and ZWSP (e2 80 8b -> empty)
|
||||||
|
sanitized=$(printf "%s" "$current_content" | sed 's/\xc2\xa0/ /g; s/\xe2\x80\x8b//g')
|
||||||
|
|
||||||
|
# Guard: Only write back if a cheesy space was actually destroyed
|
||||||
|
# This prevents infinite loops with clipnotify
|
||||||
|
if [[ "$current_content" != "$sanitized" ]]; then
|
||||||
|
printf "%s" "$sanitized" | xclip -selection clipboard -i
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user