// ==UserScript== // @name TypeScript中文网广告隐藏 // @namespace // @include *://ts.nodejs.cn // @include *://ts.nodejs.cn/* // @version 0.1.1 // @description 隐藏TypeScript中文网广告,包括普通广告及谷歌广告 // @author ymzhao // @namespace // @license MIT // @grant GM_addStyle // @downloadURL https://update.greasyfork.icu/scripts/536028/TypeScript%E4%B8%AD%E6%96%87%E7%BD%91%E5%B9%BF%E5%91%8A%E9%9A%90%E8%97%8F.user.js // @updateURL https://update.greasyfork.icu/scripts/536028/TypeScript%E4%B8%AD%E6%96%87%E7%BD%91%E5%B9%BF%E5%91%8A%E9%9A%90%E8%97%8F.meta.js // ==/UserScript== GM_addStyle(` #bottomad, /* 底部广告 */ .adsbygoogle /* 全屏谷歌广告 */ { display: none !important; } `); // 需要手动清除的广告选择器列表(css优先级不够) const adSelectors = ['.adsbygoogle']; /** * 清理广告函数 * 遍历所有广告选择器,找到匹配的元素并隐藏它们 */ function cleanAds() { adSelectors.forEach(selector => { document.querySelectorAll(selector).forEach(ad => { ad.style.setProperty('display', 'none', 'important'); }); }); } /** * 初始化MutationObserver来监听DOM变化 */ function watchForNewAds() { // 创建一个观察器实例并传入回调函数 const observer = new MutationObserver((mutations) => { // 检查是否有节点被添加 const hasAddedNodes = mutations.some(mutation => mutation.addedNodes && mutation.addedNodes.length > 0 ); if (hasAddedNodes) cleanAds(); }); // 配置观察选项 const config = { childList: true, // 观察子节点的添加或删除 subtree: true // 观察所有后代节点 }; // 开始观察document.body observer.observe(document.body, config); } // 页面加载完成后执行 document.addEventListener('DOMContentLoaded', () => { cleanAds(); watchForNewAds(); }); // 确保在DOM完全加载前插入的内容也能被处理 if (document.readyState === 'complete' || document.readyState === 'interactive') { cleanAds(); watchForNewAds(); }