メモ:Javascriptで短縮URLを展開

XMLHttpRequestを使って内部でアクセスする感じ。転送先がどういう形式で戻されるかは短縮URLのサイトによるので、別途調査要。

            const xhr = new XMLHttpRequest();
            xhr.open(‘GET’, “<展開したい短縮URL>”, true);
            xhr.responseType = ‘document’;
            xhr.onload = function(e) {
                <xhr.responseXMLにDOMが入っている>
            }
            xhr.send();

ちなみに、Twitterのt.co短縮URLの場合はこういうのがresponseに入って返ってくるので、getElementsByTagName(‘title’)とかで拾ってやるのが手っ取り早いかな。

<html>
<head>
<noscript>
<meta http-equiv=”refresh” content=”0;URL=<転送先URL>”>
</noscript>
<title><転送先URL></title>
<script>
window.opener = null;
location.replace(“<転送先URL>“)
</script>
</head>
<body></body>
</html>

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です