Obtener Trending Posts de Reddit

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

目前為 2023-06-03 提交的版本,檢視 最新版本

// ==UserScript==
// @license MIT
// @name         Obtener Trending Posts de Reddit
// @namespace    https://www.example.com
// @version      1.0
// @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
                document.getElementById('trendingPosts').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}`;
                    document.getElementById('trendingPosts').appendChild(postElement);
                });
            })
            .catch(error => {
                console.log('Ha ocurrido un error:', error);
            });
    }

    // Creamos la ventana y el botón
    const container = document.createElement('div');
    container.className = 'container';
    container.innerHTML = `
        <h1>Trending Posts de Reddit</h1>
        <button id="getTrendingBtn">Obtener Trending Posts</button>
        <div id="trendingPosts"></div>
    `;
    document.body.appendChild(container);

    // Evento click del botón
    document.getElementById('getTrendingBtn').addEventListener('click', getTrendingPosts);
})();