Last active
March 28, 2025 09:25
-
-
Save wzulfikar/23f2d0a2a901e5686d5b7b564ea3f919 to your computer and use it in GitHub Desktop.
JS script to replace emails with random emails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Grok wrote this :) | |
* The prompt: js script that i can run in browser console to detect all html tags which contain email. | |
* i want to replace those emails from eg [email protected] to a random email based on random first and last name. | |
*/ | |
// Function to generate completely random email with full name | |
function generateRandomFullEmail() { | |
const firstNames = ['sandra', 'john', 'mary', 'peter', 'lisa', 'david', 'emma', 'robert']; | |
const lastNames = ['smith', 'johnson', 'brown', 'taylor', 'wilson', 'davis', 'clark', 'lewis']; | |
const domains = ['gmail.com', 'yahoo.com', 'outlook.com', 'hotmail.com', 'example.com']; | |
// Get random first name | |
const firstName = firstNames[Math.floor(Math.random() * firstNames.length)]; | |
// Get random last name | |
const lastName = lastNames[Math.floor(Math.random() * lastNames.length)]; | |
// Get random domain | |
const domain = domains[Math.floor(Math.random() * domains.length)]; | |
return `${firstName}${lastName}@${domain}`; | |
} | |
// Find and replace emails in all HTML elements | |
function replaceEmailsInPage() { | |
// Regular expression for email detection | |
const emailRegex = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g; | |
// Get all HTML elements | |
const elements = document.getElementsByTagName('*'); | |
for (let element of elements) { | |
// Check text nodes | |
for (let node of element.childNodes) { | |
if (node.nodeType === 3) { // Text node | |
const text = node.nodeValue; | |
if (emailRegex.test(text)) { | |
const newText = text.replace(emailRegex, generateRandomFullEmail()); | |
node.nodeValue = newText; | |
} | |
} | |
} | |
// Check attributes that might contain emails | |
if (element.attributes) { | |
for (let attr of element.attributes) { | |
if (emailRegex.test(attr.value)) { | |
attr.value = attr.value.replace(emailRegex, generateRandomFullEmail()); | |
} | |
} | |
} | |
} | |
console.log('Email replacement with random full-name emails complete!'); | |
} | |
// Run the function | |
replaceEmailsInPage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment