如何检测iframe内部的点击(跨域)?Aka防止点击欺诈

ola*_*laf 8 html javascript iframe jquery ads

我的广告系统提供商收到了有关点击欺诈的警告.没有进一步的信息,他们所推荐的是"为太快点击广告的用户隐藏广告".我写了一段JS脚本,点击时隐藏所有DIV广告N秒(使用cookie),但这个解决方案不起作用,因为"内部"内容(带广告)是由调用和呈现的JS脚本生成的来自外部服务器的内容(正如您对广告系统的期望).因此,当考虑到跨域安全性时,它有点像Catch 22.我如何检测DIV(本地定义)内部由外部JS和iframe呈现的内容中的单击?

例:

<div class="ad-class"> <!-- locally defined div -->
   <div id="my-id"> </div> <!-- identifies my ad in the provider's system -->
   <script>
      var foo = blah // declares the ad dimensions and stuff
      //  and renders the contextual ad in #my-id DIV
   </script>
</div>
Run Code Online (Sandbox Code Playgroud)

如果它都是本地的,解决方案将很容易,因为内部div将继承父类("ad-class").如果是跨域,则无效.有什么提示,老兄?

Lui*_*grs 9

您无法在跨域iframe中检测点击事件.

那么,你可能有一个不好的选择:

您可以做的最近事之一是检测焦点从窗口移动到iframe:

window.focus(); //force focus on the currenct window;
window.addEventListener('blur', function(e){
    if(document.activeElement == document.querySelector('iframe'))
    {
        alert('Focus Left Current Window and Moved to Iframe / Possible click!');
    }
});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/wk1yv6q3/

然而,它不可靠,松散焦点并不意味着点击,它可能是用户在网站上移动使用TAB.

另一个问题是,你只检测到第一次将焦点移动到iframe,你不知道用户在那里做了什么,他可以点击一百万次而你永远不会知道.


Ano*_*ird 7

Luizgrs激发了我这个解决方案:

var clickIframe = window.setInterval(checkFocus, 100);
var i = 0;

function checkFocus() {
  if(document.activeElement == document.getElementById("ifr")) {
  	console.log("clicked "+(i++));
  	window.focus();
   }
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<h2>Onclick event on iframe</h2>
<iframe src="https://www.brokenbrowser.com/" id="ifr"></iframe>
Run Code Online (Sandbox Code Playgroud)

该函数检测iframe是否具有焦点,如果是,则用户单击iframe.然后我们将焦点回馈给我们的主窗口,这样我们就可以找到用户是否点击了另一次.

这个技巧对我来说对于2步iframe点击顶升的POC非常有用.了解用户第一次在iframe上点击的时间允许我重新组织我的不同图层以保持幻觉完美.