确保内容脚本可以看到"click"事件 [英] Ensure `click` event is seen by the content script

查看:66
本文介绍了确保内容脚本可以看到"click"事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Chrome扩展程序,该扩展程序可以侦听和捕获用户做出的点击事件.这就是我捕获事件的方式

I'm writing an extension for Chrome that listen and capture the click events made by the user. This is the way I'm capturing the event

document.addEventListener('click', async function (e) {

});

在许多情况下它都很好用,但是在其他情况下,单击事件从未触发,而是有一个或多个焦点事件触发.我认为当javascript将诸如将值设置为隐藏输入之类的东西更改时,可能会触发focusout事件.

It works good in many cases, but there're other cases where click event never get triggered, instead there're one or more focusout events that get triggered. I understad that focusout event may be shooted when javascript changes some like setting value to an hidden input or something like that.

问题是我无法理解为什么在某些情况下未触发点击事件.我可以认为,当功能(上面显示的功能)附加到内容上时,仍有一些元素尚未附加到DOM,但是我不确定并且确实没有找到相关文档.或一种测试方法.如果有人可以帮我一个忙,我会很感激

The problem is that I cannot understand why in some cases click event is not triggered. I could think that in moment when the function (the function showed above) is attached to the content there're some elements that still aren't attached to the DOM, but I'm not sure and really have not find documentation about. or a way to test it. I'll be thankfull if somebody can give me a hand with this

推荐答案

侦听 click 事件的页面元素函数可以在该事件上调用preventDefault()或stopPropagation(),这样您的侦听器就赢了没看到.

The page element function that listens to a click event can call preventDefault() or stopPropagation() on the event so your listener won't see it.

在传播链中第一个事件目标 window :

Try listening in the first phase of the event, the capturing phase, on the first event target in the propagation chain, window:

window.addEventListener('click', yourFunction, true);

或对于现代浏览器:

window.addEventListener('click', yourFunction, {capture: true});

如果这一项也被取消,则您必须执行以下两项操作中的一项或两项:

If this one gets canceled as well, you'll have to do one or both of these two things:

  1. 使用"run_at":"document_start"
  2. 收听 mousedown 事件

这篇关于确保内容脚本可以看到"click"事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆