[英]Obfuscating an email using JavaScript
I understand that this question has been asked many a times and the general consensus is that spam-bots will always figure out a way to harvest emails eventually so it is a lost cause.我知道这个问题已经被问过很多次了,普遍的共识是垃圾邮件机器人最终总会想出一种方法来收集电子邮件,所以这是一个失败的原因。 But I want a newly created email visible on a page that I created and I want at least something that protects it from spam-bots.
但是我希望在我创建的页面上可以看到新创建的电子邮件,并且我至少想要一些可以保护它免受垃圾邮件机器人攻击的东西。 Here is a simple
JavaScript
technique I wish to use:这是我希望使用的一个简单的
JavaScript
技术:
$(document).ready(function() { setEmailAddress("abc@xyz.com"); }); function setEmailAddress(emailValue) { $("#spanTest").append($("<a>").attr("href", "javascript:location='mailto:" + obfuscateString(emailValue) + "';void 0").text(obfuscateString(emailValue))); } function obfuscateString(plaintext) { var obfuscated = ""; for (var i = 0; i < plaintext.length; i++) { obfuscated += "\\\\u00\u0026quot; + plaintext.charCodeAt(i).toString(16); } return obfuscated; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span id="spanTest"></span>
Here are the questions:以下是问题:
If the email abc@xyz.com
is picked up from a database and passed to the method setEmailAddress
then since the html crawled by the bot in the first place will not find an email, do I still need to obfuscate it on the page?如果电子邮件
abc@xyz.com
是从数据库中提取并传递给方法setEmailAddress
那么由于机器人首先抓取的 html 将找不到电子邮件,我还需要在页面上对其进行混淆吗?
You will notice that the link text of anchor is not obfuscated.您会注意到anchor 的链接文本没有被混淆。 If I need to obfuscate that part too, is it possible to achieve that without using
eval
?如果我也需要混淆那部分,是否可以在不使用
eval
情况下实现?
Note : I already have a Contact form on the page that uses reCaptcha but this query is for when I wish to have an email visible on the page as well.注意:我已经在使用reCaptcha的页面上有一个联系表单,但此查询适用于我希望在页面上也显示电子邮件的情况。
EDIT: After reading the comments, I think I need to emphasize on the question no.编辑:阅读评论后,我想我需要强调的问题没有。 2 above.
2 以上。 Please see above updated code.
请参阅上面的更新代码。
As you can see, I am trying to hide/obfuscate the link text as well.如您所见,我也在尝试隐藏/混淆链接文本。 I understand that I need to
document.write
the produced result by the method obfuscateString
but using eval
for that is not recommended.我知道我需要通过
obfuscateString
方法document.write
生成的结果,但不建议使用eval
。 Is there a way to obfuscate this part of text too?有没有办法混淆这部分文本?
So I ended up using the following:所以我最终使用了以下内容:
$(document).ready(function() { setEmailAddress("abc@xyz.com"); }); function setEmailAddress(emailValue) { $("#spanTest").append($("<a>").attr("href", "javascript:location='mailto:" + obfuscateString(emailValue) + "';void 0").html("<img src='https://i.stack.imgur.com/CFrNW.png' />")); } function obfuscateString(plaintext) { var obfuscated = ""; for (var i = 0; i < plaintext.length; i++) { obfuscated += "\\\\u00\u0026quot; + plaintext.charCodeAt(i).toString(16); } return obfuscated; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span id="spanTest"></span>
That is, I changed the part in the code for the link text.也就是说,我更改了链接文本代码中的部分。 From this:
由此:
.text(obfuscateString(emailValue))
to this:对此:
.html("<img src='https://i.stack.imgur.com/CFrNW.png' />")
It will render an image (of the email) in place of text.它将呈现(电子邮件的)图像代替文本。 Your suggestions are still welcome.
仍然欢迎您的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.