Forem

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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Visualizing Promises and Async/Await 🤯

async await

☝️ Check out this all-time classic DEV post

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay