Chome控制台.click()在一个网站上工作,但不在另一个网站上工作.为什么和如何? [英] Chome console .click() working on one website, but not on another website. Why and how?
问题描述
当我打开Facebook的 messenger.com 并在控制台中运行document.getElementsByClassName('_1htf')[2].click();
时,它将跳至对话并打开它. _1htf
是我选择的一个随机类,每个对话条目都包含该类.
When I open Facebook's messenger.com and run document.getElementsByClassName('_1htf')[2].click();
in the console, it jumps to a conversation and opens it. _1htf
is a random class that I picked, every conversation entry contains this class.
在 web.whatsapp.com 上进行相同的操作以访问对话,没有任何反应.我什至不能click()
此网站上的真实"按钮.
Doing the same on web.whatsapp.com to access a conversation, nothing happens. I can't even click()
the "real" buttons on this website.
我真的不明白为什么该方法在第一个网站上有效,但是在第二个网站上无效.
I don't really understand why this works on the first, but does not work on the second website.
我能够指出的网站之间有2个区别:
There are 2 differences between the websites that I was able to point out:
-
web.whatsapp.com
会阻止浏览器的鼠标右键菜单上下文菜单,并使用自定义菜单覆盖它-messenger.com
不会这么做
web.whatsapp.com
blocks the browser's right-mouse button context menu and overrides it with a custom one -messenger.com
doesn't do that
messenger.com
的_1htf
元素包含一个导致对话的URI
(您可以在新选项卡中打开uri并结束对话)- web.whatsapp.com
'的元素不包含链接,但单击后仍会以某种方式打开对话
messenger.com
's _1htf
elements contain an URI
that leads to the conversation (you can open the uri in a new tab and end up in the conversation) - web.whatsapp.com
's elements do not contain a link but somehow still open the conversation when clicked
这是如何实现的?是否有一些Javascript可以监听鼠标事件? 如果是,该如何触发事件?我真的需要通过编程方式点击"对话.
How is this achieved? Is there some Javascript listening to mouse events? If so, how can I trigger the event? I really need to "click" the conversation programmatically.
这是web.whatsapp.com
<div class="infinite-list-item infinite-list-item-transition" style="z-index: 200; height: 72px; transform: translateY(144px);">
<div tabindex="-1" class="chat-drag-cover">
<div class="active chat hover">
<div class="chat-avatar">
<div class="avatar icon-user-default">
<div class="avatar-body" style="cursor: pointer;"><img src="https://dyn.web.whatsapp.com/pp?t=s&u=4 . . . " draggable="false" class="avatar-image is-loaded"></div>
</div>
</div>
<div class="chat-body">
<div class="chat-main">
<div class="chat-title"><span class="emojitext ellipsify" dir="auto" title="CONVERSATION PARTNER"><!-- react-text: 92 -->CONVERSATION PARTNER<!-- /react-text --></span></div>
<div class="chat-meta"><span class="timestamp">21:16</span></div>
</div>
<div class="chat-secondary">
<div class="chat-status"><span class="last-msg" title="CONVERSATION MESSAGE"><span class="emojitext" dir="ltr"><!-- react-text: 28843 -->CONVERSATION MESSAGE<!-- /react-text --></span></span>
</div>
<div class="chat-meta"><span></span><span></span><span class="context-container"><div data-icon="down" role="button" class="img btn-context" style="width: 20px; opacity: 1;"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 21 21" width="21" height="21"><path fill-opacity=".31" d="M4.8 6.1l5.7 5.7 5.7-5.7 1.6 1.6-7.3 7.2-7.3-7.2 1.6-1.6z"></path></svg></div></span></div>
</div>
</div>
</div>
</div>
</div>
更新:我试图触发Chrome开发者控制台>元素>事件监听器中列出的许多事件,但似乎都不起作用,它只会输出ƒ focus() { [native code] }
,而不会跳转到对话.
Update: I tried to trigger numerous events listed in Chrome Dev Console > Elements > Event Listeners but non of that seems to work, it only outputs ƒ focus() { [native code] }
but doesn't jump to the conversation.
谢谢
推荐答案
Whatsapp可能会阻止不受信任的点击.
Whatsapp probably blocks untrusted clicks.
要了解我在说什么,请首先在控制台中输入以下内容:
To see what I'm talking about, first type this into the console:
document.onclick = (e) => { console.log(e.isTrusted) };
现在,尝试单击页面上的任意位置.现在应该在控制台中显示true
.
Now, try clicking anywhere on the page. It should show true
in the console now.
如果您尝试执行以下操作:
If you try doing something like this:
document.getElementsByTagName('a')[0].click();
然后isTrusted
将为假,因为它不是真正的点击.
then isTrusted
will be false, since it's not a real click.
据我所知,您无法伪造该值,因此您必须找出一种执行所需操作的不同方法(尝试检查您单击的任何click/mousedown事件处理程序,然后看看是否可以直接调用某些函数.
As far as I know, you can't spoof the value, so you'll have to find out a different way to do what you want (try inspecting any click/mousedown event handlers on what you're clicking, and see if you can call some functions directly).
这篇关于Chome控制台.click()在一个网站上工作,但不在另一个网站上工作.为什么和如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!