generated from 5t3ph/11ty-sass-skeleton
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Obfuscate contact emails so that they don't get web scraped
The inspiration for this comes from a few places: - https://css-tricks.com/how-to-safely-share-your-email-address-on-a-website/ - https://mauriciorobayo.github.io/react-obfuscate-email/?path=/docs/react-obfuscate-email--mail Also, these two commits from my other repo: - pcraig3/taxgpt@5dffdfc - pcraig3/taxgpt@5820321
- Loading branch information
Showing
5 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Obfuscate emails */ | ||
|
||
const obfuscateEmail = (node) => { | ||
// Create a new element | ||
const newNode = document.createElement("a"); | ||
|
||
if (node.dataset.email) { | ||
// Preserve data attributes | ||
newNode.dataset.email = node.dataset.email; | ||
if (node.dataset.text) { | ||
newNode.dataset.text = node.dataset.text; | ||
} | ||
|
||
// Get email and text | ||
const email = atob(node.dataset.email); | ||
const text = node.dataset.text || email.split("?")[0]; | ||
|
||
// Add dummy href and obfuscated email | ||
newNode.setAttribute("href", "#"); | ||
// idea comes from https://mauriciorobayo.github.io/react-obfuscate-email/?path=/docs/react-obfuscate-email--mail | ||
newNode.innerHTML = text.replaceAll("@", '<span class="roe"></span>'); | ||
|
||
newNode.addEventListener("mouseover", unobfuscateEmail, false); | ||
newNode.addEventListener("focus", unobfuscateEmail, false); | ||
|
||
node.replaceWith(newNode); | ||
} | ||
}; | ||
|
||
function unobfuscateEmail(event) { | ||
const node = event.target; | ||
if (node.dataset.email) { | ||
// Get email and text | ||
const email = atob(node.dataset.email); | ||
const text = node.dataset.text || email.split("?")[0]; | ||
|
||
// Set real mailto attribute and content | ||
node.setAttribute("href", "mailto:".concat(email)); | ||
node.innerText = text; | ||
} | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", function () { | ||
// Loop through all elements with "email--swap" classname | ||
var nodes = document.getElementsByClassName("email--swap"); | ||
while (nodes.length) { | ||
obfuscateEmail(nodes[0]); | ||
} | ||
}); |
This file contains 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
This file contains 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
This file contains 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