Which is a bit similar in construct to Jonathan's answer below. But there's some scenarios this will not work, for example, HTML form elements (see comment box on this page) and IFrames (see embed youtube video on this page).
So, here's a little different approach :)
The idea is to put a viewport "mask" that captures clicks to everything in view, but to get the element beneath uses the elementFromPoint API by temporarily hiding the mask and re-enabling it again (so as to not capture itself) :)
I think about 5 minutes of trying out stuff on the dev tools. I didn't actually have to go searching for answer somewhere. I should tell that I was lucky though, to actually detect on this page with the youtube link and input box and that the first method does not work, so I naturally shifted attention trying out the alternate.
// Below: Passing in true for the third parameter causes the event to be captured on the way down. Stopping propagation means that the event never reaches the listeners that are listening for it.constremoveBlocker=()=>{window.addEventListener('click',(event)=>{event.stopPropagation()},true)}// Sometime later...removeBlocker()
Thanks for answering. Did you test this on producthunt?
Apparently there are sites (ie: producthunt) where this should not work because not every click event is a click event. Sometimes there are mousedown and other events such as virtual dom based events as well.
Sounds like there would need to be bindings for each event, with associated un-bindings for each event as well. Probably doesn't change my solution too much, just needs to be applied to more things.
I'm a professional freelancer and a Fullstack web developer. I develop outstanding websites and webapps with beautiful and smooth user interfaces with a fast and scalable backend.
functionaddBlocker(){lethtml=document.querySelector("html")html.style.pointerEvents="none"consthandleClick=e=>{html.style.pointerEvents="auto"console.log({target:document.elementFromPoint(e.clientX,e.clientY),x:e.clientX,y:e.clientY,})html.style.pointerEvents="none"}html.addEventListener("click",handleClick)return()=>{html.style.pointerEvents="auto"html.removeEventListener("click",handleClick)}}// also removes the click event listenervarremoveBlocker=addBlocker();
We're a place where coders share, stay up-to-date and grow their careers.
I was tempted to do something like this initially, with event capturing and cancelling:
Which is a bit similar in construct to Jonathan's answer below. But there's some scenarios this will not work, for example, HTML form elements (see comment box on this page) and IFrames (see embed youtube video on this page).
So, here's a little different approach :)
The idea is to put a viewport "mask" that captures clicks to everything in view, but to get the element beneath uses the
elementFromPointAPI by temporarily hiding the mask and re-enabling it again (so as to not capture itself) :)I didn't really put this code in different pages and test though, but it should hopefully work :)
This is such super cool solution!
Great answer.
I have two questions,
Thanks!
Well.. I believe this is textbook clickjacking :P
I think about 5 minutes of trying out stuff on the dev tools. I didn't actually have to go searching for answer somewhere. I should tell that I was lucky though, to actually detect on this page with the youtube link and input box and that the first method does not work, so I naturally shifted attention trying out the alternate.
5 minutes and textbook clickjacking 👍 .
Those who searched for blocking events would get "event.preventDefault" no matter how much they want to search.
Just a little different search term, specially a common term that they never thought of, would result in a obviously common way.
I will post my solution used in the video later on :D .
I highly suggest others to try out different solutions other than these two already submitted.
Nice! Eager to see other approaches that you may have had :)
For adding all the events:
And for removing the added event:
Thanks for answering. Did you test this on producthunt?
Apparently there are sites (ie: producthunt) where this should not work because not every click event is a click event. Sometimes there are
mousedownand other events such as virtual dom based events as well.Nah, just did a rudimentary test in my console.
Sounds like there would need to be bindings for each event, with associated un-bindings for each event as well. Probably doesn't change my solution too much, just needs to be applied to more things.
I just tried on devTools using
pointer-events.This worked on producthunt too with newly loaded products on scroll.
This is a straightforward solution.
However, only this will not print the element under the mouse on click.
This works ->