本篇内容主要讲解“Druid去广告的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Druid去广告的方法”吧!
我们先在页面定位广告产生的逻辑,
线索一、由于广告不是页面加载时出现的,所以肯定有个异步的方法来加载;
线索二、因为每个页面都会调用,因此一般封装在某个js中。
在Druid主页面,按F12,可以看到页面引用了如下js文件
<script src="js/lang.js" type="text/javascript" charset="utf8"></script>
<script src="js/common.js" type="text/javascript" charset="utf8"></script>
经逐个排查,发现广告代码存在与common.js中
第30行
buildFooter : function() {
var html ='<footer class="footer">'+
' <div class="container">'+
'<a href="https://render.alipay.com/p/s/taobaonpm_click/druid_banner_click" target="new"><img src="https://render.alipay.com/p/s/taobaonpm_click/druid_banner"></a><br/>' +
' powered by <a href="https://github.com/alibaba/" target="_blank">AlibabaTech</a> & <a href="http://www.sandzhang.com/" target="_blank">sandzhang</a> & <a href="http://melin.iteye.com/" target="_blank">melin</a> & <a href="https://github.com/shrekwang" target="_blank">shrek.wang</a>'+
' </div>'+
' </footer>';
$(document.body).append(html);
}
目前在不能更改源代码的前提下,我们可以通过Filter过滤器来修改 common.js 的返回内容
在Filter中判断当前路径是否以 /druid/js/common.js
结尾;
获取当前文件路径应返回的内容;
删除广告相关代码,并构造新内容;
将新内容,返回输出至前台;
if(requestUrl.contains("/druid/js/common.js")){
// 过滤 /druid/js/common.js 路径,将广告代码替换掉
chain.doFilter(request, response);
response.resetBuffer();
// 获取common文件内容
String text = Utils.readFromResource("support/http/resources/js/common.js");
// 正则表达式删除 <footer class="footer"> 与 </footer> 之间的内容,包括footer本身
text = text.replaceAll("<footer class=\"footer\">[^%]*</footer>", "");
// 将新内容返回至前台页面
response.getWriter().write(text);
}
重新运行代码后,访问 druid/js/common.js
,会发现广告代码没了
buildFooter : function() {
var html ='';
$(document.body).append(html);
},
此时访问 Druid相关页面,发现没有广告了,大功告成!
到此,相信大家对“Druid去广告的方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/gmarshal/blog/3074094