From 6d77228469de94e243132446430d308bb06ab9a8 Mon Sep 17 00:00:00 2001 From: Azathothas Date: Wed, 5 Jun 2024 12:06:47 +0545 Subject: [PATCH] + --- index.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/index.js b/index.js index 5e71588..5c2e323 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,63 @@ function jsonToCsv(json) { return csv; } +// Function to convert filteredJson to HTML +function jsonToHtml(json) { + let html = '\n\n\nIP Details\n\n\n\n\n'; + for (const [key, value] of Object.entries(json)) { + html += `\n`; + } + html += '
${key}${isURL(value) ? `${value}` : value}
\n\n'; + return html; +} +// Function to check if a string is a URL or IPv6 address +function isURL(str) { + const urlPattern = new RegExp('^(https?:\\/\\/)?'+ // protocol + '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name + '((\\d{1,3}\\.){3}\\d{1,3})|'+ // OR ipv4 address + '([0-9a-fA-F]{1,4}:){1,7}:?([0-9a-fA-F]{1,4})?)'+ // OR ipv6 address + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path + '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string + '(\\#[-a-z\\d_]*)?$','i'); // fragment locator + return urlPattern.test(str); +} + // Function to convert filteredJson to XML function jsonToXml(json) { let xml = '\n\n'; @@ -108,6 +165,17 @@ var src_default = { } }); + //Print HTML if request is to ip.ajam.dev/html + } else if (pathname === "/html") { + const htmlStr = jsonToHtml(filteredJson); + console.log(htmlStr); + return new Response(htmlStr, { + headers: { + ...CORS_HEADERS, + "Content-Type": "text/html;charset=utf-8", + } + }); + //Print json if request is to ip.ajam.dev/json } else if (pathname === "/json") { console.log(filteredJson);