0

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://www.youtube.com/watch?v=ZrU_tt4R3xY. 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?

2
  • Do you want to use ajax? Commented May 8, 2015 at 4:33
  • @tuananh Ahh that's a good idea. And I actually need this functionality for a chrome extension. So maybe that can help with the "cross-domain" scripting? Commented May 8, 2015 at 5:12

2 Answers 2

3

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://www.youtube.com/watch?v=ZrU_tt4R3xY", 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.