Youtube direct downloader

Video/short download button hidden in three dots combo menu below video. Downloads MP4 or MP3 from youtube. You can choose your preferred quality from 8k or audio only, codec (h264, vp9 or av1) or service provider (cobalt, y2mate, yt1s) in settings.

目前为 2024-07-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube direct downloader
  3. // @version 2.0.2
  4. // @description Video/short download button hidden in three dots combo menu below video. Downloads MP4 or MP3 from youtube. You can choose your preferred quality from 8k or audio only, codec (h264, vp9 or av1) or service provider (cobalt, y2mate, yt1s) in settings.
  5. // @author FawayTT
  6. // @namespace FawayTT
  7. // @icon https://i.imgur.com/D57wQrY.png
  8. // @match https://www.youtube.com/*
  9. // @connect api.cobalt.tools
  10. // @require https://openuserjs.org/src/libs/sizzle/GM_config.js
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_xmlhttpRequest
  15. // @license MIT
  16. // ==/UserScript==
  17.  
  18. GM_registerMenuCommand('Settings', opencfg);
  19.  
  20. const defaults = {
  21. downloadService: 'cobalt',
  22. quality: 'max',
  23. vCodec: 'vp9',
  24. aFormat: 'mp3',
  25. audioOnly: false,
  26. };
  27.  
  28. let gmc = new GM_config({
  29. id: 'config',
  30. title: 'Youtube direct downloader settings',
  31. fields: {
  32. downloadService: {
  33. section: ['Download method, use cobalt for best quality.'],
  34. label: 'Service',
  35. labelPos: 'left',
  36. type: 'select',
  37. default: defaults.downloadService,
  38. options: ['cobalt', 'y2mate', 'yt1s'],
  39. },
  40. quality: {
  41. section: ['Cobalt settings'],
  42. label: 'Quality',
  43. labelPos: 'left',
  44. type: 'select',
  45. default: defaults.quality,
  46. options: ['max', '2160', '1440', '1080', '720', '480', '360', '240', '144'],
  47. },
  48. videoCodec: {
  49. label: 'Video codec: (h264 [MP4] for best compatibility, vp9 [WEBM] for better quality. AV1 = best quality but is used only by few videos.)',
  50. labelPos: 'left',
  51. type: 'select',
  52. default: defaults.vCodec,
  53. options: ['h264', 'vp9', 'av1'],
  54. },
  55. audioFormat: {
  56. label: 'Audio format.',
  57. type: 'select',
  58. default: defaults.aFormat,
  59. options: ['best', 'mp3', 'ogg', 'wav', 'opus'],
  60. },
  61. audioOnly: {
  62. label: 'Always download only audio',
  63. type: 'checkbox',
  64. default: defaults.audioOnly,
  65. },
  66. url: {
  67. section: ['Support'],
  68. label: 'https://github.com/FawayTT/userscripts',
  69. type: 'button',
  70. click: () => {
  71. GM_openInTab('https://github.com/FawayTT/userscripts');
  72. },
  73. },
  74. },
  75. events: {
  76. save: function () {
  77. gmc.close();
  78. },
  79. },
  80. });
  81.  
  82. function opencfg() {
  83. gmc.open();
  84. }
  85.  
  86. (function () {
  87. let timeout;
  88. let replaced = false;
  89. let oldHref = document.location.href;
  90.  
  91. function download(audioOnly) {
  92. switch (gmc.get('downloadService')) {
  93. case 'y2mate':
  94. window.open(document.location.href.replace('youtube', 'y2mate'));
  95. break;
  96. case 'yt1s':
  97. if (audioOnly) window.open(`https://www.yt1s.com/en/youtube-to-mp3?q=${encodeURI(document.location.href)}`);
  98. else window.open(`https://www.yt1s.com/en/youtube-to-mp4?q=${encodeURI(document.location.href)}`);
  99. break;
  100. default:
  101. GM_xmlhttpRequest({
  102. method: 'POST',
  103. url: 'https://api.cobalt.tools/api/json',
  104. headers: {
  105. 'Cache-Control': 'no-cache',
  106. Accept: 'application/json',
  107. 'Content-Type': 'application/json',
  108. },
  109. data: JSON.stringify({
  110. url: encodeURI(document.location.href),
  111. vQuality: gmc.get('quality'),
  112. vCodec: gmc.get('videoCodec'),
  113. aFormat: gmc.get('audioFormat'),
  114. isAudioOnly: audioOnly || gmc.get('audioOnly'),
  115. }),
  116. onload: (response) => {
  117. const data = JSON.parse(response.responseText);
  118. if (data.url) window.open(data.url);
  119. },
  120. });
  121. break;
  122. }
  123. }
  124.  
  125. function createButton() {
  126. if (document.getElementsByTagName('custom-dwn-button').length !== 0) return;
  127. const menu = document.getElementsByTagName('ytd-menu-popup-renderer')[0];
  128. const downButtonOuter = document.createElement('custom-dwn-button');
  129. const icon = document.createElement('div');
  130. const text = document.createElement('div');
  131. const downButton = document.createElement('button');
  132. const extra = document.createElement('div');
  133. const settings = document.createElement('div');
  134. const downAudioOnly = document.createElement('div');
  135. downAudioOnly.title = 'Download audio only';
  136. settings.title = 'Settings';
  137. menu.style.minHeight = '100px';
  138. menu.style.minWidth = '150px';
  139. downButtonOuter.style.cssText = `
  140. cursor: pointer;
  141. margin-top: 8px;
  142. font-size: 1.4rem;
  143. line-height: 2rem;
  144. font-weight: 400;
  145. position: relative;
  146. color: var(--yt-spec-text-primary);
  147. font-family: "Roboto","Arial",sans-serif;
  148. white-space: nowrap;
  149. display: flex;
  150. margin-bottom: -10px;
  151. padding: 10px 0 10px 21px;
  152. gap: 21px;
  153. align-items: center;`;
  154. downButton.style.cssText = `
  155. position: absolute;
  156. left: 0;
  157. top: 0;
  158. width: 90%;
  159. height: 100%;
  160. opacity: 0;
  161. cursor: pointer;
  162. z-index: 9999;`;
  163. extra.style.cssText = `
  164. position: absolute;
  165. display: flex;
  166. flex-direction: column;
  167. justify-content: center;
  168. align-items: center;
  169. background: rgba(255, 255, 255, 0.2);
  170. border-radius: 3px;
  171. padding: 1px;
  172. color:white;
  173. z-index: 9999;
  174. right: 0;
  175. top: 0;
  176. width: 10%;
  177. height: 90%;`;
  178. icon.innerText = '⇩';
  179. text.innerText = 'Download';
  180. settings.innerText = '☰';
  181. downAudioOnly.innerText = '▶';
  182. icon.style.cssText = `
  183. font-size: 2.1rem;`;
  184. downButtonOuter.appendChild(icon);
  185. downButtonOuter.appendChild(text);
  186. downButtonOuter.appendChild(extra);
  187. downButtonOuter.appendChild(downButton);
  188. extra.appendChild(settings);
  189. extra.appendChild(downAudioOnly);
  190. downButton.addEventListener('click', () => {
  191. download();
  192. });
  193. downAudioOnly.addEventListener('click', () => {
  194. download(true);
  195. });
  196. settings.addEventListener('click', opencfg);
  197. downButtonOuter.addEventListener('mouseenter', () => {
  198. downButtonOuter.style.backgroundColor = 'rgba(255,255,255,0.1)';
  199. });
  200. downButtonOuter.addEventListener('mouseleave', () => {
  201. downButtonOuter.style.backgroundColor = '';
  202. });
  203. menu.insertBefore(downButtonOuter, menu.firstChild);
  204. }
  205.  
  206. function watchMenu() {
  207. const menu = document.getElementById('button-shape');
  208. menu.addEventListener('click', createButton);
  209. replaced = true;
  210. clearTimeout(timeout);
  211. }
  212.  
  213. function modifyMenu() {
  214. if (document.hidden) {
  215. window.addEventListener('visibilitychange', () => {
  216. if (document.hidden) return;
  217. for (let i = 0; i < 10; i++) {
  218. if (replaced) break;
  219. timeout = setTimeout(watchMenu, 500 * i);
  220. }
  221. });
  222. } else {
  223. for (let i = 0; i < 10; i++) {
  224. if (replaced) break;
  225. timeout = setTimeout(watchMenu, 500 * i);
  226. }
  227. }
  228. }
  229.  
  230. window.onload = function () {
  231. const bodyList = document.querySelector('body');
  232. modifyMenu();
  233. const observer = new MutationObserver(function (mutations) {
  234. mutations.forEach(function (mutation) {
  235. if (oldHref != document.location.href) {
  236. oldHref = document.location.href;
  237. if (window.location.href.indexOf('youtube.com/watch') > -1 || window.location.href.indexOf('youtube.com/shorts') > -1) {
  238. modifyMenu();
  239. }
  240. }
  241. });
  242. });
  243. observer.observe(bodyList, {
  244. childList: true,
  245. subtree: true,
  246. });
  247. };
  248. })();