Youtube direct downloader

Video/short download button hidden in three dots combo menu below video. Downloads MP4, WEBM or MP3 from youtube. Choose your preferred quality from 8k to 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.4
  4. // @description Video/short download button hidden in three dots combo menu below video. Downloads MP4, WEBM or MP3 from youtube. Choose your preferred quality from 8k to 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-only 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. init: onInit,
  80. },
  81. });
  82.  
  83. function opencfg() {
  84. gmc.open();
  85. config.style = `
  86. width: 100%;
  87. height: 100%;
  88. max-height: 40rem;
  89. max-width: 80rem;
  90. border-radius: 10px;
  91. z-index: 9999999;
  92. position: fixed;
  93. `;
  94. }
  95.  
  96. let timeout;
  97. let oldHref = document.location.href;
  98. let menuIndex = 1;
  99. let menuMaxTries = 10;
  100.  
  101. function download(audioOnly) {
  102. switch (gmc.get('downloadService')) {
  103. case 'y2mate':
  104. window.open(document.location.href.replace('youtube', 'youtubepp'));
  105. break;
  106. case 'yt1s':
  107. if (audioOnly) window.open(`https://www.yt1s.com/en/youtube-to-mp3?q=${encodeURI(document.location.href)}`);
  108. else window.open(`https://www.yt1s.com/en/youtube-to-mp4?q=${encodeURI(document.location.href)}`);
  109. break;
  110. default:
  111. GM_xmlhttpRequest({
  112. method: 'POST',
  113. url: 'https://api.cobalt.tools/api/json',
  114. headers: {
  115. 'Cache-Control': 'no-cache',
  116. Accept: 'application/json',
  117. 'Content-Type': 'application/json',
  118. },
  119. data: JSON.stringify({
  120. url: encodeURI(document.location.href),
  121. vQuality: gmc.get('quality'),
  122. vCodec: gmc.get('videoCodec'),
  123. aFormat: gmc.get('audioFormat'),
  124. isAudioOnly: audioOnly || gmc.get('audioOnly'),
  125. }),
  126. onload: (response) => {
  127. const data = JSON.parse(response.responseText);
  128. if (data.url) window.open(data.url);
  129. },
  130. });
  131. break;
  132. }
  133. }
  134.  
  135. function createButton() {
  136. if (document.getElementsByTagName('custom-dwn-button').length !== 0) return;
  137. const menu = document.getElementsByTagName('ytd-menu-popup-renderer')[0];
  138. const downButtonOuter = document.createElement('custom-dwn-button');
  139. const icon = document.createElement('div');
  140. const text = document.createElement('div');
  141. const downButton = document.createElement('button');
  142. const extra = document.createElement('div');
  143. const settings = document.createElement('div');
  144. const downAudioOnly = document.createElement('div');
  145. downAudioOnly.title = 'Download audio only';
  146. settings.title = 'Settings';
  147. menu.style.minHeight = '100px';
  148. menu.style.minWidth = '150px';
  149. downButtonOuter.style.cssText = `
  150. cursor: pointer;
  151. margin-top: 8px;
  152. font-size: 1.4rem;
  153. line-height: 2rem;
  154. font-weight: 400;
  155. position: relative;
  156. color: var(--yt-spec-text-primary);
  157. font-family: "Roboto","Arial",sans-serif;
  158. white-space: nowrap;
  159. display: flex;
  160. margin-bottom: -10px;
  161. padding: 10px 0 10px 21px;
  162. gap: 21px;
  163. align-items: center;`;
  164. downButton.style.cssText = `
  165. position: absolute;
  166. left: 0;
  167. top: 0;
  168. width: 90%;
  169. height: 100%;
  170. opacity: 0;
  171. cursor: pointer;
  172. z-index: 9999;`;
  173. extra.style.cssText = `
  174. position: absolute;
  175. display: flex;
  176. flex-direction: column;
  177. justify-content: center;
  178. align-items: center;
  179. background: rgba(255, 255, 255, 0.2);
  180. border-radius: 3px;
  181. padding: 1px;
  182. color:white;
  183. z-index: 9999;
  184. right: 0;
  185. top: 0;
  186. width: 10%;
  187. height: 90%;`;
  188. icon.innerText = '⇩';
  189. text.innerText = 'Download';
  190. settings.innerText = '☰';
  191. downAudioOnly.innerText = '▶';
  192. icon.style.cssText = `
  193. font-size: 2.1rem;`;
  194. downButtonOuter.appendChild(icon);
  195. downButtonOuter.appendChild(text);
  196. downButtonOuter.appendChild(extra);
  197. downButtonOuter.appendChild(downButton);
  198. extra.appendChild(settings);
  199. extra.appendChild(downAudioOnly);
  200. downButton.addEventListener('click', () => {
  201. download();
  202. });
  203. downAudioOnly.addEventListener('click', () => {
  204. download(true);
  205. });
  206. settings.addEventListener('click', opencfg);
  207. downButtonOuter.addEventListener('mouseenter', () => {
  208. downButtonOuter.style.backgroundColor = 'rgba(255,255,255,0.1)';
  209. });
  210. downButtonOuter.addEventListener('mouseleave', () => {
  211. downButtonOuter.style.backgroundColor = '';
  212. });
  213. menu.insertBefore(downButtonOuter, menu.firstChild);
  214. }
  215.  
  216. function watchMenu() {
  217. menuIndex += 1;
  218. if (menuMaxTries < menuIndex) {
  219. menuIndex = 1;
  220. clearTimeout(timeout);
  221. return;
  222. }
  223. const topRow = document.getElementById('top-row');
  224. const menu = topRow.querySelector('#button-shape');
  225. if (!topRow || !menu) {
  226. timeout = setTimeout(watchMenu, 500 * menuIndex);
  227. return;
  228. }
  229. menu.addEventListener('click', createButton);
  230. menuIndex = 1;
  231. clearTimeout(timeout);
  232. }
  233.  
  234. function modifyMenu() {
  235. if (document.location.href.indexOf('youtube.com/watch') === -1 && document.location.href.indexOf('youtube.com/shorts') === -1) return;
  236. if (document.hidden) {
  237. window.addEventListener('visibilitychange', () => {
  238. if (document.hidden) return;
  239. timeout = setTimeout(watchMenu, 500 * menuIndex);
  240. });
  241. } else {
  242. timeout = setTimeout(watchMenu, 500 * menuIndex);
  243. }
  244. }
  245.  
  246. function onInit() {
  247. const bodyList = document.querySelector('body');
  248. modifyMenu();
  249. const observer = new MutationObserver(function (mutations) {
  250. mutations.forEach(function (mutation) {
  251. if (oldHref != document.location.href) {
  252. oldHref = document.location.href;
  253. modifyMenu();
  254. }
  255. });
  256. });
  257. observer.observe(bodyList, {
  258. childList: true,
  259. subtree: true,
  260. });
  261. }