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.

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Visualizing Promises and Async/Await 🤯

async await

Learn the ins and outs of Promises and Async/Await!

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay