[JavaScript] Domain (Hostname) Extraction Tool from Source Code
Introduction
HTML and JavaScript source code to extract domains (hostnames). This is a JavaScript tool for when you want to know what domains are included in your source code.
<< Table of Contents >>
■ Tool Sample
■ How to Use
■ HTML Code
■ JavaScript Code
■ Tool Sample
[JavaScript] Domain (Hostname) Extraction Tool from Source Code
https://text.sakura.ne.jp/memo/tool-javascript/20221117/
■ How to Use
1. Enter the source code you want to check into the form input
2. Click the ' Extract' button
3. A list of domains (hostnames) extracted from the source code will be displayed
■ HTML Code
<form>
<p><textarea id="input" rows="10" cols="100" placeholder="ソースコードを入力"></textarea></p>
<p><input id="clickBtn" type="button" value="ドメイン(ホスト名)抽出する"> <input type="reset" value="リセット"></p>
<p><textarea id="output" rows="20" cols="100" placeholder="結果が出力されます"></textarea></p>
</form>■ JavaScript Code
window.onload = function() {
// [実行] ボタンクリック時に実行
const clickBtn = document.getElementById("clickBtn");
clickBtn.addEventListener("click", event => {
// [取得] テキストエリア内の文字を取得 (id="input")
let str = document.getElementById("input").value;
// [分岐] 取得した文字の中にドメインがある場合
// [ 例 ] "example.com"、'example.com'、/example.com/、%2Fexample.com%2F、\/example\.com\/
if (str.match(/(\"|\'|\/|\%2F)([0-9a-zA-Z\.\-]|\\\.|\\\-)+(\\)?\.[a-zA-Z]+(\\)?(\"|\'|\/|\%2F)/g)) {
// [作成] 配列を作成&検索にマッチする文字を配列に格納
str = str.match(/(\"|\'|\/|\%2F)([0-9a-zA-Z\.\-]|\\\.|\\\-)+(\\)?\.[a-zA-Z]+(\\)?(\"|\'|\/|\%2F)/g);
// [変換] 配列を文字列に変換
str = str.join(",");
// [削除] ドメイン前後の記号を削除
str = str.replace(/\\/gim, "").replace(/\"/gim, "").replace(/\'/gim, "").replace(/\//gim, "").replace(/\%2F/gim, "");
// [変換] 文字列を配列に変換
str = str.split(",");
// [削除] 重複するドメインを削除
str = Array.from(new Set(str));
// [削除] ドメインでないもの削除 (拡張子など削除)
str = str.filter(item => (item.match(/^[0-9a-zA-Z\.\-]+\.(asp|aspx|atom|cgi|css|eot|exe|gif|htm|html|ico|jpeg|jpg|js|json|ogg|otf|php|png|rdf|svg|swf|tif|ttf|txt|webmanifest|webp|wmv|woff|xml|zip|alpha|android|appl|assign|body|change|clicked|client|close|code|comment|controls|cookies|count|custom|dh|dir|disabled|dom|dtd|element|enabled|entries|error|faq|fexp|fill|find|from|generic|header|hide|href|icon|includes|init|item|iterator|key|keys|keyword|keywords|label|length|list|load|loading|login|map|mm|mobile|mpegur|navigation|onload|params|password|phone|prod|script|ss|sso|start|terms|tgz|title|token|tokens|url|value|values|version|versionl|vflset|volume|webkit|wide|width|write|[a-zA-Z]{1})$/)) == null);
// [削除] ドメインでないもの削除 (英語の大文字小文字が混ざった拡張子など削除)
str = str.filter(item => (item.match(/^[0-9a-zA-Z\.\-]+\.(?=.*?[A-Z])[a-zA-Z0-9]+$/)) == null);
// [削除] ドメインでないもの削除 (先頭がドットの文字を削除)
str = str.filter(item => (item.match(/^(\.).+$/)) == null);
// [削除] 一部のドメインを除外
str = str.filter(item => (item.match(/^(www\.w3\.org|schema\.org|api\.w\.org|ogp\.me)$/)) == null);
// [変換] 配列を文字列に変換、ドメインを縦に並べる
str = str.join(",").replace(/\,/gim, "\n");
// [表示] 該当ゼロの場合のメッセージ
str = str.replace(/^$/gim, "ドメインが見つかりませんでした。");
document.getElementById("output").value = str;
// [分岐] 取得した文字の中にドメインが無い場合
} else {
// [表示] 該当ゼロの場合のメッセージ
str = "ドメインが見つかりませんでした。";
document.getElementById("output").value = str;
}
});
};<< JavaScript Workflow >>
1. When the user clicks the button, the execution starts
2. Get the text entered in the textarea
3. Check if a domain (hostname) is included
4. Store the domain (hostname) in an array
5. Remove strings unrelated to the domain (hostname)
6. Display a list of the domain
(hostname)7. If no domain (hostname) is found, display 'No domain found.'
■ [Related Information] JavaScript Articles
This note also contains other 'JavaScript related articles'.
[JavaScript] How to add & retrieve query strings in data URI schemes
https://note.com/text_sakura/n/naf0fbceb94f4
[JavaScript] Share iPhone bookmarks with TinyURL
https://note.com/text_sakura/n/naf5404d50a3f
[JavaScript] Convert & display 'Western calendar' to 'Japanese calendar' (e.g., 1976 -> Showa 51)
https://note.com/text_sakura/n/n4629971e7ba7
[JavaScript] Create a cipher by replacing characters (e.g., hello ⇔ khddb)
https://note.com/text_sakura/n/n727601f0ef83
[JavaScript] Display 'form input text' in the 'URL hash (after #)'
https://note.com/text_sakura/n/nfc4b5bda1dcc
#MyWorkIntroduction / #ProgrammingBeginner
(text)
◇ ◇ ◇
<< List of articles written in the past >>
https://note.com/text_sakura/n/na2cd73291236
<< Contact information regarding articles >>
text.sakura.note@gmail.com
※ Emails containing attachments cannot be accepted.
※ Please send text only.
いいなと思ったら応援しよう!
読んでくれてありがとうございます 😄 このnoteでは「トークテーマ一覧」「note記事テーマ一覧」「配信企画一覧」など、まとめ記事を中心に書いています ✏️ 役に立ちそうな記事がありましたら、自由にご利用ください ☕ 記事一覧は『サイトマップ』で見られます 📄