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:
- I save the
hostnamefor later comparison so the explainxkcd link is only opened when not already open. - For
replaceexperimentation (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