DEV Community

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

Posted on

7 2

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
TIMESTAMP
TARGETS
SAVE_TARGETS
MULTIPLE
STRING
UTF8_STRING
TEXT
text/plain
text/html
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)

Visualizing Promises and Async/Await πŸ€“

async await

☝️ Check out this all-time classic DEV post on visualizing Promises and Async/Await πŸ€“

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay