Youtube Downloader

Add video download button in combo menu.

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

  1. // ==UserScript==
  2. // @name Youtube Downloader
  3. // @version 1.0.2
  4. // @description Add video download button in combo menu.
  5. // @author FawayTT
  6. // @namespace FawayTT
  7. // @icon https://i.imgur.com/D57wQrY.png
  8. // @match https://www.youtube.com/watch*
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. let timeout;
  15. let replaced = false;
  16.  
  17. function addButton() {
  18. if (document.location.href.includes('playlist') || document.getElementsByTagName('custom-dwn-button').length !== 0) return;
  19. const menu = document.getElementsByTagName('ytd-menu-popup-renderer')[0];
  20. const downButton = document.createElement('custom-dwn-button');
  21. const icon = document.createElement('div');
  22. const text = document.createElement('div');
  23. menu.style.minHeight = '100px';
  24. downButton.style.cssText = `
  25. cursor: pointer;
  26. margin-top: 8px;
  27. font-size: 1.4rem;
  28. line-height: 2rem;
  29. font-weight: 400;
  30. position: relative;
  31. color: var(--yt-spec-text-primary);
  32. font-family: "Roboto","Arial",sans-serif;
  33. white-space: nowrap;
  34. display: flex;
  35. padding: 10px 0 10px 21px;
  36. gap: 21px;
  37. align-items: center;`;
  38. icon.innerText = '⇩';
  39. text.innerText = 'Download';
  40. icon.style.cssText = `
  41. font-size: 2.1rem;`
  42. downButton.appendChild(icon);
  43. downButton.appendChild(text);
  44. downButton.addEventListener('click', onClick);
  45. downButton.addEventListener('mouseenter', ()=>{
  46. downButton.style.backgroundColor = 'rgba(255,255,255,0.1)';
  47. });
  48. downButton.addEventListener('mouseleave', ()=>{
  49. downButton.style.backgroundColor = '';
  50. });
  51. menu.insertBefore(downButton, menu.firstChild);
  52. }
  53.  
  54. function watchMenu() {
  55. const menu = document.getElementById('button-shape');
  56. menu.addEventListener('click', addButton);
  57. replaced = true;
  58. clearTimeout(timeout);
  59. }
  60.  
  61. function onClick() {
  62. window.open(document.location.href.replace('youtube', 'youtubepp'));
  63. }
  64.  
  65. if (document.hidden) {
  66. window.addEventListener("visibilitychange", ()=>{
  67. if (document.hidden) return;
  68. for (let i = 0; i < 10; i++) {
  69. if (replaced) break;
  70. timeout = setTimeout(watchMenu, 500 * i);
  71. }
  72. })
  73. } else {
  74. for (let i = 0; i < 10; i++) {
  75. if (replaced) break;
  76. timeout = setTimeout(watchMenu, 500 * i);
  77. }
  78. }
  79. })();