使用 JavaScript 关闭 Youtube 注释 [英] Turn Off Youtube Annotations Using JavaScript

查看:23
本文介绍了使用 JavaScript 关闭 Youtube 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我知道有三种方法可以禁用 YouTube 视频中的注释:

There’re currently three ways I know of, to disable annotations in youtube videos:

  1. 您可以使用 YouTube 设置.这对我不起作用,因为我没有(也不想要)帐户.
  2. 您可以使用专门的扩展程序.这可能会奏效,但我宁愿没有包含大量选项的完整扩展,仅此而已.
  3. 您可以使用(广告)拦截扩展程序,并将 ||youtube.com/annotations_ 添加到其过滤器中.这与上一点存在相同的问题.此外,它会完全禁用它们,而我只是希望它们默认关闭(因此我可以选择打开它们).
  1. You can use the YouTube settings. This will not work for me, as I do not have (nor want) an account.
  2. You can use a specialised extension. That might work, but I’d rather not have a full-fledge extension with a ton of options, just for that.
  3. You can use a (ad)blocking extension, and add ||youtube.com/annotations_ to its filters. That suffers from the same issue as the previous point. In addition, it disables them completely, while I simply want them turned off by default (so I have the option to turn them on).

是否可以使用 JavaScript 来实现?我有一个 UserScript,它已经对 YouTube 的网站进行了一些修改,所以理想情况下我想扩展它.这真的是我最缺少的东西.

Is it possible to do it using JavaScript? I have a UserScript that already performs some modifications to YouTube’s website, so ideally I’d like to expand it. That’s really the last thing I’m missing.

我要求答案仅限于使用 JS 而不是浏览器扩展建议.既因为(如前所述),我已经知道这些,而且因为这与学习过程和结果一样重要.这是更多用户脚本的练习.

I ask that answers be constrained to using JS and not browser extensions recommendations. Both because (as mentioned), I already know about those, and because this is as much about the learning process as it is about the result. It’s practice for more UserScripts.

推荐答案

由于 youtube 有时会改变玩家的行为,我会尽量使代码保持最新并尽可能健壮.

Since youtube sometimes changes the players’s behaviour, I’ll try to keep the code up to date and as robust as possible.

var settings_button = document.querySelector(".ytp-settings-button");
settings_button.click(); settings_button.click(); // open and close settings, so annotations label is created

var all_labels = document.getElementsByClassName("ytp-menuitem-label");
for (var i = 0; i < all_labels.length; i++) {
  if ((all_labels[i].innerHTML == "Annotations") && (all_labels[i].parentNode.getAttribute("aria-checked") == "true")) { // find the correct label and see if it is active
    all_labels[i].click(); // and in that case, click it
  }
}

这篇关于使用 JavaScript 关闭 Youtube 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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