烧饼论坛 LZ.SB
登录

V2EX 脚本

689
  • COM
    UID 789Lv.6 经常露面博主evan.xin

    广告屏蔽脚本,哈哈!

    image.webp

    // ==UserScript==
    // @name         V2EX 广告清理
    // @name:zh-CN   V2EX 广告清理
    // @namespace    https://v2ex.com/
    // @version      1.0.0
    // @description  屏蔽 V2EX 页面中的 PRO 推广位(Promoted by 广告盒)、万维广告(wwads)与 Google AdSense 广告,并清理广告移除后残留的间距
    // @author       COM
    // @match        *://v2ex.com/*
    // @match        *://*.v2ex.com/*
    // @run-at       document-start
    // @grant        none
    // ==/UserScript==
    
    (function () {
        'use strict';
    
        var ADS_CSS = [
            /* ---- 1. V2EX 官方 PRO 推广位(侧边栏 / 列表里 "Promoted by xxx" 的广告盒) ---- */
            '#pro-campaign-container,',
            '[class^="pro-unit"],',
            '[class*=" pro-unit"],',
    
            /* ---- 2. 万维广告 wwads(列表中带 "广告" 角标、"Shop Now" 按钮的横幅) ---- */
            '.wwads-cn,',
            '[class^="wwads"],',
            '[class*=" wwads"],',
    
            /* ---- 3. Google AdSense 及常见第三方广告 iframe ---- */
            'ins.adsbygoogle,',
            '.adsbygoogle,',
            '.google-auto-placed,',
            '[id^="google_ads"],',
            '[id^="aswift"],',
            'iframe[id^="aswift"],',
            'iframe[src*="googlesyndication"],',
            'iframe[src*="doubleclick"] {',
    
            '    display: none !important;',
            '    visibility: hidden !important;',
            '    height: 0 !important;',
            '    min-height: 0 !important;',
            '    max-height: 0 !important;',
            '    width: 0 !important;',
            '    min-width: 0 !important;',
            '    margin: 0 !important;',
            '    padding: 0 !important;',
            '    border: 0 !important;',
            '    overflow: hidden !important;',
            '    pointer-events: none !important;',
            '}',
    
            /* ---- 4. 广告盒隐藏后,顺带吃掉它上方多余的 20px 间距(旧浏览器不支持 :has 会自动忽略) ---- */
            'div.sep20:has(+ #pro-campaign-container),',
            'div.sep20:has(+ div > .wwads-cn) {',
            '    display: none !important;',
            '}'
        ].join('\n');
    
        function injectCSS() {
            var root = document.head || document.documentElement;
            if (!root) return;
            var style = document.createElement('style');
            style.type = 'text/css';
            style.textContent = ADS_CSS;
            root.appendChild(style);
        }
    
        /* 广告容器被隐藏后,若 wwads 的外层只剩一个空壳,一并收掉,避免残留视觉缝隙 */
        function cleanupEmptyShell() {
            var ads = document.querySelectorAll('.wwads-cn');
            for (var i = 0; i < ads.length; i++) {
                var box = ads[i].parentElement;
                if (box && box.tagName === 'DIV' && !box.id && box.children.length === 1) {
                    box.style.display = 'none';
                }
            }
        }
    
        /* document-start 阶段就注入,早于广告脚本渲染,避免闪烁 */
        injectCSS();
    
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', function () {
                injectCSS();
                cleanupEmptyShell();
            }, { once: true });
        } else {
            cleanupEmptyShell();
        }
    })();
    
    
  • 收藏支持反对打赏

9 条回复

  • mubdao
    UID 1206Lv.10 崭露头角
    亮了 1#1

    没账号

  • 顺丰
    UID 7422Lv.4 渐入佳境
    亮了 1#2

    没账号 + 1

  • 大便
    UID 7070Lv.6 经常露面
    #3

    没账号

  • COM
    UID 789Lv.6 经常露面楼主博主evan.xin
    #4

  • Xshell
    UID 2323Lv.9 社区常客
    #5

    没账号

  • BSD
    UID 755Lv.5 小有收获
    #6

    没账号

  • 妈妈
    UID 2303Lv.5 小有收获
    #7

    奇怪 为什么我进去没开任何去广告的插件 看不到一个广告

  • 触手怪
    UID 7049Lv.7 熟悉面孔
    #8

    没账号 太贵

  • shashaoa24
    UID 5871Lv.3 初识社区
    #9

    佬来个邀请码 我去帮你试试

发表回复