(function ($) { Drupal.behaviors.cs_facebook_comments = { attach: function (context, settings) { try { var timer = setInterval(function() { if(typeof FB != 'undefined') { clearInterval(timer); FB.Event.subscribe('comment.create', function(response) { var commentQuery = FB.Data.query("SELECT text, fromid, time FROM comment WHERE post_fbid='"+response.commentID+"' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url='"+response.href+"')"); var userQuery = FB.Data.query("SELECT name, profile_url FROM user WHERE uid in (select fromid from {0})", commentQuery); FB.Data.waitOn([commentQuery, userQuery], function() { var comment = commentQuery.value[0]; var user = userQuery.value[0]; var settings = { url : Drupal.settings.basePath + 'ajax/facebook/comment.create', submit: {'facebook_comment': {'response': response, 'comment': comment, 'user': user}} }; var ajax = new Drupal.ajax(false, this, settings); ajax.eventResponse(ajax, {}); }); } ); } }, 10); } catch(e) { if('console' in window) console.error(e); } } }; }(jQuery));