From 8cabf1b74c46d925b9b79b13372589512fb03041 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 15 Jan 2019 17:53:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'baidu/clearAd.user.js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baidu/clearAd.user.js | 127 ++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 55 deletions(-) diff --git a/baidu/clearAd.user.js b/baidu/clearAd.user.js index aa701ae..38dff54 100644 --- a/baidu/clearAd.user.js +++ b/baidu/clearAd.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name 百度广告去除 // @namespace http://ide.yumc.pw/ -// @version 0.0.7 +// @version 0.0.8 // @description 去除百度上下推广广告 // @author MiaoWoo // @include http*://*.baidu.* @@ -10,71 +10,88 @@ // ==/UserScript== (function() { - 'use strict'; - var selectors = ` + 'use strict'; + var selectors = ` #content_left [style*="display:block !important;visibility:visible !important"] [tpl="right_toplist"] [class="c-gray c-feedback"] -[data-click*="vLevel"] `; - selectors = selectors.trim().split('\n'); - var count = 0; - function showCount(cn){ - if(cn>0){ - count += cn; - var content_right = document.querySelector('#content_right'); - if(content_right){ - content_right.style.position = 'relative'; - var style = ` + selectors = selectors.trim().split('\n'); + var count = 0; + + function clearDom(dom){ + if(dom.parentNode){ + dom.parentNode.removeChild(dom); + } + } + + function showCount(cn){ + if(cn>0){ + count += cn; + var content_right = document.querySelector('#content_right'); + if(content_right){ + content_right.style.position = 'relative'; + var style = ` line-height: 42px; text-align: center; background: #0c0; color: #fff; margin-bottom:30px; `; - var msgCount = document.querySelector('#msg-remove'); - if(!msgCount){ - content_right.insertAdjacentHTML('afterbegin','
'); - msgCount = document.querySelector('#msg-remove'); + var msgCount = document.querySelector('#msg-remove'); + if(!msgCount){ + content_right.insertAdjacentHTML('afterbegin','
'); + msgCount = document.querySelector('#msg-remove'); + } + msgCount.innerHTML = `已过滤 ${count}条垃圾广告 本次过滤:${cn}条`; + } } - msgCount.innerHTML = '已过滤:'+count+'条垃圾广告,本次过滤:'+cn+'条'; - } } - } - function clear() { - var tempCount = 0 - selectors.forEach(function(selector) { - var doms = document.querySelectorAll(selector); - if(doms.length){ - doms = Array.from(doms); - tempCount += doms.length; - doms.forEach(function(dom) { - dom.parentNode.removeChild(dom); + + function clearResult(){ + var count = 0; + selectors.forEach(function(selector) { + var doms = document.querySelectorAll(selector); + if(doms.length){ + doms = Array.from(doms); + count += doms.length; + doms.forEach(dom => clearDom(dom)); + } }); - } - }); - var adKeyword = ['广告', '评价'] - var classKeyWord = ['.m', 'a span'] - classKeyWord.forEach(function (c) { - var result = $(c) - result.each(function (index,element) { - if(adKeyword.indexOf($(element).text())){ - $(element).parent().parent().remove(); - tempCount++ - } - }); - }) - $('.ad-block').each(function (index,element) { - $(element).parent().parent().parent().remove(); - tempCount++ - }) - $('div a').each(function (index, element) { - if($(element).text()=="广告"){ - $(element).parent().parent().parent().parent().parent().parent().parent().remove() - } - tempCount++ - }) - showCount(tempCount); - } - setInterval(clear, 500); + return count; + } + + function clearByTag(){ + var count = 0; + var adKeyword = ['广告', '评价'] + var classKeyWord = ['m', 'a span'] + classKeyWord.forEach(function (c) { + Array.from(document.getElementsByClassName(c)).forEach(function (element) { + if (adKeyword.indexOf(element.innerText) > -1){ + clearDom(element.parentNode.parentNode) + count++ + } + }); + }) + return count; + } + + function clearByClass(){ + var count = 0; + Array.from(document.getElementsByClassName('ad-block')).forEach(function (element) { + clearDom(element.parentNode.parentNode.parentNode) + count++ + }) + return count; + } + + function clear() { + var tempCount = 0 + tempCount += clearResult(); + tempCount += clearByTag(); + tempCount += clearByClass(); + showCount(tempCount); + } + //setTimeout(function(){clear();}, 500); + setInterval(function(){clear();}, 500); })(); \ No newline at end of file