在 Chrome 扩展中的广告上使用鼠标悬停时遇到问题

Trouble using mouseover on ad in Chrome extension

作为 Chrome 扩展程序的一部分,我正在尝试检测您何时将鼠标悬停在广告上。现在,我只在 NYT 的首页上进行测试。内容脚本问题区域如下:

$(document).ready(function (){
    setTimeout(function() {
        console.log("starting...");
        console.log(document.querySelectorAll("iframe"));
        var frames = $("iframe").contents().find(".ad-frame.frame-for-homepage");
        console.log(frames);

        frames.on("mouseover", function(event){
            console.log("on ad");
        });
    }, 10000);


});

setTimeout 函数是专门用于在页面上获取正确的 iframe 的 hack。 console.log 表明它选择了包含广告的正确 iframe,但鼠标悬停事件没有触发。它实际上似乎不会 运行 任何超过 querySelectorAll 行的东西,因为没有进一步的 console.log 出现。

如果我将鼠标悬停在 setTimeout 函数之外,它会在鼠标悬停在页面上的任何元素上时触发。

我在这方面遇到了一些困难,因此非常感谢您的帮助。谢谢

选择器不正确,您可以简化代码使其工作,因为广告 iframe 具有 类 .ad-frame.frame-for-homepage。这很好用:

$(document).ready(function (){

    setTimeout(function() {

        console.log("starting...");
        console.log(document.querySelectorAll("iframe"));
        var frames = $("iframe.ad-frame.frame-for-homepage");
        console.log(frames);

        frames.on("mouseover", function(event){
            console.log("on ad");
        });

    }, 10000);

});

编辑:将其作为 Chrome 扩展进行测试并且工作正常,将鼠标悬停在广告上时控制台会记录 "on ad",除了顶部的 Flash 广告(我检查过并且 iframe 有 height:0)