Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

javascript - Ensure `click` event is seen by the content script

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) {

});

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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);

or for the modern browsers:

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. declare the content script with "run_at": "document_start"
  2. listen to mousedown event

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...