Facebook Post Extender

Shows all posts from a searched user, prioritizing unseen posts.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

Advertisement:

// ==UserScript==
// @name         Facebook Post Extender
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Shows all posts from a searched user, prioritizing unseen posts.
// @author       You
// @match        https://www.facebook.com/*
// @grant        GM_addStyle
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// ==/UserScript==

(function() {
    'use strict';

    // Configuration
    const targetUserID = '100000603742441'; // Replace with the target User ID
    const loadInterval = 2000; // Time between attempts to load more posts (milliseconds)
    const maxPostsToLoad = 50; // Maximum number of additional posts to load.

    // GM_addStyle to fix some facebook styling issues
    GM_addStyle(`
        .fbPostLoader {
            margin-top: 10px;
            text-align: center;
            cursor: pointer;
        }
    `);

    // Function to load more posts
    function loadMorePosts(userId) {
        const moreLink = document.querySelector('a[href*="more"]'); // Typical "See More" link
        if (!moreLink) {
            console.log("No more 'See More' link found.");
            return;
        }

        moreLink.click();

        setTimeout(() => { //Give the post time to load
            const posts = document.querySelectorAll('div[data-testid="post"]');
            if(posts.length >= maxPostsToLoad){
              console.log("Max posts loaded.");
              return;
            }
            loadMorePosts(userId);
        }, loadInterval);
    }

    // Function to fetch posts directly using the Graph API
    function fetchPostsFromGraphAPI(userId, after) {
        $.ajax({
            url: `/v15.0/${userId}/posts?fields=id,message,created_time,likes.summary(true),comments.summary(true)&limit=25&after=${after}`,
            dataType: 'json',
            success: function(response) {
                if (response.data && response.data.length > 0) {
                    const postsContainer = document.querySelector('div[data-testid="root"]'); // Find container
                    response.data.forEach(postData => {
                        const postElement = document.createElement('div');
                        postElement.setAttribute('data-testid', 'post');
                        postElement.innerHTML = `
                            <div class="fbPost">
                                <p>${postData.message || ''}</p>
                                <p>Created: ${postData.created_time}</p>
                                <p>Likes: ${postData.likes.summary.total_count}</p>
                                <p>Comments: ${postData.comments.summary.total_count}</p>
                            </div>
                        `;
                        postsContainer.insertBefore(postElement, postsContainer.firstChild); //Insert at beginning
                    });

                    if (response.paging && response.paging.cursors && response.paging.cursors.after) {
                        fetchPostsFromGraphAPI(userId, response.paging.cursors.after);
                    }
                }
            }
        });
    }


    // Main function to execute after page loads
    function main() {
        const searchInput = document.querySelector('input[name="q"]'); // Get the search input.
        if (searchInput) {
            searchInput.addEventListener('keyup', function(event) {
                if (event.key === 'Enter') { // Trigger on Enter keypress
                    setTimeout(() => {  //Wait for the search results to load
                       const profileLinks = document.querySelectorAll('a[href*="/profile.php?id="]');

                       profileLinks.forEach(link => {
                           const userId = link.href.match(/id=(\d+)/)[1];
                           if (userId === targetUserID) {
                               console.log(`Target user found with ID: ${userId}`);
                               //Load more posts from the more link if present
                               loadMorePosts(userId);
                               //Fetch posts using the Graph API
                               fetchPostsFromGraphAPI(userId, '');
                           }
                       });
                    }, 1500); // Wait a bit for search results to load
                }
            });
        }
    }

    // Run the main function
    main();

})();