javascriptgoogle-chromebookmarklet

Getting the current domain name in Chrome when the page fails to load


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?


Solution

  • 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;