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

你。 提交于 2019-12-01 05:37:09

问题


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?


回答1:


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;



回答2:


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



回答3:


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



回答4:


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'));  

Mark +1 if this answer is useful to you.



来源:https://stackoverflow.com/questions/29989031/getting-the-current-domain-name-in-chrome-when-the-page-fails-to-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!