Preventing Comment Spam with JavaScript bot detection

Update: Due to lack of time and interest (on my part), I am no longer maintaining JSSpamBlock or ImageScaler.

I got my first comment spam on this blog the other day. It inspired me to try an idea I got a few months back. My theory was that these bots aren’t very smart – they are programmed to post as many comments as possible on as many sites as possible, hoping that a handful of these comments would get past whatever system the blogger was using to prevent spam. I hypothesized that these bots did not understand JavaScript, and that by requiring some JavaScript to run in the browser I would be able to check with reasonable accuracy weather the comment was submitted by a human or a bot.

I wrote up a simple plugin to test the theory. I checked the logs to find that I was right. In fact, most of the bots that were spamming my blog did not even include the hidden element, which indicates that they were posting to the wp-comments-post.php file directly rather than accessing the form first. The bots that did access the form did not execute the JavaScript and therefore their comments were blocked. Since the trick only involves JavaScript, most users will not even notice the difference. Users without JavaScript simply need to follow the given instructions to copy a number to a text box in order to prove they are human. This is what users without JavaScript will see:

JSSpamBlock Screenshot

JSSpamBlock Screenshot

If you want to use JSSpamBlock on your blog, check out the JSSpamBlock project page.

21 Comments

  1. Tony says:

    That’s actually quite cool. A lot of blogs use some capcha/challenge against bots, at the expense of annoying users. Having a JavaScript autofill that field is simply… brilliant.

  2. Jeff Kee says:

    Hey Paul,

    keep up the good posts.

    I use Akismet spam protection, and there’s also brian’s threated-comments plugin which allows you to change the name of your wordpress comment post file. mine is something liek “i-hate-spamming-assholes.php” or somethign like that.

    I still get HUNDREDS of spam filtered through teh Akis met spam protection, and once ina while I get one slipping by.

  3. Jared says:

    Great job hopefully this will stop the constant spam on my site. Also noted you’re also from halifax as well :)

  4. Paul Butler says:

    Cool, small world, eh?

  5. Fred says:

    Is there any way so that I can use this plugin to present a challenge regardless of javascript being present?

  6. Paul Butler says:

    Fred, you could remove everything between < script > and < /script > and it will function as you want. The reason I do not provide this as an option is that my understanding is that there are already several scripts that do what you are looking for. If this isn’t the case, I can make an official version that works that way.

    So far, I haven’t seen any bots get past it.

  7. Fred says:

    Thanks Paul I’ll give the full version a try 1st.

  8. Michele says:

    I personally use Akismet on all the blogs I run, still this looks like an interesting approach. Good job!

  9. sadara says:

    paul -

    thanks for the plugin. elegant!

    a suggestion:
    it would be helpful if you included – maybe in the readme.txt – the SQL for creating the database table manually. for me it wasn´t such a big deal to extract it from the .php script, but others maybe not find it so easy.

  10. Glenn Dixon says:

    I’ve been using Akismet for awhile, but it has always bugged me that it does might actually *encourage* spammers. After all, the post was successful as far as the bot can tell….no error messages. I’d rather the spam not get into my database to begin with. Thanks for the great script!

  11. Paul Butler says:

    Glenn, I’m glad you like it. Unfortunately, based on my experience with spam bots, I doubt they even bother to check the output after attempting to post a comment, but if they do, the plugin will sere them up a nice error message.

  12. [...] way JSSpamBlock has evolved since I first released it has reminded me why I love open-source. From day one, I had users pointing out bugs and features [...]

  13. Ilya says:

    An amazing, an elegant and a human friendly solution! Thanks a lot! If I were a painter I would create some masterpiece where a human defeats the spam bot! :) good luck!

  14. [...] while ago I installed Paul Butler’s JSSpamBlock on my Wordpress blog here.  His original idea is simple and brilliant:  Spambots don’t [...]

  15. Patricia says:

    Well, I’m rather sceptical about this. There were so many programs against spam that seemed brilliant and all of them failed some time later. Will check this one out soon!

  16. RC says:

    Not Using WordPress…does this work for FrontPage apps using JavaScript? If so, how do you install it?

  17. Paul Butler says:

    RC – It could work for FrontPage, but not without modification. Others have already modified it to work with Perl and other PHP apps.

  18. ken says:

    I want to test the comment

  19. Jai says:

    This method is good to protect spam. :)

Leave a Reply