How to get domain name from shortened URL with Javascript?

旧巷老猫 提交于 2019-12-12 06:14:01

问题


Suppose I have a list of domains that I want to block with front end technology. I want to block any URL that is hosted by the domain. On top of my head I have the following methods:

  • regex
  • string.split("/")
  • location.hostname

But what if the URL is a shortened one? e.g. https://goo.gl/lWyZ5B. All of the above methods will return undesired result: goo.gl.

This is actually a part of a Chrome extension I am planning. So maybe Chrome can fetch the shortened URL then redirect to a warning page if the result is on block list?


回答1:


The only way to know what the real host that is behind a shortened URL is to actually attempt to fetch the URL and then see if you get a 302 redirect and see what that redirect URL is.

Since you generally can't load cross-origin URLs from within browser Javascript, you would probably need a server to do this for you and return the result to you.


One other option is that if the link shortener is a known one that you have API access to, then you can query that API to see what a given shortened link is a shortcut for. For example, bit.ly has this api http://dev.bitly.com/data_apis.html. The API will typically allow cross origin access so you can access it from a browser.

Since you can't confidently have API access to all possible link shorteners, this strategy is really only applicable to specific link shorteners that you prepare for. A general solution for any link shortener would need to use a server to access the URL directly to get the redirect.


For example, here's a simple node.js program that fetches a URL and checks to see if it returns a redirect response and gets the redirect URL if so.

var request = require("request");

request({url: "https://goo.gl/lWyZ5B", followRedirect: false}, function(error, response, body) {
    console.log(response.statusCode);
    if (response.statusCode >= 300 && response.statusCode < 400) {
        console.log(response.headers.location);
    }
});

This generates this output:

301
https://www.youtube.com/watch?v=ZrU_tt4R3xY

This could be wrapped in a server that would take a URL as a request and return the resulting URL.




回答2:


It seems you would have to do an http call and view the response in order to accomplish this.



来源:https://stackoverflow.com/questions/30115801/how-to-get-domain-name-from-shortened-url-with-javascript

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