[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-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 you have installed the
chromix-too
Chrome extension package. Which is easy enough to get.
In Fedora, I install the
npm
package first:# dnf install npm
or equivalent
sudo
if you prefer that. Then:# npm install -g chromix-too
This package contains a client-server pair:
chromix-too
andchromix-too-server
. The server can be run after Chrome itself starts up. (I run both Chrome andchromix-too-server
as startup commands.) -
The script executes the client via the shell command:
$ chromix-too raw chrome.tabs.query '{ "active":true, "currentWindow":true }'
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 an "array of hash", but I think the array should always have just one element, so we just pop that.
-
Ugly things probably happen if you run this without the browser or the chromix server
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.