为什么调用Window.scroll()会给出一个受信任的事件? [英] Why does calling Window.scroll() give a trusted event?

查看:99
本文介绍了为什么调用Window.scroll()会给出一个受信任的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Chrome扩展程序需要产生类似人类的鼠标和键盘行为(具体而言,生成 isTrusted 值为的事件为true )。除了使用 chrome.debugger API滚动外,我可以做我需要的一切。

I have a Chrome extension that needs to produce human-like mouse and keyboard behavior (specifically, generate events that have a isTrusted value of true). I can do everything I need except for scrolling with the chrome.debugger APIs.

但似乎 Window.scroll()方法足以达到Chrome 52和Firefox 48.0a1的目的。这可以通过将事件监听器附加到页面来观察:

But it seems that the Window.scroll() method is sufficient for this purpose up to Chrome 52 and Firefox 48.0a1. This can be observed by attaching an event listener to the page as follows:

document.addEventListener("scroll", function (event) { 
    console.log("event trusted? " + event.isTrusted);
});

然后运行类似 window.scroll(0,10); 在开发者控制台中。这会记录事件可信吗?真的到开发者控制台。

and then running something like window.scroll(0, 10); in the developer console. This will log event trusted? true to the developer console.

我的问题是:为什么会这样?在这种情况下, isTrusted 属性不应该是 false ,因为滚动事件是由脚本明确生成的吗?

My question is: why is this the case? Shouldn't the isTrusted property be false in this case since the scroll event was clearly generated by a script?

推荐答案

根据规范, DOM Living Standard


注意: isTrusted 表示方便事件是否为(而不是使用 dispatchEvent() )。唯一遗留的例外是 click() ,这会导致用户代理发送活动 isTrusted 属性初始化为false。

此外,在 DOM Level 3 Events Specification


3.4。可信事件



用户代理,其权限不通过 createEvent() 方法,使用 initEvent() 方法,或通过 dispatchEvent() 方法。 isTrusted 属性值 true ,而不受信任的事件具有 isTrusted 属性值 false

3.4. Trusted events

Events that are generated by the user agent, either as a result of user interaction, or as a direct result of changes to the DOM, are trusted by the user agent with privileges that are not afforded to events generated by script through the createEvent() method, modified using the initEvent() method, or dispatched via the dispatchEvent() method. The isTrusted attribute of trusted events has a value of true, while untrusted events have a isTrusted attribute value of false.

因此, isTrusted 仅反映事件是否已分派或者使用 createEvent initEvent dispatchEvent 进行人工创建。现在,根据 Window.scroll 的定义rel =nofollow noreferrer> CSSOM查看模块编辑器的草稿

Thus, isTrusted only reflects if the event has been dispatched or created artificially using createEvent, initEvent, or dispatchEvent. Now, look at the definition of Window.scroll per the CSSOM View Module Editor's Draft:


<$时c $ c> scroll() 方法被调用这些步骤必须运行:

When the scroll() method is invoked these steps must be run:

[... ]


  1. 如果使用两个参数调用,请遵循以下子步骤:

      [...]

     [...]

      ; 12。 执行滚动视口到位置 document 的根元素作为关联元素(如果有),否则为null,滚动行为是行为的值 选项的词典成员

     12. Perform a scroll of the viewport to position, document’s root element as the associated element, if there is one, or null otherwise, and the scroll behavior being the value of the behavior dictionary member of options.

该方法中没有任何地方是使用<$ c创建的人工事件$ c> createEvent , initEvent ,或 dispatchEvent ,因此值 isTrusted true 。请注意,使用 Window.scroll 仍会触发事件处理程序,因为它与事件循环集成,并且 scroll 事件是滚动视口或元素时发出。但,使用 createEvent initEvent dispatchEvent

Nowhere in the method is an artificial event created with createEvent, initEvent, or dispatchEvent, thus the value of isTrusted is true. Note that using Window.scroll still triggers the event handler because it integrates with the event loop, and a scroll event is emitted when a viewport or element is scrolled. This does not, however, use createEvent, initEvent, or dispatchEvent.

使用 isTrusted 事件不是一种可靠的方式检测脚本是否生成了事件。它仅检测是否已使用 createEvent initEvent dispatchEvent创建并分派事件

Using the isTrusted event isn't a sure-fire way of detecting whether a script generated an event. It only detects if an event has been created and dispatched with createEvent, initEvent, or dispatchEvent.

这篇关于为什么调用Window.scroll()会给出一个受信任的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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