X (Twitter) Feed to Markdown

Extracts content from the X (Twitter) feed and converts it to Markdown format, handling tweets, quotes, reposts, and threads while filtering out ads.

// ==UserScript==
// @name         X (Twitter) Feed to Markdown
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Extracts content from the X (Twitter) feed and converts it to Markdown format, handling tweets, quotes, reposts, and threads while filtering out ads.
// @match        https://x.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let isMonitoring = false;
    let collectedTweets = new Map();
    let observer;

    const button = document.createElement('button');
    button.textContent = '开始转换Markdown';
    Object.assign(button.style, {
        position: 'fixed',
        top: '10px',
        right: '10px',
        zIndex: '9999',
        padding: '8px 16px',
        backgroundColor: '#1DA1F2',
        color: 'white',
        border: 'none',
        borderRadius: '5px',
        cursor: 'pointer',
        fontSize: '14px'
    });
    document.body.appendChild(button);

    button.addEventListener('click', toggleMonitoring);

    function toggleMonitoring() {
        if (isMonitoring) {
            stopMonitoring();
            displayCollectedTweets();
        } else {
            startMonitoring();
        }
    }

    function startMonitoring() {
        isMonitoring = true;
        button.textContent = '停止并导出Markdown';
        button.style.backgroundColor = '#FF4136';
        collectedTweets.clear();
        console.log("开始监控推文...");

        document.querySelectorAll('article[data-testid="tweet"]').forEach(processTweet);

        const config = { childList: true, subtree: true };
        observer = new MutationObserver(mutations => {
            for (const mutation of mutations) {
                if (mutation.addedNodes.length) {
                    mutation.addedNodes.forEach(node => {
                        if (node.nodeType === Node.ELEMENT_NODE) {
                            if (node.matches('article[data-testid="tweet"]')) {
                                processTweet(node);
                            }
                            node.querySelectorAll('article[data-testid="tweet"]').forEach(processTweet);
                        }
                    });
                }
            }
        });

        observer.observe(document.body, config);
    }

    function stopMonitoring() {
        isMonitoring = false;
        button.textContent = '开始转换Markdown';
        button.style.backgroundColor = '#1DA1F2';
        if (observer) {
            observer.disconnect();
        }
        console.log("停止监控。");
    }

    function processTweet(tweet) {
        if (tweet.querySelector('[data-testid="promotedTweet"]')) return;
        const timeElement = tweet.querySelector('time[datetime]');
        if (timeElement && timeElement.closest('div[data-testid="User-Name"]')?.nextElementSibling?.textContent?.includes('Ad')) {
             return;
        }

        const tweetData = formatTweet(tweet);
        if (tweetData && tweetData.url && !collectedTweets.has(tweetData.url)) {
            collectedTweets.set(tweetData.url, tweetData.markdown);
        }
    }

    function displayCollectedTweets() {
        if (collectedTweets.size === 0) {
            alert('没有收集到任何推文。');
            return;
        }

        const sortedTweets = Array.from(collectedTweets.values()).sort((a, b) => {
             const timeMatchA = a.match(/\*\*发布时间\*\*: (.*)/);
             const timeMatchB = b.match(/\*\*发布时间\*\*: (.*)/);
             if (!timeMatchA || !timeMatchB) return 0;
             const timeA = new Date(timeMatchA[1]);
             const timeB = new Date(timeMatchB[1]);
             return timeB - timeA;
        });

        const markdownOutput = sortedTweets.join('\n\n---\n\n');
        const newWindow = window.open('', '_blank');
        newWindow.document.write('<pre style="white-space: pre-wrap; word-wrap: break-word; padding: 10px;">' + markdownOutput.replace(/</g, "&lt;").replace(/>/g, "&gt;") + '</pre>');
        newWindow.document.title = 'Twitter Feed as Markdown';
    }

    /**
     * 提取节点内的文本,处理表情和链接
     */
    function extractTextContent(element) {
        if (!element) return '';
        let text = '';
        element.childNodes.forEach(node => {
            if (node.nodeType === Node.ELEMENT_NODE) {
                if (node.tagName === 'IMG') {
                    text += node.alt;
                } else if (node.tagName === 'A') {
                    const url = node.href;
                    if (!url.includes('/photo/') && !url.includes('/video/')) {
                       text += `[${node.textContent}](${url})`;
                    }
                } else {
                    text += node.textContent;
                }
            } else {
                text += node.textContent;
            }
        });
        return text.trim();
    }

    /**
     * 从推文元素中提取并格式化信息
     */
    function formatTweet(tweet) {
        const timeElement = tweet.querySelector('time');
        if (!timeElement) return null;

        const linkElement = timeElement.closest('a');
        if (!linkElement) return null;

        const tweetUrl = 'https://x.com' + linkElement.getAttribute('href');
        const authorHandle = `@${tweetUrl.split('/')[3]}`;
        const postTime = timeElement.getAttribute('datetime');

        // --- 内容提取 ---
        const mainContentElement = tweet.querySelector('div[data-testid="tweetText"]');
        const mainContent = extractTextContent(mainContentElement);

        // --- 引用推文 (Quote Tweet) ---
        let quoteContent = '';
        const quoteHeader = Array.from(tweet.querySelectorAll('span')).find(s => s.textContent === 'Quote');
        if (quoteHeader) {
            // "Quote" 标题的父容器的下一个兄弟节点通常是引用的推文块
            const quoteContainer = quoteHeader.parentElement.nextElementSibling;
            if (quoteContainer && quoteContainer.getAttribute('role') === 'link') {
                const quoteAuthorEl = quoteContainer.querySelector('[data-testid="User-Name"]');
                const quoteAuthor = quoteAuthorEl ? quoteAuthorEl.textContent.replace(/\n/g, ' ').replace(/\s+/g, ' ').trim() : '未知作者';

                // 引用推文的文本在 lang 属性的 div 中
                const quoteTextEl = quoteContainer.querySelector('div[lang]');
                const quoteText = extractTextContent(quoteTextEl);

                const quoteLines = `**${quoteAuthor}**: ${quoteText}`.split('\n');
                quoteContent = `\n\n${quoteLines.map(line => `> ${line}`).join('\n> ')}`;
            }
        }


        // --- 分享链接卡片 ---
        let sharedLink = '';
        const cardWrapper = tweet.querySelector('[data-testid="card.wrapper"]');
        if (cardWrapper) {
            const cardLinkEl = cardWrapper.querySelector('a');
            if(cardLinkEl) {
                const cardUrl = cardLinkEl.href;
                // 尝试提取卡片标题
                const detailContainer = cardWrapper.querySelector('[data-testid$="detail"]');
                let cardTitle = '';
                if (detailContainer) {
                    const spans = detailContainer.querySelectorAll('span');
                    // 标题通常是第二个非空的 span
                    cardTitle = spans.length > 1 ? spans[1].textContent : '链接';
                } else {
                     // 尝试从大的媒体卡片中获取标题
                    const largeMediaTitleEl = cardWrapper.querySelector('div[class*="r-fdjqy7"] span');
                    cardTitle = largeMediaTitleEl ? largeMediaTitleEl.textContent : '链接';
                }

                sharedLink = `\n- **分享链接**: [${cardTitle.trim()}](${cardUrl})`;
            }
        }


        // --- 转推 (Repost) 处理 ---
        const socialContext = tweet.querySelector('[data-testid="socialContext"]');
        let repostedBy = '';
        if (socialContext && socialContext.textContent.toLowerCase().includes('reposted')) {
            repostedBy = `> *由 ${socialContext.textContent.replace(/reposted/i, '').trim()} 转推*\n\n`;
        }

        // --- 串推 (Thread) 标记 ---
        let threadIndicator = '';
        const hasThreadLink = Array.from(tweet.querySelectorAll('a[role="link"] span')).some(span => span.textContent === 'Show this thread');
        if (hasThreadLink) {
            threadIndicator = `- **串推**: 是\n`;
        }

        // --- 组合 Markdown 输出 ---
        let markdown = `${repostedBy}- **原文链接**: ${tweetUrl}\n`;
        markdown += `- **作者**: ${authorHandle}\n`;
        markdown += `- **发布时间**: ${postTime}\n`;
        markdown += threadIndicator;
        markdown += `- **推文内容**:\n${mainContent}${quoteContent}`;
        markdown += sharedLink;

        return {
            url: tweetUrl,
            markdown: markdown
        };
    }
})();