Download video/audio from YouTube quickly via SaveFrom

Adds buttons in userscript manager on YouTube videos to go to SaveFrom and get video or audio, and automatically selects and downloads the highest quality video/audio on SaveFrom links

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

  1. // ==UserScript==
  2. // @name Download video/audio from YouTube quickly via SaveFrom
  3. // @namespace https://github.com/fxolan/userscripts
  4. // @version 1.0
  5. // @description Adds buttons in userscript manager on YouTube videos to go to SaveFrom and get video or audio, and automatically selects and downloads the highest quality video/audio on SaveFrom links
  6. // @match https://en.savefrom.net/*
  7. // @match https://www.youtube.com/*
  8. // @exclude https://www.youtube.com/feed/*
  9. // @exclude https://www.youtube.com/channel/*
  10. // @grant GM_registerMenuCommand
  11. // @homepage https://github.com/fxolan/userscripts
  12. // @license The Unlicense
  13. // @supportURL https://github.com/fxolan/userscripts/issues
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. const url = window.location.href;
  19. const saveF = 'https://en.savefrom.net/';
  20.  
  21. function overrideTimeout() {
  22. const originalSetTimeout = window.setTimeout;
  23. window.setTimeout = function(callback, delay) {
  24. return originalSetTimeout(callback, 1);
  25. };
  26. }
  27.  
  28. function getHighestQuality() {
  29. // Get all the video download links on the page
  30. const links = document.querySelectorAll('.main > div.link-group > .ga_track_events.subname.link-download.link');
  31.  
  32. let highestQuality = 0;
  33. let highestQualityLink = null;
  34.  
  35. // Iterate through the video download links
  36. for (let i = 0; i < links.length; i++) {
  37. const link = links[i];
  38. const words = link.innerText.split(" ");
  39. const lastWord = words.pop();
  40. if(localStorage.getItem('format')) {
  41. if(link.innerText.startsWith('MP4')) {
  42. if (parseInt(lastWord) > highestQuality) {
  43. highestQuality = parseInt(lastWord);
  44. highestQualityLink = link;
  45. }
  46. }
  47. }
  48. else {
  49. if(link.innerText.startsWith('Audio')) {
  50. if (parseInt(lastWord) > highestQuality) {
  51. highestQuality = parseInt(lastWord);
  52. highestQualityLink = link;
  53. }
  54. }
  55. }
  56. }
  57. highestQualityLink.click();
  58. }
  59.  
  60. function dlV() {
  61. window.open(saveF + url, '_blank').focus();
  62. }
  63.  
  64. function dlA() {
  65. localStorage.setItem('format', 'false');
  66. window.open(saveF + url, '_blank').focus();
  67. }
  68.  
  69. if(url.startsWith(saveF)) {
  70. overrideTimeout();
  71. const intervalId = setInterval(function() {
  72. if (document.querySelector('.links')) {
  73. clearInterval(intervalId);
  74. getHighestQuality();
  75. }
  76. }, 200);
  77. } else {
  78. localStorage.setItem('format', true);
  79. GM_registerMenuCommand('Download Video', dlV);
  80. GM_registerMenuCommand('Download Audio', dlA);
  81. }
  82.  
  83. })();