T66Y Anti-Ad-Block Defeater (v2.6 Ultimate Pro+)

Adds iframe integration fix for scripts like 'Link Previewer'. Auto-loads hidden content, auto-expands comments, fixes videos, removes all ads, and handles pagination. The ultimate all-in-one solution.

// ==UserScript==
// @name         T66Y Anti-Ad-Block Defeater (v2.6 Ultimate Pro+)
// @name:zh-CN   草榴社区反广告屏蔽破解脚本 (v2.6 )
// @namespace    http://tampermonkey.net/
// @version      2.6
// @description  Adds iframe integration fix for scripts like 'Link Previewer'. Auto-loads hidden content, auto-expands comments, fixes videos, removes all ads, and handles pagination. The ultimate all-in-one solution.
// @description:zh-CN [v2.6] 包含自动加载隐藏内容、自动展开评论、视频修复、广告移除、短回复过滤、链接修复、反广告屏蔽功能。
// @author       Expert Analyst
// @match        *://t66y.com/*
// @match        *://*.t66y.com/*
// @match        *://*.t66y.com/thread*
// @match        *://*.t66y.com/htm_data/*/*.html
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // --- 第 1 层防御:先发制人的函数“缴械”(外科手术式打击)---
    try {
        Object.defineProperty(window, 'r9aeadS', {
            value: function() {
                console.log('恶意函数 "r9aeadS" 已被成功“缴械”。第 1 层防御生效。');
                return;
            },
            writable: false,
            configurable: false
        });
    } catch (e) {
        console.error('“缴械” "r9aeadS" 失败。', e);
    }

    // --- 核心功能执行 ---
    document.addEventListener('DOMContentLoaded', () => {

        // 将图片修复功能独立出来,以便在iframe中重复调用
        function fixLazyImages() {
            try {
                document.querySelectorAll('img[ess-data]').forEach(img => {
                    const imageUrl = img.getAttribute('ess-data');
                    if (imageUrl) {
                        img.setAttribute('src', imageUrl);
                        img.removeAttribute('ess-data');
                        console.log(`图片修复:已加载图片 -> ${imageUrl}`);
                    }
                });
            } catch (e) {
                console.error('修复图片懒加载时出错。', e);
            }
        }

        function runAllOptimizations() {
            console.log('--- 开始执行页面优化脚本 (v2.6) ---');
            // --- 功能模块 1 (v2.0):高级广告屏蔽 ---
            try {
                // 1. 移除页面顶部和底部的横幅广告
                document.querySelectorAll('script').forEach(script => {
                    if (script.innerHTML.includes('spinit2()')) {
                        const parentContainer = script.parentElement;
                        if (parentContainer) {
                            parentContainer.style.display = 'none';
                            console.log('广告屏蔽:已隐藏 spinit2() 广告容器。');
                        }
                    }
                });
                const footerAd = document.querySelector('#footer');
                if (footerAd && footerAd.innerHTML.includes('spinit()')) {
                    footerAd.style.display = 'none';
                    console.log('广告屏蔽:已隐藏页脚 spinit() 广告容器。');
                }

                // 2. 移除主楼和所有回帖内的内嵌广告
                const inPostAds = document.querySelectorAll('div.tips');
                inPostAds.forEach(ad => {
                    ad.style.display = 'none';
                    const clearer = ad.nextElementSibling;
                    if (clearer && clearer.classList.contains('c')) {
                        clearer.style.display = 'none';
                    }
                });
                if (inPostAds.length > 0) {
                    console.log(`广告屏蔽:已隐藏 ${inPostAds.length} 个帖子内嵌广告。`);
                }
            } catch (e) {
                console.error('高级广告屏蔽时出错。', e);
            }

            // --- 功能模块 2 (v1.7):按长度过滤短回复 ---
            const maxReplyLength = 5;
            try {
                document.querySelectorAll('div.tpc_content[id^="cont"]').forEach(contentDiv => {
                    const postText = contentDiv.textContent.trim();
                    if (postText.length > 0 && postText.length <= maxReplyLength) {
                        const postContainer = contentDiv.closest('div.t.t2');
                        if (postContainer) {
                            postContainer.style.display = 'none';
                            console.log(`内容过滤:已隐藏短回复 -> "${postText}"`);
                        }
                    }
                });
            } catch (e) {
                console.error('按长度过滤回复时出错。', e);
            }

            // --- 功能模块 3 (v1.5):修复重定向下载链接 ---
            try {
                document.querySelectorAll('a[href*="redircdn.com/?"]').forEach(link => {
                    const originalHref = link.getAttribute('href');
                    const prefix = 'https://2023.redircdn.com/?';
                    if (originalHref && originalHref.startsWith(prefix)) {
                        let realUrlDirty = originalHref.substring(prefix.length);
                        if (realUrlDirty.endsWith('&z')) {
                            realUrlDirty = realUrlDirty.slice(0, -2);
                        }
                        const realUrlCleaned = realUrlDirty.replace(/______/g, '.');
                        link.setAttribute('href', realUrlCleaned);
                        console.log(`链接修复:已替换跳转链接 -> ${realUrlCleaned}`);
                    }
                });
            } catch (e) {
                console.error('修复下载链接时出错。', e);
            }

            // --- 功能模块 4 (v1.4):修复图片懒加载 ---
            fixLazyImages();

            // --- 功能模块 5 (v2.3):修复视频懒加载与跳转 ---
            try {
                // 修复 iframe 懒加载
                document.querySelectorAll('iframe[data-src]').forEach(videoFrame => {
                    if (!videoFrame.getAttribute('src')) {
                        const videoUrl = videoFrame.getAttribute('data-src');
                        if (videoUrl) {
                            videoFrame.setAttribute('src', videoUrl);
                            console.log(`视频修复(iframe):已加载视频 -> ${videoUrl}`);
                        }
                    }
                });
                // 修复 thumbsnap.com 跳转视频
                document.querySelectorAll('video[src*="thumbsnap.com/i/"]').forEach(videoEl => {
                    const originalUrl = videoEl.getAttribute('src');
                    if (originalUrl &&!originalUrl.includes('cdn2.thumbsnap.com')) {
                        const newUrl = originalUrl.replace('thumbsnap.com', 'cdn2.thumbsnap.com');
                        videoEl.setAttribute('src', newUrl);
                        console.log(`视频修复(跳转):已将 ${originalUrl} 替换为 ${newUrl}`);
                    }
                });
            } catch (e) {
                console.error('修复视频时出错。', e);
            }

            // --- 功能模块 6 (v2.5):自动展开内容 ---
            try {
                document.querySelectorAll('a').forEach(link => {
                    // 展开楼中楼评论
                    if (link.textContent.includes('還有') && link.textContent.includes('條點評')) {
                        link.click();
                        console.log(`自动展开点评:已点击 "${link.textContent}"`);
                    }
                    // 加载被隐藏的视频等内容
                    if (link.textContent.includes('以下內容被隱藏')) {
                        link.click();
                        console.log(`自动加载隐藏内容:已点击 "${link.textContent}"`);
                        // 点击后隐藏该链接和旁边的分隔符,美化页面
                        link.style.display = 'none';
                        if (link.previousSibling && link.previousSibling.nodeType === 3 && link.previousSibling.textContent.trim() === '|') {
                            link.previousSibling.textContent = '';
                        }
                    }
                });
            } catch (e) {
                console.error('自动展开内容时出错。', e);
            }

            console.log('--- 页面优化脚本执行完毕 ---');
        }

        // 首次加载页面时,运行一次所有优化
        runAllOptimizations();

        // --- 功能模块 7 (v2.6):Iframe 框架集成修复 ---
        if (window.self!== window.top) {
            console.log('Iframe环境检测:脚本正在iframe中运行。');
            window.addEventListener('message', (e) => {
                // 监听来自“链接预览”等脚本的消息,确保图片在iframe加载后能被修复
                if (e.data && e.data.from === '__link__prev_top') {
                    console.log('Iframe集成:接收到链接预览父窗口消息,重新运行图片修复。');
                    // 延迟执行以确保iframe内的DOM已经稳定
                    setTimeout(fixLazyImages, 100);
                }
            }, false);
        }

        // --- 功能模块 8 (v1.9):设置动态内容监视器,处理翻页 ---
        const postListContainer = document.querySelector('form[name="delatc"]');
        if (postListContainer) {
            const pageChangeObserver = new MutationObserver((mutationsList) => {
                for (const mutation of mutationsList) {
                    if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                        console.log('监视器:检测到页面内容更新(可能为翻页),将重新运行优化...');
                        runAllOptimizations();
                        return;
                    }
                }
            });

            pageChangeObserver.observe(postListContainer, { childList: true });
            console.log('翻页监视器已激活。');
        }


        // --- 第 2 层防御:具备弹性的 MutationObserver 后备(哨兵机制)---
        const targetNode = document.querySelector('div.tpc_content');

        if (!targetNode) {
            console.log('未找到目标内容区域 ".tpc_content"。第 2 层防御不会激活。');
            return;
        }

        let originalContent = targetNode.innerHTML;
        let isOriginalContentValid =!originalContent.includes('去广告插件屏蔽');

        const observerCallback = function(mutationsList, observer) {
            for (const mutation of mutationsList) {
                if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                    if (targetNode.textContent.includes('去广告插件屏蔽')) {
                        console.warn('检测到恶意 DOM 修改!第 2 层防御正在激活以恢复内容。');
                        observer.disconnect();
                        if (isOriginalContentValid) {
                            targetNode.innerHTML = originalContent;
                        } else {
                            targetNode.innerHTML = '内容已被反广告屏蔽脚本替换。无法恢复原始内容,但警告信息已移除。';
                        }
                        observer.observe(targetNode, { childList: true, subtree: true });
                        console.log('内容已恢复,第 2 层防御已重新激活。');
                        break;
                    }
                }
            }
            if (!targetNode.textContent.includes('去广告插件屏蔽')) {
                originalContent = targetNode.innerHTML;
                isOriginalContentValid = true;
            }
        };

        const observer = new MutationObserver(observerCallback);
        observer.observe(targetNode, { childList: true, subtree: true });
        console.log('第 2 层防御 (MutationObserver) 已激活并正在监视 ".tpc_content"。');
    });
})();