移除框架广告 Remove Ads In iframes

Block ads in websites. The script can only block the ads wraped with iframe.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name             移除框架广告 Remove Ads In iframes
// @namespace        https://ez118.github.io/
// @version          1.7
// @icon             https://adblockplus.org/favicon.ico
// @description      Block ads in websites. The script can only block the ads wraped with iframe.
// @author           ZZY_WISU
// @match            *://*/*
// @license          GPLv3
// @run-at document-end
// ==/UserScript==

/*If this can't block your ads, add the url to block it!*/
var AdsUrlList = ["kunpeng-sc.csdnimg.cn", "googleads.g.doubleclick.net", "pos.baidu.com", "vt.ipinyou.com", "www.2345.com", "show-3.mediav.com",
                 "acdn.adnxs.com", "googleads.g.doubleclick.net", "gum.criteo.com", "ads.yieldmo.com", "ads.pubmatic.com", "bh.contextweb.com",
                 "contextual.media.net", "prebid.a-mo.net", "safeframe.googlesyndication.com", "tpc.googlesyndication.com", "s0.2mdn.net",
                 "c.aaxads.com"];
/*目前可拦截这些广告商的广告: CSDN, GOOGLE, BAIDU等 */

/* function BlockIt() {
    Array.prototype.forEach.call(document.querySelectorAll('iframe'), function(iframe) {
        for(let i = 0; i < AdsUrlList.length; i ++){
            if(iframe.src.includes(AdsUrlList[i])){
                try { iframe.remove(); }
                catch(e) { iframe.parentElement.removeChild(iframe); }
                break;
            }
        }
    });
} */

function BlockIt() {
    // 将广告 URL 列表合并为正则表达式
    const adsUrlPattern = new RegExp(AdsUrlList.map(url => url.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|'), 'i');

    // 遍历所有 iframe 元素
    document.querySelectorAll('iframe').forEach(function(iframe) {
        const src = iframe.src; // 缓存 src 属性
        if (adsUrlPattern.test(src)) { // 使用正则表达式检查 src
            try {
                iframe.remove(); // 尝试移除 iframe
            } catch (e) {
                iframe.parentElement.removeChild(iframe); // 兼容处理
            }
        }
    });
}


(function() {
    'use strict';

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

    BlockIt();

    'use strict';
    setTimeout(BlockIt, 800);
    setInterval(BlockIt, 3000);
})();