本文介绍了如何使用JS(DOM)聚焦/单击YouTube的注释框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建浏览器扩展以增强YouTube的键盘导航。我想到的快捷方式之一是注释。

I'm creating a browser extension to enhance YouTube's keyboard navigation. One of the shortcuts I have in mind is for commenting.

YouTube使用了很多自定义HTML元素,因此我不清楚什么有效以及何时使用,最终不得不对发生的事情进行逆向工程

YouTube uses a lot of custom HTML elements, so it hasn't been clear to me what works and when, I end up having to reverse-engineer what's going on every time I'm faced with a custom tag.

基本上,我基本上尝试过这样的事情:

I've basically tried mostly something like this:

document.querySelector('#labelAndInputContainer > div').click();

我应该改为使用 focus()吗?

div#labelAndInputContainer 元素一旦获得焦点,就会获得一个名为 focused 的新类。 / p>

3。其他可选挑战


The div#labelAndInputContainer element gets a new class called focused once it is focused.

首先,YouTube不会在视频下方加载评论部分。仅当您向下滚动时,注释部分才会出现。那么,如何确定哪个事件触发了评论部分的加载?我该如何人为地发送它? —否则,注释框的HTML元素将不存在。

At first, YouTube doesn't load the comment section below a video. Only when you scroll down does the comment section appear. So how can I find out which event is triggering the comment section to load? And how can I artificially dispatch it? — Otherwise the HTML element for the comment box will be nonexistent.

我一直在使用 getEventListeners()找出哪些事件

I've been using getEventListeners() to figure out which events work on which elements.

您可能已经注意到,我不是JS专家,所以随时

As you've probably noticed, I'm not a JS expert, so feel free to go down to the basics or give me noob tips.

推荐答案

当前,您可以专注于使用其ID的注释输入:

Currently you can focus on the comment input using its Id:

document.getElementById("simplebox-placeholder").focus();

但是,只有向下翻页并等待加载时,它才会加载。因此,向下翻页并等待一段时间。

However, it is not loaded until you page-down and wait for it to load. So, page-down and wait a while.

此外,根据窗口的宽度,您可能需要进行多次向下翻页。

Also, depending on the width of the window, you might need to multiple page-downs.

这篇关于如何使用JS(DOM)聚焦/单击YouTube的注释框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!