Search code examples
javascriptfacebookfacebook-social-plugins

Detecting Comment to Social Plugin with Javascript


Facebook lets you include a comment box that visitors to your site can leave comments on. It exists as an iframe, and that allows users to comment as themselves without revealing their identity to you (the site owner).

http://developers.facebook.com/docs/reference/plugins/comments/

I would like to be able to detect when someone leaves a comment using this -- I don't want to know who they are, what their comment was, or any other details (just that they left the comment).

I can't seem to find a way to do it, but here's what I've tried or thought of doing:

  1. Facebook doesn't appear to expose any Javascript events, but maybe I missed them?
  2. Comments that have been listed are added below the comment field -- I thought maybe I could detect when that field grew or changed. It seems that I can't (iframe doesn't change height), and it could be because someone else left a comment at the same time as you.
  3. Click-jacking for good? I thought maybe I could place an invisible DIV above the "Comment" button, then when someone clicks remove it and pass the click event through. It seems that I can't do that either (probably for good reason).

Any ideas how I can detect when comments are made?

Thanks!


Solution

  • Facebook does expose events for such a case:

    FB.Event.subscribe("comment.create", function (response) {
        console.log('create', response);
    });
    
    FB.Event.subscribe("comment.remove", function (response) {
        console.log('remove', response);
    });
    

    More here: FB.Event.subscribe - Facebook Developers.