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

Optimized URL matching to run on all pages. Perfectly handles pagination and removes all ads including the footer ad. The ultimate all-in-one solution.

// ==UserScript==
// @name         T66Y Anti-Ad-Block Defeater (v2.1 Ultimate Pro+)
// @name:zh-CN   草榴社区反广告屏蔽破解脚本 (v2.1 全站匹配版)
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  Optimized URL matching to run on all pages. Perfectly handles pagination and removes all ads including the footer ad. The ultimate all-in-one solution.
// @description:zh-CN [v2.1] 优化:采用全站匹配规则,确保脚本在所有页面激活。包含精准移除页脚广告、高级广告屏蔽、按字数过滤回复、链接修复、图片修复、反广告屏蔽功能。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', () => {

        function runAllOptimizations() {
            console.log('--- 开始执行页面优化脚本 (v2.1) ---');
            // --- 功能模块 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):修复图片懒加载 ---
            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);
            }
            console.log('--- 页面优化脚本执行完毕 ---');
        }

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

        // --- 功能模块 5 (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"。');
    });
})();