用于禁用YouTube的"up next"/自动播放功能的脚本或插件



是否有任何方法可以完全使用脚本禁用YouTube"下一步"或自动播放功能或为Firefox添加吗?请注意,名称为" auto"播放"令人困惑。我特别指的是在当前视频完成播放后将播放"建议"视频的功能。该功能通常会陷入循环中,重复相同的两个视频ad infinitum。此外,当我在视频下方写评论时,它通常会播放下一个视频。最近的UI更改打破了我用来完成此任务的添加。启用cookie对我来说不是一个选择,因为那时我必须在这两个不愉快的替代方案之间进行选择:

  1. 保持cookie并处理污染当前视频侧面链接的侵入性"推荐"视频。

  2. 处理这个可怕的功能,该功能似乎是专门设计用于从用户那里提取广告收入的。

我不相信此线程是重复的,因为我不知道在最近的UI发生更改后,我不知道任何可禁用自动播放功能的脚本(在对Github进行了大量搜索之后,Firefox(firefox)围绕:addons and Google)。如果脚本符合两个条件,这将非常有帮助。

  1. 它不依赖琐碎的解决方案,例如在视频结束之前1秒钟暂停。
  2. 在YouTube再次更改其UI之后,它仍然会起作用。

下面是一个与我在更新之前使用的usercript相似的,但现在已损坏。

 (function () {
'use strict';
function removeAPUN() {
    var autoplaybar = document.getElementsByClassName('autoplay-bar')[0];
    if (autoplaybar) {
        autoplaybar.removeAttribute('class');
        document.getElementsByClassName('checkbox-on-off')[0].remove();
    }
}
window.addEventListener('readystatechange', removeAPUN, true);
window.addEventListener('spfdone', removeAPUN);

}());

预先感谢!

您说完全禁用该功能,因此我不确定以下内容是100%您需要的,但也许会有所帮助。

以下内容一直在寻找"禁用自动播放"按钮,直到存在,然后单击它,如果当前设置为活动:

setTimeout(disable_autoplay, 1000);
function disable_autoplay(){
    // Try to get the toggle button
    var toggle_button = document.getElementById("toggle");
    if (toggle_button){
        if (toggle_button.hasAttribute("checked")){
            // If it is checked, click it to disable autoplay
            toggle_button.click();
            // Retry one more time to make sure it stays disabled
            setTimeout(disable_autoplay, 5000);
        } 
    } else {
        // Not found, retry
        console.log("Retrying in 1s");
        setTimeout(disable_autoplay, 1000);
    }
}

单击按钮后,它将再重新恢复一次,我不确定这是必需的,但是当我手动禁用自动播放时,有时它会在稍后再切换,所以这实际上只是为了确保它保持不变。

这是我为个人使用的解决方案的改编,可用于新的和旧的YouTube布局。可能会有一个更简单的解决方案,但是该解决方案不使用间隔计时器来避免电池耗尽。

function fixAutoplayNext()
{
    let autoplay = document.getElementsByClassName("autoplay-bar")[0];
    if (autoplay) // old layout
    {
        let check = document.querySelector("#autoplay-checkbox-label > .checked");
        let input = document.getElementById("autoplay-checkbox") || document.getElementById("toggle");
        if (check && input)
        {
            if (parseInt(window.getComputedStyle(check).width, 10) > 0)
            {
                input.click();
            }
        }
        // Removing the switch is optional, but I don't see a reason to leave it around.
        autoplay.parentNode.removeChild(autoplay);
    }
    else // new layout
    {
        autoplay = document.getElementById("head");
        if (autoplay)
        {
            let toggler = document.getElementById("toggle");
            if (toggler && toggler.hasAttribute("checked"))
            {
                //toggler.click();
                toggler.removeAttribute("checked");
                toggler.dispatchEvent(new Event("change"));
            }
            // Removing the switch is optional, but I don't see a reason to leave it around.
            autoplay.parentNode.removeChild(autoplay);
        }
    }
}
fixAutoplayNext();
// Youtube loads pages entirely using Ajax. Listening for a video to load is enough for now.
document.addEventListener("loadstart", function(e) {
    if (e.target instanceof window.HTMLMediaElement)
    {
        window.setTimeout(fixAutoplayNext, 25);
    }
}, true );

我将Adisib的脚本扩展为另一个布局。

function fixAutoplayNext() {
  let autoplay = document.getElementsByClassName('autoplay-bar')[0];
  if (autoplay) { // Old case 1
    const check = document.querySelector('#autoplay-checkbox-label > .checked');
    const input = document.getElementById('autoplay-checkbox') || document.getElementById('toggle');
    if (check && input) {
      if (parseInt(window.getComputedStyle(check).width, 10) > 0) {
        input.click();
      }
    }
    // Removing the switch is optional, but I don't see a reason to leave it around.
    autoplay.parentNode.removeChild(autoplay);
  } else { // Old case 2
    autoplay = document.getElementById('head');
    if (autoplay) {
      console.log(autoplay);
      const toggler = document.getElementById('toggle');
      if (toggler && toggler.hasAttribute('checked')) {
        // toggler.click();
        toggler.removeAttribute('checked');
        toggler.dispatchEvent(new Event('change'));
      }
      // Removing the switch is optional, but I don't see a reason to leave it around.
      autoplay.parentNode.removeChild(autoplay);
    } else { // Newest case
      const video = document.querySelector('video');
      video.addEventListener('ended', (event) => {
        setTimeout(() => {
          const button = document.getElementsByClassName("ytp-autonav-endscreen-upnext-button ytp-autonav-endscreen-upnext-cancel-button")[0];
          button.click();
        }, 25);
      });
    }
  }
}
fixAutoplayNext();
document.addEventListener('loadstart', (e) => {
  if (e.target instanceof window.HTMLMediaElement) {
    window.setTimeout(fixAutoplayNext, 25);
  }
}, true);

最新更新