是否可以模拟isTrusted = true [英] Is it possible to simulate isTrusted=true
问题描述
我希望能够在调用touchStart事件时模拟isTrusted = true。是否有任何类型的库或解决方法可以实现这一目标?
I want to be able to simulate an isTrusted=true when I call a touchStart event. Is there any library or workaround of any sort to make this possible?
这是我以编程方式运行touchStart与实际调用touchStart时的输出。
Here is the output when I run the touchStart programmatically vs. when I actually call the touchStart.
我正在使用手机游戏。根据此网站,移动版Safari不支持但这不可能是正确的,因为输出显示事件的存在。任何有关此的帮助/建议将不胜感激。谢谢。
I am using mobile safari. According to this site mobile safari does not support it but that can't be true as the output shows the presence of the event. Any help/advice on this would be greatly appreciated. Thank you.
推荐答案
不可能。通过脚本触发的事件将始终标记为不受信任(除非使用IE)。这是出于安全原因。换句话说, event.isTrusted === false
如果你通过脚本调用它。
Not possible. Events triggered through scripts will always be marked as not trusted (unless using IE). This is for security reasons. In other words event.isTrusted === false
if you invoke it through script.
欲了解更多信息,请阅读< a href =https://www.w3schools.com/jsref/event_istrusted.asp =nofollow noreferrer>这个。
For more info read this.
非常确定您已经获得了这些结果,但这是一个点击示例。注意第一个控制台消息是假的,这是脚本调用的消息:
Pretty sure you are already getting these results but here is an example with clicks. Notice how the first console message is false which is the one that is called by the script:
const element = document.querySelector('div');
const scriptEvent = new Event('click');
element.addEventListener('click', event => console.log(event.isTrusted));
element.dispatchEvent(scriptEvent);
<div>CLICK HERE</div>
这篇关于是否可以模拟isTrusted = true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!