Skip to content

Instantly share code, notes, and snippets.

@HemanthJabalpuri
Created July 29, 2024 16:49
Show Gist options
  • Save HemanthJabalpuri/7d6fee797299a2dc5072fa64f94c0584 to your computer and use it in GitHub Desktop.
Save HemanthJabalpuri/7d6fee797299a2dc5072fa64f94c0584 to your computer and use it in GitHub Desktop.
This JavaScript snippet is used to show all the domains a website is using. This is helpful when maintaining a proxy server with whitelisting domains. All the code is taken from ChatGPT. You can add this as snippet in Chrome -> Developer Tools -> Sources (tab) -> Snippets (sub tab)
(function() {
let domains = new Set();
// Collect all resource URLs from the performance API
performance.getEntriesByType("resource").forEach((entry) => {
let url = new URL(entry.name);
domains.add(url.hostname);
});
// Helper function to add a URL's hostname to the set
function addDomainFromUrl(url) {
try {
let parsedUrl = new URL(url);
domains.add(parsedUrl.hostname);
} catch (e) {
console.error(`Invalid URL: ${url}`);
}
}
// Collect URLs from hyperlinks and form actions
document.querySelectorAll('a[href], form[action], iframe[src]').forEach((element) => {
let url;
if (element.tagName === 'A') {
url = element.href;
} else if (element.tagName === 'FORM') {
url = element.action;
} else if (element.tagName === 'IFRAME') {
url = element.src;
}
if (url) {
addDomainFromUrl(url);
}
});
// Collect URLs from inline styles (e.g., background images)
document.querySelectorAll('[style]').forEach((element) => {
let style = element.style.backgroundImage || element.style.background || element.style.content;
let matches = style.match(/url\((['"]?)(.*?)\1\)/);
if (matches && matches[2]) {
addDomainFromUrl(matches[2]);
}
});
// Convert Set to array
let domainsArray = [...domains];
// Function to sort domains with fewer parts (likely top-level domains) first
function sortDomains(a, b) {
// Compare based on the number of parts in the domain name
return a.split('.').length - b.split('.').length;
}
// Sort domains
domainsArray.sort(sortDomains);
// Generate a filename based on the website's main domain
function getBaseDomain(url) {
try {
let parsedUrl = new URL(url);
return parsedUrl.hostname;
} catch (e) {
console.error(`Invalid URL for base domain: ${url}`);
return 'website';
}
}
// Get the base domain from the current page URL
let baseDomain = getBaseDomain(window.location.href);
let sanitizedBaseDomain = baseDomain.replace(/[^a-zA-Z0-9]/g, '_'); // Sanitize filename
let filename = `${sanitizedBaseDomain}_domains.txt`;
// Convert sorted array to a text format
let domainsText = domainsArray.join('\n');
// Create a Blob from the domains text
let blob = new Blob([domainsText], { type: 'text/plain' });
// Create a link element
let link = document.createElement('a');
// Set the download attribute with the dynamic filename
link.download = filename;
// Create a URL for the Blob and set it as the href attribute
link.href = URL.createObjectURL(blob);
// Append the link to the body
document.body.appendChild(link);
// Programmatically click the link to trigger the download
link.click();
// Remove the link from the document
document.body.removeChild(link);
// Log the sorted domains to console
console.log("Sorted Domains:", domainsArray);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment