diff --git a/clip-sanitizer.sh b/clip-sanitizer.sh index 277cbb9..2684e90 100755 --- a/clip-sanitizer.sh +++ b/clip-sanitizer.sh @@ -1,17 +1,32 @@ #!/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 - # Grab what was just copied - current_content=$(xclip -selection clipboard -o 2>/dev/null) - - if [[ -n "$current_content" ]]; then - # Nuke NBSP (c2 a0) and ZWSP (e2 80 8b) - sanitized=$(echo -n "$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 an infinite event loop - if [[ "$current_content" != "$sanitized" ]]; then - echo -n "$sanitized" | xclip -selection clipboard -i +export DISPLAY="${DISPLAY:-:0}" + +# Wait for X server display to become ready +while ! xset q >/dev/null 2>&1; do + sleep 2 +done + +# Main event loop (resilient against clipnotify transient exits) +while true; do + if ! clipnotify 2>/dev/null; then + sleep 1 + 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 done