YouTube add to Watch Later shortcut

Simply Alt-Click a video in the YouTube recommendations to immediately add it to Watch Later.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        YouTube add to Watch Later shortcut
// @namespace   https://greasyfork.org/en/users/1436613-gosha305
// @match       https://www.youtube.com/*
// @license     MIT
// @version     1.0
// @author      gosha305
// @description Simply Alt-Click a video in the YouTube recommendations to immediately add it to Watch Later.
// ==/UserScript==


document.addEventListener("click", function(event){
  if (event.altKey) {
    event.preventDefault()
    event.stopImmediatePropagation();
    clickOnWatchLater(findVideoContainer(event.target))
  }
}, true)

function findVideoContainer(target){
  return (target.closest("#dismissible") || document.querySelector(`[href=\'${target.closest("#media-container-link").getAttribute("href")}\']`).closest("#dismissible"))
}

function clickOnWatchLater(element){
  (new MutationObserver(function(_,observer){
    const watchLaterButton = document.querySelector("ytd-popup-container ytd-menu-service-item-renderer:nth-of-type(2)");
    if (watchLaterButton){
      watchLaterButton.click()
      observer.disconnect()
    }
  })).observe(document.querySelector("ytd-popup-container"), {subtree: true, attributes:true})
  element.querySelector("#button.ytd-menu-renderer > button").click()
}