Greasy Fork

Greasy Fork is available in English.

Stage1st 引用内容快速查看

在Stage1st论坛点击引用链接时显示被引用内容的悬浮框

当前为 2025-06-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         Stage1st 引用内容快速查看
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  在Stage1st论坛点击引用链接时显示被引用内容的悬浮框
// @author       mmm
// @match        https://stage1st.com/2b/forum.php?mod=viewthread&tid=*
// @match        https://stage1st.com/2b/thread-*
// @grant        GM_xmlhttpRequest
// @connect      stage1st.com
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 创建悬浮框样式
    const style = document.createElement('style');
    style.textContent = `
        .s1-quote-popup {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: #f5f5f5;
            border: 1px solid #ddd;
            padding: 10px;
            max-width: 80%;
            max-height: 80vh;
            z-index: 800;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
            border-radius: 4px;
            font-size: 14px;
            line-height: 1.5;
            overflow: auto;
            display: none;
        }
        .s1-quote-popup-content {
            overflow: auto;
            max-height: calc(80vh - 50px);
        }
        .s1-quote-popup-close {
            position: absolute;
            top: 5px;
            right: 5px;
            cursor: pointer;
            color: #999;
            font-size: 16px;
            font-weight: bold;
            z-index: 1001;
        }
        .s1-quote-loading {
            color: #999;
            padding: 20px;
            text-align: center;
        }
        .s1-quote-popup-header {
            font-weight: bold;
            margin-bottom: 10px;
            padding-bottom: 5px;
            border-bottom: 1px solid #ddd;
        }
        .s1-quote-popup img {
            max-width: 100%;
            height: auto;
        }
    `;
    document.head.appendChild(style);

    // 创建悬浮框元素(只创建一次)
    const popup = document.createElement('div');
    popup.className = 's1-quote-popup';

    const closeBtn = document.createElement('div');
    closeBtn.className = 's1-quote-popup-close';
    closeBtn.textContent = '×';

    const header = document.createElement('div');
    header.className = 's1-quote-popup-header';
    header.textContent = '引用内容';

    const content = document.createElement('div');
    content.className = 's1-quote-popup-content';

    popup.appendChild(closeBtn);
    popup.appendChild(header);
    popup.appendChild(content);
    document.body.appendChild(popup);

    // 显示/隐藏悬浮框
    function showPopup() {
        popup.style.display = 'block';
    }

    function hidePopup() {
        popup.style.display = 'none';
    }

    // 修复懒加载图片
    function fixLazyLoadImages(html) {
        const tempDiv = document.createElement('div');
        tempDiv.innerHTML = html;

        // 将file属性转为src属性
        tempDiv.querySelectorAll('img[file]').forEach(img => {
            const fileValue = img.getAttribute('file');
            if (fileValue) {
                img.setAttribute('src', fileValue);
                img.removeAttribute('file');
                img.removeAttribute('onmouseover'); // 移除可能的事件监听
                img.removeAttribute('onload');     // 移除可能的事件监听
            }
        });

        return tempDiv.innerHTML;
    }

    // 从URL中提取帖子ID和页码
    function extractIdsFromUrl(url) {
        const pidMatch = url.match(/pid=(\d+)/);
        const ptidMatch = url.match(/ptid=(\d+)/);
        return {
            pid: pidMatch ? pidMatch[1] : null,
            ptid: ptidMatch ? ptidMatch[1] : null
        };
    }

    // 获取帖子内容
    function fetchPostContent(url, callback) {
        content.innerHTML = '<div class="s1-quote-loading">加载中...</div>';
        showPopup();

        GM_xmlhttpRequest({
            method: "GET",
            url: url,
            onload: function(response) {
                const parser = new DOMParser();
                const doc = parser.parseFromString(response.responseText, "text/html");
                const ids = extractIdsFromUrl(url);
                const quoteContent = doc.querySelector(`#pid${ids.pid} .t_f`);

                if (quoteContent) {
                    const fixedHtml = fixLazyLoadImages(quoteContent.innerHTML);
                    callback(fixedHtml);
                } else {
                    callback("无法找到引用内容");
                }
            },
            onerror: function() {
                callback("加载引用内容失败");
            }
        });
    }

    // 处理引用链接点击
    function handleQuoteClick(e) {
        e.preventDefault();
        e.stopPropagation();

        const link = e.currentTarget;
        const href = link.href;

        fetchPostContent(href, function(htmlContent) {
            content.innerHTML = htmlContent;

            // 为内容中的引用链接添加事件
            content.querySelectorAll('.quote a:has(font)').forEach(newLink => {
                newLink.addEventListener('click', function(ev) {
                    ev.preventDefault();
                    handleQuoteClick(ev);
                });
            });
        });
    }

    // 关闭按钮事件
    closeBtn.addEventListener('click', hidePopup);

    // 点击页面其他地方关闭悬浮框
    document.addEventListener('click', function(e) {
        if (!popup.contains(e.target) && !e.target.closest('.quote a:has(font)')) {
            hidePopup();
        }
    });

    // 为页面中的引用链接添加点击事件
    function initQuoteLinks() {
        document.querySelectorAll('.quote a:has(font)').forEach(link => {
            link.addEventListener('click', function(e) {
                handleQuoteClick(e);
            });
        });
    }

    // 初始化
    initQuoteLinks();
})();