妖火帖子列表IFrame打开展示(兼容非全屏版)

在妖火论坛列表页面旁生成一个iframe,点击帖子后在iframe中打开显示,左右平分屏幕

// ==UserScript==
// @name         妖火帖子列表IFrame打开展示(兼容非全屏版)
// @namespace    https://yaohuo.me/
// @version      0.2
// @description  在妖火论坛列表页面旁生成一个iframe,点击帖子后在iframe中打开显示,左右平分屏幕
// @author       我黄某与赌毒不两立
// @match        *://yaohuo.me/bbs/book_list*
// @match        *://yaohuo.me/bbs/list*
// @match        *://yaohuo.me/bbslist-*
// @match        *://www.yaohuo.me/bbs/book_list*
// @match        *://www.yaohuo.me/bbs/list*
// @match        *://www.yaohuo.me/bbslist-*
// @license MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const isMobile = /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
    if (isMobile) {
        alert('(脚本作者提醒您)\r\n脚本 "妖火帖子列表IFrame打开展示(电脑版)" 不支持在移动端运行,请停止启动该脚本');
        return;
    }

    if (window.self !== window.top) {
        return;
    }

    // 创建主容器
    const mainContainer = document.createElement('div');
    mainContainer.style.display = 'flex';
    mainContainer.style.width = '100%';
    mainContainer.style.height = '100vh';
    mainContainer.style.position = 'fixed';
    mainContainer.style.top = '0';
    mainContainer.style.left = '0';
    mainContainer.style.zIndex = '1000';

    // 创建左侧内容容器(原始内容)
    const leftContainer = document.createElement('div');
    leftContainer.style.flex = '1';
    leftContainer.style.minWidth = '0'; // 防止内容溢出
    leftContainer.style.overflow = 'auto';
    leftContainer.style.backgroundColor = 'white';

    // 将原始内容移动到左侧容器
    const originalContent = document.body.cloneNode(true);
    document.body.innerHTML = '';
    leftContainer.appendChild(originalContent);

    // 创建右侧iframe容器
    const rightContainer = document.createElement('div');
    rightContainer.style.flex = '1';
    rightContainer.style.minWidth = '0';
    rightContainer.style.overflow = 'hidden';
    rightContainer.style.display = 'flex';
    rightContainer.style.flexDirection = 'column';
    rightContainer.style.borderLeft = '1px solid #ddd';

    // 创建iframe
    const iframe = document.createElement('iframe');
    iframe.style.flex = '1';
    iframe.style.width = '100%';
    iframe.style.height = '100%';
    iframe.style.border = 'none';

    rightContainer.appendChild(iframe);
    mainContainer.appendChild(leftContainer);
    mainContainer.appendChild(rightContainer);
    document.body.appendChild(mainContainer);

    // 自动调整iframe高度
    function adjustIframeHeight() {
        try {
            const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
            if (iframeDoc && iframeDoc.body) {
                // 设置iframe高度为内容高度
                iframe.style.height = iframeDoc.documentElement.scrollHeight + 'px';

                // 确保内容不会超出视口
                const maxHeight = window.innerHeight;
                if (iframeDoc.documentElement.scrollHeight > maxHeight) {
                    iframe.style.height = maxHeight + 'px';
                }
            }
        } catch (e) {
            console.log('调整高度时出错:', e);
        }
    }

    // iframe加载完成后的处理
    iframe.addEventListener('load', function() {
        const iframeDocument = this.contentDocument || this.contentWindow.document;

        // 自动调整高度
        adjustIframeHeight();

        // 监听iframe内部内容变化
        iframeDocument.addEventListener('DOMSubtreeModified', adjustIframeHeight);

        // 滚动到标题位置
        const biaotiwenziElement = iframeDocument.querySelector('.biaotiwenzi');
        if (biaotiwenziElement) {
            biaotiwenziElement.scrollIntoView({
                behavior: 'smooth',
                block: 'start'
            });
        }

        // 处理内部链接点击
        const iframeLinks = iframeDocument.querySelectorAll('a');
        iframeLinks.forEach(link => {
            link.addEventListener('click', function(event) {
                const href = this.getAttribute('href');
                if (href) {
                    const url = new URL(href, iframeDocument.baseURI);
                    if (!url.hostname.includes('yaohuo.me') || (url.hostname.includes('yaohuo.me') && url.href.includes("download.aspx"))) {
                        event.preventDefault();
                        window.open(href, '_blank');
                    }
                }
            });
        });
    });

    // 设置帖子链接点击事件
    function setupLinkEvents() {
        const links = leftContainer.querySelectorAll('.topic-link');
        links.forEach(link => {
            if (!link.hasAttribute('data-clicked')) {
                link.addEventListener('click', function(event) {
                    event.preventDefault();
                    const href = this.getAttribute('href');
                    iframe.src = href;
                    this.style.color = 'red';
                    this.setAttribute('data-clicked', 'true');
                });
            }
        });
    }

    setupLinkEvents();

    // 使用MutationObserver监听DOM变化
    const observer = new MutationObserver((mutationsList) => {
        for (const mutation of mutationsList) {
            if (mutation.type === 'childList') {
                setupLinkEvents();
            }
        }
    });

    const targetNode = leftContainer.querySelector('#KL_show_next_list') || leftContainer;
    observer.observe(targetNode, { childList: true, subtree: true });
})();