adblock拦截iframe
#*(src*=http://cpro.baidu.com/)
来源: http://thepcspy.com/read/how_to_block_adblock
Blocking Adblock
We start off with our advert code. Most designers nest their code in a positioning element like a <div>. Have a butchers at the following code to see what I’m on about:
<div class="myTestAd" style=" text-align:center;margin:10px">
<!-- advert code goes here -->
</div>
In place of the comment there is advert code. This is usually a JavaScript link off to the advertising agency’s website. In the case of Google AdSense, it’s a link off to http://pagead2.googlesyndication.com/pagead/show_ads.js. This script generates the HTML to be displayed on the page inside an iframe. In layman’s‘ terms, an iframe is effectively a web page nested inside a page. This allows Google, in this case, to display their advert to the end user.
So what ends up being rendered is an iframe inside our div. If Adblock is introduced into the equation, the following rule nukes the advert: #*(src*=http://pagead2.)
This is a CSS-selector-based rule that hides any output whose source points to any address beginning with http://pagead2. so the iframe whose source URL does begin with that, is hidden. Not blocked. The JavaScript code still runs but the output is hidden.
Therefore using another chunk of JavaScript at page-level, we can quickly see if the advert has been blocked with this rule. How? We just query the height of the output iframe. If it equals zero, it has been mushed otherwise everything is fine. To do this check we need to call upon the mystic powers of the Document-Object Model but because I’m lazy like that, I’m using the very excellent jQuery library to do all the leg-work. Here is all the JavaScript we need to detect and alert the user they’re blocking adverts:
function TestPage() {
if ($('.myTestAd').height() == 0)
alert("You are blocking my beautiful adverts, you swine!");
}
$(TestPage);
I meant it. A chimp could come up with that. The Window.onDomReady part is a MooTools function that only executes the code when the page is ready to be tested. That is the only nearly-complex thing about it.
Check out the test page
This version just pops up a message box, calls the user a swine and leaves them be but if people wanted to get really obnoxious about their adverts, they could redirect them, blocking them outright.
<div class="myTestAd" style=" text-align:center;margin:10px">
<!-- advert code goes here -->
</div>
function TestPage() {
if ($('.myTestAd').height() == 0)
alert("You are blocking my beautiful adverts, you swine!");
}
$(TestPage);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架