[Update 2025-08-02: Switched over to the Claude-written Chrome API Bridge extension instead of the defunct chromix-too.]
[Update 2020-07-16: added some new logic to allow link target text to be provided on standard input. Prettified (slightly) the site-string chopping regex.]
First, a bit of background on my environment:
-
I use Google Chrome for my browser in Linux.
-
I use plain old vim in a terminal window to compose HTML for this blog.
- And what I want to do all the time when composing HTML is to generate a link to the page displayed in the active tab in Chrome's current window.
For example, if I'm looking at this page in Chrome, I might say "Ooh, cool!" and want to insert the following into my HTML:
<a href="https://science.slashdot.org/story/20/07/01/1816253/a-massive-star-has-seemingly-vanished-from-space-with-no-explanation">A Massive Star Has Seemingly Vanished from Space With No Explanation</a>
That's not hard to do by hand: copy the link from Chrome into the terminal window, add in the
surrounding code for the a
tag, add the target text, don't forget the end tag (</a>
),
and we're done!
Yeah, it's not hard, but it can be tedious.
I'm sure people—much smarter people—have come up with good solutions for this. But I'm
a DIY kind of guy.
So eventually (it only took years), I wrote this small (43 68 110-line) Perl script
to do that for me. For historical reasons (by which I mean:
arbitrary and silly reasons), I named it ttb
.
Which stands for "Thing To Blog", and it's installed in my
$HOME/bin
directory.
My usual use is in vim command mode, bang-bang-ttb:
!!ttb
… which will replace the current line with the HTML link:
<a href="URL">target</a>
where URL is (duh) the URL of the active tab of Chrome's current window.
The target link text is determined by the following logic:
- If the current line contains any (non-whitespace) text, use that for the target text. (After trimming any leading or trailing whitespace.)
- Otherwise, if any command-line arguments are specified, join them together with spaces, using the result as the target text.
- Otherwise, use the HTML title of the displayed page as the target text.
That might look a bit convoluted, but… well, it is. But it works OK for me.
Notes:
-
The script assumes the presence of the
Chrome API Bridge extension that I implored the Claude AI to write for me,
due to the deprecation/no-longer-workiness of the
chromix-too
extension.
The Chrome API Bridge extension is available at GitHub
-
The script executes the client via the shell command:
curl -s http://localhost:7444/chrome/tabs/getCurrent
which produces JSON output about the active tab in the current window. The JSON perl module (I think it's installed by default in Fedora) is required to decode that into a Perl structure. The decode function returns a Perl data structure that's pretty easy to dissect to get at the tab's URL and the page's HTML title.
-
Ugly things probably happen if you run this without the browser or the extension
running. I should probably provide a clean exit in that case.
-
I noticed a lot of sites (mostly blogs) have HTML page titles that append
a uniform site string. There's an ugly ad hoc regex in the code to chop those
off. (Or should that be ad hack?)
That's a dreadful lot of verbiage about such a short script. As usual, this is not earth-shattering code, but I hope someone finds it useful, if only for tutorial purposes.
And if you know of a better way to do this… don't tell me, OK?
The source may be found at GitHub.