8

If you try to load with Chrome: http://sdqdsqdqsdsqdsqd.com/

You'll obtain:

ERR_NAME_NOT_RESOLVED

I would like, with a bookmarklet, to be able to get the current domain name and redirect it to a whois page in order to check if the domain is available.

I tried in the console:

window.location.href

but it outputs:

"data:text/html,chromewebdata"

Is there any way to retrieve the failed URL?

nderscore
  • 4,182
  • 22
  • 29
mattspain
  • 723
  • 9
  • 18

4 Answers4

5

The solutions given by others didn't work (maybe because I was getting a different error or have a newer version: Chrome 55):

document.querySelector('strong[jscontent="hostName"]').textContent

but the same can be achieved via:

document.querySelector('#reload-button').url

A potentially more future-proof version (from Thomas's comment)

loadTimeData.data_.summary.failedUrl

So a cross-version solution incorporating all workarounds:

var url = (l‌​ocation.href === 'data‌​:text/html,chromeweb‌​data'
    && loadTimeData.data_.summary.failedUrl
    || document.querySelector('#reload-button').url
) || location.href;

var hostname = (l‌​ocation.href === 'data‌​:text/html,chromeweb‌​data'
    && loadTimeData.data_.summary.hostName
    || document.querySelector('strong[jscontent="hostName"]').textContent
) || location.hostname;
Community
  • 1
  • 1
TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
3

On the Chrome error page, location.href doesn't point to the domain you tried to visit, since it's an internally-hosted page.

However, the domain name you tried to visit is available if you expand the "Show Details" link.

You can run this code in console (or a bookmarklet) to parse out the domain name:

document.querySelector('strong[jscontent="hostName"]').textContent
nderscore
  • 4,182
  • 22
  • 29
  • Very clever, how could I miss this! – mattspain May 01 '15 at 15:27
  • Unfortunately, this only exposes the host name, not the full URL. – chbrown Jun 10 '15 at 20:32
  • 3
    @chbrown, this is what I used to get full url (chrome only, but my use case was in a chrome bookmark): `location.href==="data:text/html,chromewebdata"&&loadTimeData.data_.summary.failedUrl||location.href` – Thomas Guyot-Sionnest Jun 16 '16 at 04:38
  • And FWIW, this is the full bookmark - loads page from the Wayback Machine: `javascript:void(location.href='http://web.archive.org/*/'+(location.href==="data:text/html,chromewebdata"&&loadTimeData.data_.summary.failedUrl||location.href))` – Thomas Guyot-Sionnest Jun 16 '16 at 04:40
2

A modified version of nderscore's since you'll need to have an if statement for the return of the correct one.

   function getUrl () {
     if(window.location.hostname == "") {
       return document.querySelector('strong[jscontent="hostName"]').textContent
     } else{
       return window.location.href;
     }
   }
nickmenow
  • 74
  • 6
2

While looking in source code of html page using Developer Tools (right-click on a web page, and select Inspect Element), I've found that full original failed URL is in a variable called

loadTimeData.data_.summary.failedUrl

In my case I have to update some 'incorrect part' of auto generated URL to 'correct part'. So I've created bookmarlet like this:

javascript: location.assign(loadTimeData.data_.summary.failedUrl.replace('IncorrectPart','CorrectPart'));  
anacron
  • 6,443
  • 2
  • 26
  • 31
CSharp
  • 1,573
  • 2
  • 14
  • 25
  • Not sure why this was downvoted. But `loadTimeData.data_.summary.failedUrl` seems to print the correct url that orignally failed. – anacron Jun 16 '20 at 06:49