- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to auto like all the comments on a Facebook post using JavaScript?
We would first need to access the post's comment section through the Facebook API. Then, we would use a loop to iterate through each comment and use the API to like each one. Finally, we would need to implement error handling in case any issues arise during the process.
Approach
If you want to use JavaScript to automatically like all comments on a Facebook post, here are some requirements you must meet −
You would need to use the Facebook Graph API to first get all comments on a post.
For each comment, you would need to call the “like” API endpoint.
Here is the approach in pseudo code for the same −
// 1. Get all comments on a post FB.api( ‘/{post-id}/comments’, ‘GET’, {}, function(response) { // 2. For each comment, call the like API endpoint for (var i=0; i < response.data.length; i++) { FB.api( ‘/’ + response.data[i].id + ‘/likes’, ‘POST’, {}, function(response) { // handle response } ); } } );
Example
Assuming you have a Facebook Access Token with the required permissions −
Go to the Facebook Graph API Explorer tool − https://developers.facebook.com/tools/explorer/
Select your app from the dropdown menu at the top.
Enter the following Graph API request into the text field and click “Submit” −
{post-id}/comments?limit=500&fields=id
This will return a JSON object with a list of all the comments on the specified post, including their IDs.
Copy this list of IDs and paste them into the following JavaScript code −
var commentIDs = [ID1, ID2, ID3]; var accessToken = 'Your-Access-Token'; var numToLike = commentIDs.length; var likeCounter = 0; function likeNextComment() { if (likeCounter < numToLike) { FB.api( "/" + commentIDs[likeCounter] + "/likes", "POST", { access_token: accessToken }, function(response) { if (response && !response.error) { likeCounter++; likeNextComment(); } } ); } } likeNextComment();
This code will like all the comments on the specified post, one at a time. You will need to replace {post-id} with the actual ID of the post, and Your-Access-Token with a valid Access Token.
You can run this code by opening the JavaScript console in your browser (usually Control+Shift+K in Chrome) and pasting it in.