The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 717 other subscribers

XKCD to ExplainXKCD Bookmarklet

Posted by jpluimers on 2025/01/21

Sometimes the XKCD episodes are hard to get, so I wanted a Bookmarklet to navigate to [Wayback/Archive] explain xkcd.

Here is the regular expression to change the domain name part if it matches: [Wayback/Archive] regex101: build, test, and debug regex: VI34VW with this generated sed code:

sed -E 's/^(www\.)?(xkcd\.com$)/$1explain$2/gm;t;d' <<< "example.com
xkcd.com
explainxkcd.com
www.example.com
www.xkcd.com
www.explainxkcd.com"

Since that only replaced the domain name part of the URL, I wrote some JavaScript so only that part needs replacement as there you have the class [Wayback/Archive] URL – Web APIs | MDN.

This is the Bookmarklet:

javascript:{ url = new URL(location); hostname = url.hostname ; url.hostname = hostname.replace(/^(www\.)?(xkcd\.com$)/g, '$1explain$2'); if (hostname !== url.hostname) { window.open(url, '_blank').focus(); }}

This is the more readable form:

javascript: {
    url = new URL(location);
    hostname = url.hostname;
    url.hostname = hostname.replace(/^(www\.)?(xkcd\.com$)/g, '$1explain$2');
    if (hostname !== url.hostname) {
        window.open(url, '_blank').focus();
    }
}

Two notes:

  1. I save the hostname for later comparison so the explainxkcd link is only opened when not already open.
  2. For replace experimentation (not the regular expression goes without quotes!), I use the JavaScript runner on the [Wayback/Archive] String.prototype.replace() – JavaScript | MDN documentation page.

--jeroen

Leave a Comment