How to detect a click inside of an iframe (cross-domain)? Aka prevent click fraud

后端 未结 6 1776
萌比男神i
萌比男神i 2020-12-03 18:29

I got a warning by my ad system provider about click fraud. No further info, all they are recommending is \"hide the ads for users who click on ads too quickly\'\". I wrote

6条回答
  •  无人及你
    2020-12-03 19:19

    Luizgrs inspired me this solution :

    var clickIframe = window.setInterval(checkFocus, 100);
    var i = 0;
    
    function checkFocus() {
      if(document.activeElement == document.getElementById("ifr")) {
      	console.log("clicked "+(i++));
      	window.focus();
       }
    }
    
    

    Onclick event on iframe

    The function detect if the iframe has the focus, if yes, the user clicked into the iframe. We then give back the focus to our main windows, which allow us to find if the user click another time.

    This trick has been usefull to me for a POC on a 2 step iframe click-jacking. Getting to know when the user clicked for the first time on the iframe allowed me to reorganize my different layers to keep the illusion perfect.

提交回复
热议问题