Youtube Downloader

Add video download button in combo menu.

当前为 2023-12-11 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name                Youtube Downloader
// @version             1.0.2
// @description         Add video download button in combo menu.
// @author              FawayTT
// @namespace           FawayTT
// @icon                https://i.imgur.com/D57wQrY.png
// @match               https://www.youtube.com/watch*
// @grant               none
// @license             MIT
// ==/UserScript==

(function () {
  let timeout;
  let replaced = false;

  function addButton() {
    if (document.location.href.includes('playlist') || document.getElementsByTagName('custom-dwn-button').length !== 0) return;
      const menu = document.getElementsByTagName('ytd-menu-popup-renderer')[0];
      const downButton = document.createElement('custom-dwn-button');
      const icon = document.createElement('div');
      const text = document.createElement('div');
      menu.style.minHeight = '100px';
      downButton.style.cssText = `
          cursor: pointer;
          margin-top: 8px;
          font-size: 1.4rem;
          line-height: 2rem;
          font-weight: 400;
          position: relative;
          color: var(--yt-spec-text-primary);
          font-family: "Roboto","Arial",sans-serif;
          white-space: nowrap;
          display: flex;
          padding: 10px 0 10px 21px;
          gap: 21px;
          align-items: center;`;
      icon.innerText = '⇩';
      text.innerText = 'Download';
      icon.style.cssText = `
            font-size: 2.1rem;`
      downButton.appendChild(icon);
      downButton.appendChild(text);
      downButton.addEventListener('click', onClick);
      downButton.addEventListener('mouseenter', ()=>{
        downButton.style.backgroundColor = 'rgba(255,255,255,0.1)';
      });
      downButton.addEventListener('mouseleave', ()=>{
        downButton.style.backgroundColor = '';
      });
      menu.insertBefore(downButton, menu.firstChild);
  }

  function watchMenu() {
      const menu = document.getElementById('button-shape');
      menu.addEventListener('click', addButton);
      replaced = true;
      clearTimeout(timeout);
  }

  function onClick() {
      window.open(document.location.href.replace('youtube', 'youtubepp'));
  }

  if (document.hidden) {
    window.addEventListener("visibilitychange", ()=>{
      if (document.hidden) return;
      for (let i = 0; i < 10; i++) {
         if (replaced) break;
         timeout = setTimeout(watchMenu, 500 * i);
      }
    })
  } else {
      for (let i = 0; i < 10; i++) {
         if (replaced) break;
         timeout = setTimeout(watchMenu, 500 * i);
      }
  }
})();