AdBlock 到 ReactJs
AdBlock into ReactJs
我一直在努力寻找解决方案,直到现在都没有任何进展。我试图了解用户是否有 AdBlocker,如果有,我想显示一条消息,建议他将其关闭。但是,到现在都没有成功。
我将我的组件导入到我的主容器中,如:
<DetectAdBlock pathname={window.location.pathname} />
然后这是我的 adblocker.js
import React from 'react';
import PropTypes from 'prop-types'
class DetectAdBlock extends React.Component {
static propTypes = {
pathname: PropTypes.string.isRequired
};
constructor(props) {
super(props);
this.state = {
adBlockDetected: false
}
this.detectAdBlocker = this.detectAdBlocker.bind(this);
}
componentDidMount() {
this.detectAdBlocker();
}
componentWillUpdate(nextProps, nextState) {
if (this.props.pathname !== nextProps.pathname) {
this.detectAdBlocker();
}
}
detectAdBlocker() {
const head = document.getElementsByTagName('head')[0];
const noAdBlockDetected = () => {
this.setState({
adBlockDetected: false
});
}
const adBlockDetected = () => {
this.setState({
adBlockDetected: true
});
}
// clean up stale bait
const oldScript =
document.getElementById('adblock-detection');
if (oldScript) {
head.removeChild(oldScript);
}
// we will dynamically generate some 'bait'.
const script = document.createElement('script');
script.id = 'adblock-detection';
script.type = 'text/javascript';
script.src = '/ads.js';
script.onload = noAdBlockDetected;
script.onerror = adBlockDetected;
head.appendChild(script);
}
noticeContentJSX() {
return (
<div id="adblock-notice">
<div className="message">
<h3>Hey, you!</h3>
<p>Your adblocker is on again.</p>
<button
onClick={this.detectAdBlocker}
>
Check for Adblocker again
</button>
</div>
</div>
);
}
render() {
return (
<div id="adblock-wrapper">
{ this.state.adBlockDetected
? this.noticeContentJSX()
: null
}
</div>
)
}
}
// DetectAdBlock.propTypes = {
// pathname: PropTypes.string.isRequired
// };
DetectAdBlock.defaultProps = {
pathname: ''
}
export default DetectAdBlock;
问题是我启用了 AdBlock 后没有任何显示。
我建议你使用 npm 包 react-ad-block-detect
:
安装包:
npm i react-ad-block-detect
然后试试这个:
import React, { Component } from 'react';
import AdBlockDetect from 'react-ad-block-detect';
class MyComponent extends Component {
render() {
return (
<AdBlockDetect>
<p>Show this if an ad blocker has been enabled.</p>
</AdBlockDetect>
);
}
}
我想应该比这更容易。我无法实际测试这个,因为我正在关闭 adblock,但像这样的东西应该可以工作:
class AdblockDetect extends Component {
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>
);
}
}
使用 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
我一直在努力寻找解决方案,直到现在都没有任何进展。我试图了解用户是否有 AdBlocker,如果有,我想显示一条消息,建议他将其关闭。但是,到现在都没有成功。
我将我的组件导入到我的主容器中,如:
<DetectAdBlock pathname={window.location.pathname} />
然后这是我的 adblocker.js
import React from 'react';
import PropTypes from 'prop-types'
class DetectAdBlock extends React.Component {
static propTypes = {
pathname: PropTypes.string.isRequired
};
constructor(props) {
super(props);
this.state = {
adBlockDetected: false
}
this.detectAdBlocker = this.detectAdBlocker.bind(this);
}
componentDidMount() {
this.detectAdBlocker();
}
componentWillUpdate(nextProps, nextState) {
if (this.props.pathname !== nextProps.pathname) {
this.detectAdBlocker();
}
}
detectAdBlocker() {
const head = document.getElementsByTagName('head')[0];
const noAdBlockDetected = () => {
this.setState({
adBlockDetected: false
});
}
const adBlockDetected = () => {
this.setState({
adBlockDetected: true
});
}
// clean up stale bait
const oldScript =
document.getElementById('adblock-detection');
if (oldScript) {
head.removeChild(oldScript);
}
// we will dynamically generate some 'bait'.
const script = document.createElement('script');
script.id = 'adblock-detection';
script.type = 'text/javascript';
script.src = '/ads.js';
script.onload = noAdBlockDetected;
script.onerror = adBlockDetected;
head.appendChild(script);
}
noticeContentJSX() {
return (
<div id="adblock-notice">
<div className="message">
<h3>Hey, you!</h3>
<p>Your adblocker is on again.</p>
<button
onClick={this.detectAdBlocker}
>
Check for Adblocker again
</button>
</div>
</div>
);
}
render() {
return (
<div id="adblock-wrapper">
{ this.state.adBlockDetected
? this.noticeContentJSX()
: null
}
</div>
)
}
}
// DetectAdBlock.propTypes = {
// pathname: PropTypes.string.isRequired
// };
DetectAdBlock.defaultProps = {
pathname: ''
}
export default DetectAdBlock;
问题是我启用了 AdBlock 后没有任何显示。
我建议你使用 npm 包 react-ad-block-detect
:
安装包:
npm i react-ad-block-detect
然后试试这个:
import React, { Component } from 'react';
import AdBlockDetect from 'react-ad-block-detect';
class MyComponent extends Component {
render() {
return (
<AdBlockDetect>
<p>Show this if an ad blocker has been enabled.</p>
</AdBlockDetect>
);
}
}
我想应该比这更容易。我无法实际测试这个,因为我正在关闭 adblock,但像这样的东西应该可以工作:
class AdblockDetect extends Component {
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>
);
}
}
使用 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