javascriptshort-url

How To Find Out Where the Short URLs Point To with Javascript


I have created a short url, let say https://my.short.link/foo, that is pointing to https://my.other.website/bar.

How can I retrieve this url with a javascript method inside the browser? (I'm using angular)


Solution

  • It will depend on how my.short.link handles processing the URL when you request it.

    Browser

    If you mean you want to do this in a browser, you might be able to get the information for service Type 1 above, but not for other services.

    As evolutionxbox says, you could try fetch, and surprisingly (to me!) if the service handles requests with a simple redirection response, you do see a new URL in the response, but it wont have the hash fragment:

    fetch("https://tsplay.dev/wEv6gN")
    .then(r => {
        if (!r.ok) {
            throw new Error(`HTTP error ${r.status}`);
        }
        for (const [key, value] of r.headers) {
            console.log(`${key}: ${value}`);
        }
        console.log(r.url);
    })
    .catch(console.error);

    That URL, https://tsplay.dev/wEv6gN, redirects to https://www.typescriptlang.org/play?#code/MYewdgzgLgBAJgQygmBeGB5ARgKwKbBQB0AZgE554BeeAFAN4CwAUDGzBCALZ4BcMARgBMAZgA0LdjAA24AOb96AIgRLeSrEoC+E1u2kBLaPwDaS4EoC6uqSBL8lSm+wRksBqGVcBPfmACu0tLObFAAFgZgchD8AkRCuloAlADcLCxQ3gAOePBICAD6ANZ43mgwJd52MJk51YjIacwsJP5ghAbg8CAA6iBkRbRF-A2FlUkwTHps0niwAG4I0v656KMmRZZNUgD0O0QHLFpAA via a 302 ("Moved Permanently") redirect response. In the code above, we only see https://www.typescriptlang.org/play?. But maybe that's enough for your purposes.

    But that won't work for types 2 and 3 above. You'd have to be able to read and parse the HTML, and the Same Origin Policy will prevent code running on your origin from accessing that HTML. You could try opening it in an iframe (though I suspect you don't really want to visit the page, you just want to get its URL), but the same issue applies: Even when the iframe goes to the target location, you won't be able to access that because of the SOP.

    Node.js Or Similar

    If you mean in Node.js or some other non-browser environment, then: