Code - Email Obfuscation with JS
Jered Higgins / May 20, 2024
3 min read 🕒
Description:
Email Obfuscation with JS because I don't want to pay for a seperate service 🤑
I created this React component for my site because I’m stubborn and didn’t want to use an email service.
The code defines a React component named EmailRevealButton
that creates a button on a webpage.
When a user clicks this button, it reveals a partially hidden email address with a small delay added as an extra step.
There is also a simple character shift function that takes the original email address and encodes it by shifting each character’s ASCII code by 1.
It is then decoded only after the click event and 1 second delay.
Security Note:
This method is an obfuscation technique and not a foolproof security measure. Determined scrapers can still decode the email if they analyze the JavaScript. For better security, consider an email service.
Benefits of Using This Code:
- The email address is not directly visible in the HTML source, which can help protect it from being harvested by spambots.
- The email is encoded using a simple character shift method, making it less likely to be easily extracted by basic scraping techniques.
- The email is revealed only when the user clicks the button——also making it more difficult to be scraped.
- The 1-second delay before revealing the email also potentialy helps obfuscate the email more as most spambots are not patient.
- The use of React and its hooks (useEffect) ensures that the event listeners are properly managed, preventing potential memory leaks.
- The encoding and decoding logic is encapsulated within the component, making the code modular and easy to maintain.
- The email address can be easily changed by updating the email variable.
- The encoding method can be enhanced for stronger obfuscation if needed.
Example Usage:
When integrated into a React application, this component can be used to display a partially hidden email address that gets revealed upon user interaction: