DEV Community

Cover image for Inspecting the Clipboard (on Linux)
Bernd Wechner
Bernd Wechner

Posted on

Inspecting the Clipboard (on Linux)

Developing a small Javascript library to support copying arbitrary HTML elements to the clipboard ready (for use in emails), I was using CopyQ to inspect the clipboard whenever I needed to. In particular I was interested in seeing the MIME parts of the copy.

Turns out that CopyQ is a little flakey with the WebAPI copy command I'd opted for. Often it's fine, sometimes not, just a bit here and there and I lacked the or energy to diagnose the nuances, and developed a a general sense of frustration with it alas.

Inspecting the Clipboard (revisited) 🔍

So I looked for a simpler, more reliable and perhaps native solution to inspecting the clipboard contents closely, including it's MIME part breakdown.

As I'm on Linux I found the lowest level of reliable inspection was xclip easily installed on Debian/Ubuntu derived systems with sudo apt install xclip.

Subsequently I could see the contents in parts as follows:

xclip -selection clipboard -o -t TARGETS

After a copy operation, for example, I see:

$ xclip -selection clipboard -o -t TARGETS
Enter fullscreen mode Exit fullscreen mode

And I can see the parts with:

xclip -selection clipboard -o -t text/plain

xclip -selection clipboard -o -t text/html

which works a dream. This can be piped through grep to find things, but the HTML is not formatted.

So I installed HTML tidy.

After which:

xclip -selection clipboard -o -t text/html | tidy -qi --wrap 0

Produces a nicely formatted view of the HTML MIME part.

And to get the size of the HTML on the clipboard:

xclip -selection clipboard -o -t text/html | wc -c | numfmt --to=iec

all of which proved very useful inspection and diagnostics when experimenting with what to copy and how.

Top comments (0)