To protect the ads, think about creating a protective layer, that is, a transparent div after there are two or three clicks by the same user and, thus, avoid more clicks by the same visitor.
$(document).ready(function() {
$(".ads iframe").load(function() {
$(".layer-protect").hide();
});
});
.ads {
position: relative;
}
.layer-protect {
position: absolute;
}
iframe {
position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ads">
<iframe src="https://es.stackoverflow.com/"></iframe>
</div>
<div class="layer-protect">
<p>Hi! Testing...</p>
</div>
But of course this was not enough, so apart from creating a layer I would have to delete all the links that the iframe contains
let i = 0;
$("iframe *").each(function() {
this.pos = i;
$(this).on("click", (e) => {
if (localStorage.getItem("link"+i) == null) {
localStorage.setItem("link"+i, 1);
} else {
let clicks = localStorage.getItem("link"+i);
if (clicks >= 3) {
if (typeof e.target.getAttribute("href") == "string" && e.target.getAttribute("href").toLowerCase().search(location.hostname.toString().toLowerCase()) == -1) {
e.preventDefault();
}
} else {
localStorage.removeItem("link"+i);
clicks++;
localStorage.setItem("link"+i, clicks);
}
}
});
i++;
});
How can I set a validity time to the data stored in localStorage without the user being online or not?
My tests:
I have created a page with the following links: iframe.html
<div>
<a href="https://example.com" target="_black">1</a>
<a href="https://example.com" target="_black">2</a>
<a href="https://example.com" target="_black">3</a>
<a href="https://example.com" target="_black">4</a>
<a href="https://example.com" target="_black">5</a>
<a href="https://example.com" target="_black">6</a>
<a href="https://example.com" target="_black">7</a>
<a href="https://example.com" target="_black">8</a>
</div>
Then this page I have loaded in an iframe
<iframe src="http://example.com/iframe.html"></iframe>
As a result it doesn't work, I can give all clicks on the links without any restriction
Note: It is difficult to determine the content of an iframe since ad providers such as google adsense do not define an exact content.
If in most cases the content of the iframe cannot be eliminated, then it will be possible to count the clicks that the iframe receives, and then create a div, a protective layer that covers the entire iframe and, thus avoid more than two or three clicks, according to the rule that I determine.