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

Trouble using mouseover on ad in Chrome extension

本文关键字:问题 遇到 悬停 扩展 Chrome 程序 鼠标      更新时间:2023-09-26

作为Chrome扩展程序的一部分,我正在尝试检测您将鼠标悬停在广告上的时间。 现在,我只在《纽约时报》的头版进行测试。 内容脚本的问题区域如下:

$(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 的技巧。 控制台.log显示它会挑选出包含广告的正确 iframe,但鼠标悬停事件不会触发。 实际上,它似乎不会运行任何超过querySelectorAll行的内容,因为没有出现进一步的console.logs。

如果我将鼠标悬停移到 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扩展程序进行了测试,并且工作正常,将鼠标悬停在广告上时,控制台会记录"广告",除了顶部的flash(我检查了一下,iframe有height:0