简体   繁体   中英

How to make one NOT be able to simulate a click

I've noticed while working for someone on a script that this special button ( the login one ): https://www.easports.com/fifa/ultimate-team/web-app/ does not allow to simulate a click on it in any possible way.

I'm extremely curious to know how they do it. I've tried

var btn=$('#Login > div > div > button.btn-standard.call-to-action');
btn.click(); // or trigger('click');

// or
click = new Event(click);
btn.dispatchEvent(click);

// or

btn.trigger('mousedown');

// oh and also:

function click(x, y)
{
 var ev = new MouseEvent('click', {
     'view': window,
     'bubbles': true,
     'cancelable': true,
     'screenX': x,
     'screenY': y
 });

 var el = document.elementFromPoint(x, y);

 el.dispatchEvent(ev);
}

I've even tried mouseenter, followed by mousedown and mouseup;

How can one achieve this sort of feature?

Here's an example of what they might be doing. Event.isTrusted gives you information on if it was a user action or a created event. They probably have some logic around this:

From the docs :

The isTrusted read-only property of the Event interface is a Boolean that is true when the event was generated by a user action, and false when the event was created or modified by a script or dispatched via EventTarget.dispatchEvent().

 document.getElementById('btn').addEventListener('click', (e) => console.log(e.isTrusted)); // Simulate a click onload (should print false to the console) document.getElementById('btn').click(); // false // TODO: Click the Button manually, you should see 'true' being printed
 <button id="btn">Button</button>

My guess is that if you look into their source code, you'd see something similar where they are just doing an .stopPropogation or .preventDefault if isTrusted is false.

So they are probably doing this:

 document.getElementById('btn1').addEventListener('click', (e) => { if (!e.isTrusted) { e.preventDefault(); return;} console.log('Button clicked!'); }); document.getElementById('btn1').click(); // nothing printed to console.
 <button id="btn1">Button</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM