Facebook Post Extender

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

Advertisement:

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

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();

})();