我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
当前回答
我知道这个问题已经有答案了,但我看了一下建议的示例站点,我看到他们是这样做的:
<script type="text/javascript">
if(document.getElementsByTagName("iframe").item(0) == null) {
document.write("<div style="width: 160px; height: 600px; padding-top: 280px; margin-left: 5px; border: 1px solid #666666; color: #FFF; background-color: #666; text-align:center; font-family: Maven Pro, century gothic, arial, helvetica, sans-serif; padding-left: 5px; padding-right: 5px; border-radius: 7px; font-size: 18px;">Advertising seems to be blocked by your browser.<br><br><span style="font-size: 12px;">Please notice that advertising helps us to host the project.<br><br>If you find these ads intrusive or inappropriate, please contact me.</span><br><img src="http://www.playonlinux.com/images/abp.png" alt="Adblock Plus"></div>");
};
</script>
其他回答
这对我来说很管用:
function isAdBlocked() {
return (typeof(window.google_jobrunner) === "undefined") ? true : false;
}
$(document).ready(function(){
if(isAdBlocked()) {
alert('Y U NO LIKE ADS?');
}
});
我有点晚了,这里有一个最简单的解决方案,我知道的老AdSense代码与jQuery:
$ads = $("ins");
if ($ads.length == 0) {
// Replacement code about you needing ad income
}
在纯JavaScript中:
$ads = document.getElementsByTagName("ins");
if ($ads.length == 0) {
// Replacement code about you needing ad income
}
对于$ads,您可以使用任何与正在生成的广告一致的选择器。例如,对于新的AdSense代码,您可以使用$("iframe#google_ads_frame1")。
在React.js中,你可以做以下事情:
class AdblockDetect extends Component {
constructor (props) {
super(props)
this.state = {
usingAdblock: false
}
}
componentDidMount() {
this.setState({ usingAdblock: this.fakeAdBanner.offsetHeight === 0 });
}
render() {
if (this.state.usingAdblock === true) {
return this.props.children;
}
return (
<div
ref={r => (this.fakeAdBanner = r)}
style={{ height: '1px', width: '1px', visiblity: 'none', pointerEvents: 'none' }}
className="adBanner"
/>
);
}
}
class App extends Component {
render() {
return (
<div className="App">
<AdblockDetect>You are using adblock</AdblockDetect>
</div>
);
}
}
来源:https://stackoverflow.com/a/55635499/5539715
这为我工作,文件必须准备好之前,你寻找广告块 希望这能帮助到一些人!
jQuery(document).ready(function() {
var adBlockEnabled = false;
var testAd = document.createElement('div');
testAd.innerHTML = ' ';
testAd.className = 'adsbox';
document.body.appendChild(testAd);
window.setTimeout(function() {
if (testAd.offsetHeight === 0) {
adBlockEnabled = true;
}
testAd.remove();
console.log('AdBlock Enabled? ', adBlockEnabled)
}, 100);
});
如果你正在使用react和钩子:
import React, { useState, useEffect } from 'react'
const AdblockDetect = () => {
const [usingAdblock, setUsingAdblock] = useState(false)
let fakeAdBanner
useEffect(() => {
if (fakeAdBanner) {
setUsingAdblock(fakeAdBanner.offsetHeight === 0)
}
})
if (usingAdblock === true) {
return null
}
return (
<div>
<div
ref={r => (fakeAdBanner = r)}
style={{ height: '1px', width: '1px', visibility: 'hidden', pointerEvents: 'none' }}
className="adBanner"
/>
Adblock!
</div>
)
}
export default AdblockDetect