Greasy Fork

Greasy Fork is available in English.

清理百度贴吧插入广告

清理掉贴吧列表和贴子中插入的广告

当前为 2019-09-01 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         清理百度贴吧插入广告
// @icon         http://www.gzqiyi.cn/fximg/delicious.gif
// @namespace    moekai.moe.cleartiebaad
// @version      1.1.7
// @description  清理掉贴吧列表和贴子中插入的广告
// @author       YIU
// @include      *://tieba.baidu.com/f?*
// @include      *://tieba.baidu.com/p/*
// @run-at       document-end
// @grant        unsafeWindow
// @compatible   chrome OK
// @compatible   firefox OK
// ==/UserScript==

var $ = unsafeWindow.$;

//======== 你可以修改这下面的开关数值 ========
//清理 1楼 下面的 相关推荐(0不清理 1清理)
var delrec = 1;

//如果发现清理不干净可以试着改大下面的数值
var csleep = 999;




//======== 函数区,不要修改 ========
//--- 添加贴子翻页监听
function AddPostFlipListener(){
	$('.l_pager a').each(function(){
		this.addEventListener('click',FlipEvent);
	});
}

//--- 添加列表翻页监听
function AddListFlipListener(re){
	if(!re)
	{
		$('.nav_list li').each(function(){
			if(this.attributes['data-tab-main'] || this.attributes['data-tab-good']){
				this.getElementsByTagName('a')[0].addEventListener('click',FlipEvent);
			}
		});
	}

	$('#frs_list_pager a').each(function(){
		this.addEventListener('click',FlipEvent);
	});
}

//--- 添加精品子分组监听
function AddGoodNavFlipListener(){
	$('.frs_good_nav_wrap a').each(function(){
		this.addEventListener('click',FlipEvent);
	});
}

//--- 添加回帖发送监听
function AddReplyFlipListener(){
	$('.poster_submit a').each(function(){
		this.addEventListener('click',FlipEvent);
	});
}

//--- 清理贴子AD
function DelTeibaPostAD(){
	$('.p_postlist').children('div').each(function(){
		var oa = this.querySelector('.d_author');
		if(!oa){
			this.parentNode.removeChild(this);
			return;
		}

		var ob = oa.querySelector('.p_author');
		if(!ob){
			this.parentNode.removeChild(this);
			return;
		}

		var oc = ob.querySelector('.d_name');
		if(!oc){
			this.parentNode.removeChild(this);
			return;
		}

		var odat = oc.getAttribute('data-field');
		if(!odat || !odat.replace(/\s/g,'')){
			this.parentNode.removeChild(this);
		}
	});

	//--- 关闭侧边悬浮AD
	$('.close_btn[ad-dom-img="true"]').click();

	//--- 清理边栏底部AD
	$('.media_ad').parent('div').each((i,o)=>{
		$(o).css({'display':'none'});
	});
}

//--- 清理列表AD
function DelTeibaListAD(){
	$('#thread_list').children('li').each(function(){
		if(!this.classList.contains('thread_top_list_folder') && !this.attributes['data-field'])
		{
			this.parentNode.removeChild(this);
		}
		else if(this.classList.contains('thread_top_list_folder'))
		{
			$(this).children('ul').children('li:not([data-field])').each(function(){
				this.parentNode.removeChild(this);
			});
		}
	});

	//--- 关闭侧边悬浮AD
	$('.close_btn[src*=adsense]').click();

	//--- 清理边栏底部AD
	$('div[id*="/ad"]').each((i,o)=>{
		$(o).css({'display':'none'});
	});
}

//--- 清理相关推荐
function DelRecommend(){
	if(delrec < 1) return;
	var oa = $('.thread_recommend');
	var ob = $('.thread_recommend_ps');
	if(oa.length >0){
		oa[0].parentNode.removeChild(oa[0]);
	}
	if(ob.length>0){
		ob[0].parentNode.removeChild(ob[0]);
	}
}


//======== 监听事件区,不要修改 =======
//--- 翻页监听事件
function FlipEvent(e)
{
	setTimeout(
		function(){

			var si = setInterval(okcheck, 50);
			var obj = e.target;
			var oldp = obj.parentNode;
			var limit = 0;

			function okcheck(){
				//- 对于列表
				// 导航和翻页
				if(oldp.attributes['data-tab-main'] && document.getElementById('pagelet_frs-list/pagelet/thread') && $('#frs_good_nav').length < 1 ||
				   $('#frs_good_nav').length > 0 ||
				   oldp.id == 'frs_list_pager' && !$('#frs_list_pager')[0].isEqualNode(oldp))
				{
					// 急性清理
					setTimeout(function(){
						AddListFlipListener(1);
						AddGoodNavFlipListener();
						DelTeibaListAD();
					},233);
					// 惰性清理
					setTimeout(function(){
						AddListFlipListener(1);
						AddGoodNavFlipListener();
						DelTeibaListAD();
					},csleep);
					clearInterval(si);

					//- 对于贴子
					// 翻页
				} else if(oldp.className.indexOf('l_pager') >= 0 && $('.loading-tip')[0].style.display == 'none' ){
					setTimeout(function(){
						AddPostFlipListener();
						DelTeibaPostAD();

					},csleep);
					clearInterval(si);
				}
				else
				{
					limit++;
					if( limit > 120 ) clearInterval(si);
				}
			}

		},50);
}


//======== 执行区,不要修改 =======
(function() {

	//急性
	setTimeout(function(){
		AddListFlipListener();
		AddPostFlipListener();
		AddReplyFlipListener();
		AddGoodNavFlipListener();
		DelTeibaListAD();
		DelTeibaPostAD();
		DelRecommend();
	},1234);

	//惰性
	window.onload = function(){
		AddListFlipListener();
		AddPostFlipListener();
		AddReplyFlipListener();
		AddGoodNavFlipListener();
		DelTeibaListAD();
		DelTeibaPostAD();
		DelRecommend();
	};

})();