Today I had a burning need for a new bookmarklet. I received an email with a link to a URL redirect service that was located behind a corporate firewall, inaccessible from the public internet. The URL was something like this:
If you try to open this link, your browser will naturally tell you that “This webpage is not available.”
Fixing the URL is easy enough: just chop off everything before the
?url= bit. But doing this over and over
struck me as a chore that a bookmarklet could take care of. I’d written a few convenience
bookmarklets looks something like this one, which replaces the server name in the window’s URL with
With the corporate intranet URL, though, I ran right into a roadblock using this approach:
window.location.href was returning
data:text/html,chromewebdata instead of the URL in the location bar.
The problem was that Chrome’s DNS error page had usurped the window’s
location! It seemed that the
original URL was nowhere to be found in the DOM in
window.* where I was used to looking.
But then I noticed that the text of Chrome’s error page contained the original URL. While View Source was of
no help, I brought up Google’s Developer Tools (right-click on a web page, and select Inspect Element)
variable accessible via
templateData.summary.failedUrl. Typing this phrase into the Developer
Console returned the original URL.
Bingo! Now I just needed strip off everything before the
?url= parameter. Regex to the rescue!
With this in hand, I was able to create my bookmarklet:
If you’d like to use this bookmarklet too, just drag the following link into your bookmark bar:
(Note: Jekyll appears to be escaping some of the characters in this bookmarklet. It still works, but if you avoid %-encoded characters when possible as I do, just replace the bookmarklet’s URL with the sample code above.)