A new JavaScript code snippet has been released, designed to integrate Facebook comment functionality into web applications. This script enables real-time tracking of new comments posted on Facebook, directly feeding them into a web application’s backend.
The core of the system is a JavaScript function, `fb_newcomment`, which is triggered whenever a new comment is created via the Facebook platform. This function is designed to prevent duplicate submissions by utilizing a flag variable. Upon receiving a new comment, it sends an asynchronous AJAX POST request to a ‘/ajax/comment’ endpoint. The request includes crucial data such as the comment’s message, the ID of the element it’s associated with, a flag indicating it originated from facebook, and a user ID. The script also includes the necessary CSRF token for security, retrieved from a meta tag in the HTML.
Upon successful completion of the AJAX request, the script updates a counter displayed on the webpage. It increments the displayed comment count, providing immediate visual feedback to users about new activity. The script is initialized with specific Facebook SDK parameters, including an `appId` (‘2994133110631445’), enabling automatic logging of app events and the use of Facebook Markup Language (FBML) with version ‘v8.0‘.
The Facebook SDK is loaded asynchronously using a standard JavaScript pattern. This ensures that the Facebook SDK script is injected into the document only if it hasn’t been loaded already, optimizing page load performance. The script is sourced from ‘https://connect.facebook.net/ru_RU/sdk.js’,ensuring compatibility with the Russian language interface.