regex - How to get domain name from shortened URL with Javascript? -


suppose have list of domains want block front end technology. want block url hosted domain. on top of head have following methods:

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

but if url shortened one? e.g. https://goo.gl/lwyz5b. of above methods return undesired result: goo.gl.

this part of chrome extension planning. maybe chrome can fetch shortened url redirect warning page if result on block list?

the way know real host behind shortened url attempt fetch url , see if 302 redirect , see redirect url is.

since can't load cross-origin urls within browser javascript, need server , return result you.


one other option if link shortener known 1 have api access to, can query api see given shortened link shortcut for. example, bit.ly has api http://dev.bitly.com/data_apis.html. api typically allow cross origin access can access browser.

since can't confidently have api access possible link shorteners, strategy applicable specific link shorteners prepare for. general solution link shortener need use server access url directly redirect.


for example, here's simple node.js program fetches url , checks see if returns redirect response , gets 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 output:

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

this wrapped in server take url request , return resulting url.


Comments