Obtener Trending Posts de Reddit

Obtiene los trending posts de Reddit y los muestra en una página

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @license MIT
// @name         Obtener Trending Posts de Reddit
// @namespace    https://www.example.com
// @version      1.0.3
// @description  Obtiene los trending posts de Reddit y los muestra en una página
// @author       Tu Nombre
// @match        https://www.reddit.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let isFetchingPosts = false; // Variable para controlar si se están obteniendo más publicaciones
    let after = ''; // ID de la última publicación obtenida

    // Función para obtener los trending posts de Reddit
    function getTrendingPosts() {
        if (isFetchingPosts) {
            return; // Evitar múltiples llamadas mientras se están obteniendo publicaciones
        }

        isFetchingPosts = true;

        fetch(`https://www.reddit.com/r/all.json?after=${after}`)
            .then(response => response.json())
            .then(data => {
                isFetchingPosts = false;

                // Recorremos los trending posts y los mostramos en la página
                data.data.children.forEach(post => {
                    const postTitle = post.data.title;
                    const postAuthor = post.data.author;
                    const postScore = post.data.score;

                    const postElement = document.createElement('p');
                    postElement.innerHTML = `<strong>${postTitle}</strong> - Autor: ${postAuthor} - Puntuación: ${postScore}`;
                    const trendingPostsElement = document.getElementById('trendingPosts');
                    if (trendingPostsElement) {
                        trendingPostsElement.appendChild(postElement);
                    }
                });

                after = data.data.after; // Actualizamos el ID de la última publicación obtenida
            })
            .catch(error => {
                console.log('Ha ocurrido un error:', error);
                isFetchingPosts = false; // Reiniciamos la variable en caso de error
            });
    }

    // Verificar si estamos en una página de Reddit
    const isRedditPage = window.location.hostname === 'www.reddit.com';
    if (isRedditPage) {
        // Crear la estructura de la página
        const containerElement = document.createElement('div');
        containerElement.className = 'container';
        containerElement.innerHTML = `
            <h1>Trending Posts de Reddit</h1>
            <div id="trendingPosts"></div>
        `;
        document.body.appendChild(containerElement);

        // Registrar el evento de scroll para cargar más publicaciones cuando sea necesario
        window.addEventListener('scroll', function() {
            const scrollPosition = window.innerHeight + window.pageYOffset;
            const documentHeight = document.documentElement.scrollHeight;
            const buffer = 200; // Espacio adicional antes de llegar al final de la página

            if (scrollPosition >= documentHeight - buffer) {
                getTrendingPosts();
            }
        });

        // Obtener los primeros trending posts al cargar la página
        getTrendingPosts();
    }
})();