Obtener Trending Posts de Reddit

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

当前为 2023-06-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @license MIT
// @name         Obtener Trending Posts de Reddit
// @namespace    https://www.example.com
// @version      1.0.1
// @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';

    // Función para obtener los trending posts de Reddit
    function getTrendingPosts() {
        fetch('https://www.reddit.com/r/all.json')
            .then(response => response.json())
            .then(data => {
                // Limpiamos el contenido anterior
                const trendingPostsElement = document.getElementById('trendingPosts');
                if (trendingPostsElement) {
                    trendingPostsElement.innerHTML = '';
                }

                // 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}`;
                    if (trendingPostsElement) {
                        trendingPostsElement.appendChild(postElement);
                    }
                });
            })
            .catch(error => {
                console.log('Ha ocurrido un error:', error);
            });
    }

    // Crear el botón de Trending Posts
    function createTrendingButton() {
        const buttonElement = document.createElement('button');
        buttonElement.id = 'getTrendingBtn';
        buttonElement.textContent = 'Obtener Trending Posts';
        buttonElement.addEventListener('click', getTrendingPosts);

        const headerElement = document.querySelector('header');
        if (headerElement) {
            headerElement.appendChild(buttonElement);
        }
    }

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

        // Crear el botón de Trending Posts
        createTrendingButton();
    }
})();