移除框架广告 Remove Ads In iframes

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();