Skip to content

Instantly share code, notes, and snippets.

@betillogalvanfbc
Created September 28, 2023 06:45
Show Gist options
  • Save betillogalvanfbc/a3f263cefe9d04e56b32048336309de5 to your computer and use it in GitHub Desktop.
Save betillogalvanfbc/a3f263cefe9d04e56b32048336309de5 to your computer and use it in GitHub Desktop.
extract-emails
(function() {
const regex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
const results = new Set();
async function fetchAndExtractEmails(url) {
try {
const response = await fetch(url);
const text = await response.text();
const matches = text.matchAll(regex);
for (const match of matches) {
results.add(match[0]);
}
} catch (error) {
console.log("An error occurred:", error);
}
}
async function extractEmailsFromScripts() {
const scripts = document.getElementsByTagName("script");
const scriptUrls = Array.from(scripts).map(script => script.src).filter(Boolean);
await Promise.all(scriptUrls.map(fetchAndExtractEmails));
}
async function extractEmailsFromPage() {
const pageContent = document.documentElement.outerHTML;
const matches = pageContent.matchAll(regex);
for (const match of matches) {
results.add(match[0]);
}
}
async function writeEmails() {
await Promise.all([extractEmailsFromScripts(), extractEmailsFromPage()]);
results.forEach(function(email) {
document.write(email + "<br>");
});
}
setTimeout(writeEmails, 3000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment