Greasy Fork

Greasy Fork is available in English.

学术网页屏蔽广告(百度翻译)

一款

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

// ==UserScript==
// @name         学术网页屏蔽广告(百度翻译)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  一款
// @author       哔哩哔哩:凇月落
// @match        https://fanyi.baidu.com/*
// @match        https://dict.cnki.net/*
// @grant        GM_addStyle
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 广告选择器配置(集中管理,便于维护)
    const AD_SELECTORS = {
        // 主广告容器
        MAIN_AD: '.KxVKmLZM',
        // 广告元素
        AD_ELEMENTS: [
            '.URCZyDIb',
            '._U9afNhR',
          //  '.R8Aay5vw',
            '.fU_fsLfm',
            '.YImUGZJo'
        ],
        // 动态加载的广告元素
        DYNAMIC_ADS: ['.LsBEmsAO']
    };

    // 广告移除功能
    function removeAds() {
        // 移除主广告容器
        const mainAd = document.querySelector(AD_SELECTORS.MAIN_AD);
        if (mainAd) {
            mainAd.style.display = 'none';
            console.log('主广告已隐藏');
        }

        // 批量移除静态广告元素
        AD_SELECTORS.AD_ELEMENTS.forEach(selector => {
            document.querySelectorAll(selector).forEach(element => {
                element.remove();
                console.log(`已移除广告元素: ${selector}`);
            });
        });

        // 批量移除动态广告元素
        AD_SELECTORS.DYNAMIC_ADS.forEach(selector => {
            document.querySelectorAll(selector).forEach(element => {
                element.remove();
                console.log(`已移除动态广告: ${selector}`);
            });
        });
    }

    // 初始执行
    removeAds();

    // 创建优化的观察者
    const observer = new MutationObserver(mutations => {
        let foundNewAd = false;

        // 检查是否有新节点添加
        for (const mutation of mutations) {
            if (mutation.addedNodes.length > 0) {
                foundNewAd = true;
                break;
            }
        }

        // 发现新内容时重新清理广告
        if (foundNewAd) {
            setTimeout(removeAds, 10); // 延迟确保DOM完全加载
        }
    });

    // 开始观察文档变化
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 添加CSS增强隐藏(双重保障)
    const style = document.createElement('style');
    style.innerHTML = `
        /* 主广告容器 */
        ${AD_SELECTORS.MAIN_AD} {
            display: none !important;
            height: 0 !important;
            visibility: hidden !important;
        }

        /* 其他广告元素 */
        ${[...AD_SELECTORS.AD_ELEMENTS, ...AD_SELECTORS.DYNAMIC_ADS].join(',')} {
            display: none !important;
            opacity: 0 !important;
            pointer-events: none !important;
        }
    `;
    document.head.appendChild(style);

    // 页面完全加载后再次检查
    window.addEventListener('load', () => {
        setTimeout(removeAds, 500);
    });

    // 控制台输出初始化信息
    console.log('百度翻译广告屏蔽脚本已激活。当前配置:');
    console.table(AD_SELECTORS);


//********************************************************************************************************
    //知网翻译
    //********************************************************************************
   GM_addStyle(`
  a[href="https://huazhi.cnki.net/aidoc/"] {
    display: none !important;
  }
`);
})();