// ==UserScript== // @name Reddit Comment and Post Extractor // @namespace http://tampermonkey.net/ // @version 1 // @description Extract Reddit comments and posts as JSON with configurable options // @author anassk // @match https://www.reddit.com/* // @match https://old.reddit.com/* // @grant GM_registerMenuCommand // @license MIT // ==/UserScript== (function() { 'use strict'; let isUIVisible = false; let container = null; // Enhanced Styles with smaller UI const STYLES = ` .reddit-extractor { position: fixed; top: 20px; right: 20px; background: white; padding: 16px; border: 1px solid #e0e0e0; border-radius: 8px; z-index: 9999; box-shadow: 0 8px 24px rgba(0,0,0,0.12); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto; width: 280px; animation: slideIn 0.3s cubic-bezier(0.4, 0, 0.2, 1); } @keyframes slideIn { from { opacity: 0; transform: translateX(40px); } to { opacity: 1; transform: translateX(0); } } .reddit-extractor-close { position: absolute; top: 12px; right: 12px; cursor: pointer; font-size: 20px; color: #666; line-height: 1; padding: 4px 8px; border-radius: 50%; transition: all 0.2s ease; } .reddit-extractor-close:hover { background: rgba(0,0,0,0.05); color: #333; } .reddit-extractor-section { margin-bottom: 16px; padding: 12px; background: #f8f9fa; border-radius: 6px; border: 1px solid #e9ecef; transition: box-shadow 0.2s ease; } .reddit-extractor-section:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.05); } .reddit-extractor-header { margin-bottom: 12px; padding-bottom: 6px; border-bottom: 2px solid #1a1a1b; } .reddit-extractor-header h3 { margin: 0; font-size: 16px; color: #1a1a1b; font-weight: 600; letter-spacing: -0.3px; } .reddit-extractor-btn { display: inline-flex; align-items: center; justify-content: center; background: #0079d3; color: white; border: none; border-radius: 4px; padding: 8px 12px; cursor: pointer; font-size: 13px; font-weight: 600; margin-right: 6px; transition: all 0.2s ease; min-width: 70px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .reddit-extractor-btn:hover { background: #005d9f; transform: translateY(-1px); box-shadow: 0 4px 8px rgba(0,0,0,0.15); } .reddit-extractor-label { display: flex; align-items: center; margin-bottom: 8px; cursor: pointer; font-size: 13px; padding: 4px; border-radius: 4px; transition: background 0.2s ease; } .reddit-extractor-label:hover { background: rgba(0,0,0,0.03); } .reddit-extractor-checkbox { margin-right: 8px; cursor: pointer; width: 14px; height: 14px; } .reddit-extractor-input { width: 70px; padding: 6px; border: 1px solid #ddd; border-radius: 4px; margin-top: 4px; transition: all 0.2s ease; font-size: 13px; } .reddit-extractor-status { margin-top: 12px; padding: 8px; border-radius: 4px; font-size: 13px; text-align: center; display: none; font-weight: 500; animation: fadeIn 0.3s ease; } .status-success { background: #e8f5e9; color: #2e7d32; border: 1px solid #c8e6c9; } .status-error { background: #ffebee; color: #c62828; border: 1px solid #ffcdd2; } `; // Extract post data function extractPostData() { try { const titleElement = document.querySelector('h1[slot="title"]'); const creditBarElement = document.querySelector('div[slot="credit-bar"]'); const postContentElement = document.querySelector('div[slot="text-body"]'); return { title: titleElement ? titleElement.textContent.trim() : '', creditBar: creditBarElement ? { subreddit: creditBarElement.querySelector('.subreddit-name a')?.textContent.trim() || '', author: creditBarElement.querySelector('.author-name')?.textContent.trim() || '', timestamp: creditBarElement.querySelector('faceplate-timeago time')?.getAttribute('datetime') || '' } : null, content: postContentElement ? postContentElement.textContent.trim() : '' }; } catch (error) { console.error('Error extracting post data:', error); return null; } } // Extract comment data function extractCommentData(comment) { try { const username = comment.getAttribute('author') || comment.querySelector('a[href^="/user/"]')?.textContent?.trim() || comment.querySelector('.author')?.textContent?.trim() || 'unknown'; const votesStr = comment.querySelector('shreddit-comment-action-row')?.getAttribute('score') || comment.querySelector('.score')?.textContent || '0'; const votes = parseInt(votesStr) || 0; const permalink = comment.getAttribute('permalink') || comment.querySelector('a.permalink')?.getAttribute('href') || ''; const dateElem = comment.querySelector('time') || comment.querySelector('.live-timestamp'); const date = dateElem ? new Date(dateElem.getAttribute('datetime') || dateElem.getAttribute('title')).toLocaleString() : ''; const textContent = comment.querySelector('[id$="-comment-rtjson-content"]')?.textContent?.trim() || comment.querySelector('.usertext-body')?.textContent?.trim() || ''; const depth = parseInt(comment.getAttribute('depth')) || 0; return { username, votes, date, permalink: permalink ? new URL(permalink, window.location.origin).pathname : '', profileUrl: `/user/${username}`, content: textContent, depth }; } catch (error) { console.error('Error extracting comment data:', error); return null; } } // Create UI function createExtractorUI() { if (container) { container.style.display = 'block'; return container; } container = document.createElement('div'); container.className = 'reddit-extractor'; const styleSheet = document.createElement('style'); styleSheet.textContent = STYLES; document.head.appendChild(styleSheet); container.innerHTML = ` ×