Jekyll Email Protection
A few months ago, I migrated my blog from Cloudflare to my cloud server. Since then, I’ve received more spam than before. I found the reason is that Cloudflare has a feature that protects email from crawlers. Cloudflare scans your HTML pages and replaces all mailto
links with encoded URLs, then insert a Javascript that will decode them when the browser loads that page. For example, suppose we have such an email link:
1 |
|
Then Cloudflare replaces it to:
1 |
|
So it looks like a normal link. That string 523e272b27123a27333c357c313d3f
is the encoded email. If a crawler gets that, it’s hard to know that’s an email link, except run the page, like a true browser. In that case, the following Javascript would be run and the link would be decoded. It’s a large overhead for crawlers to parse a page like browsers.
The encoding algorithm is pretty simple. We choose a random one-byte number and xor it with each character of the email string. Convert each xor result to hex and join them to get the encoded string. The random number also be converted to hex and inserted into the beginning of that encoded string.
To decode it to get the original email, we regard every two characters as a one-byte hex number. Just xor the remaining numbers with the first number and convert them to characters.
I’d like to do the same thing on my self-hosted blog. I want Jekyll to encode email links when building the site, then add a piece of Javascript to decode them when the browser loads the page. Liquid, the template language used by Jekyll, has limited functions, it can’t handle string and characters. Lucky, Jekyll provided a convenient way to extend Liquid. In that case, we add a Liquid filter email_encode
to encode email. Just add a Ruby script to the directory _plugins
:
1 |
|
It uses the same way as Cloudflare to encode the email and place a ‘#’ at the beginning the make it an anchor link. So we can use it like the following:
1 |
|
And Jekyll would generate such results:
1 |
|
The last thing is adding a piece of Javascript to decode that link:
1 |
|
Reference: https://jekyllrb.com/docs/plugins/