Are events generated by Firefox extension 'trusted'?

吃可爱长大的小学妹 提交于 2019-11-27 15:12:40

Edit: This answer is badly outdated. It refers to classic extensions that are no longer supported as of Firefox 57. Extensions based on the Web Extensions API have no way of generating trusted events.

Yes, events generated by extensions are always trusted. That means that event.isTrusted will be true and the events will be able to trigger actions that require trusted events (e.g. Ctrl-Tab keypress event to switch browser tabs). However, they stay synthesized events meaning that there is no native (OS-level) event associated with them. And since the pop-up blocker works with native events it will not see the events generated by your extension.

You can use nsIDOMWindowUtils.sendMouseEventToWindow() instead of document.createEvent(). This method is meant for testing and will generate a native event as well. This should be good enough for the pop-up blocker.

var utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                  .getInterface(Components.interfaces.nsIDOMWindowUtils);
utils.sendMouseEventToWindow("click", 10, 20, 0, 1, 0);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!