Reddit Video Downloader

A script to that allows you to download videos hosted on Reddit by pressing Ctrl+S or Meta+S on the comments section.

  1. // ==UserScript==
  2. // @name Reddit Video Downloader
  3. // @author Berk "SAS41" Alyamach
  4. // @homepage https://github.com/sas41/
  5. // @homepageURL https://github.com/sas41/
  6. // @description A script to that allows you to download videos hosted on Reddit by pressing Ctrl+S or Meta+S on the comments section.
  7.  
  8. // @icon https://github.com/sas41/RedditVideoDownloader/blob/master/icons/RVD_icon_32.png?raw=true
  9. // @iconURL https://github.com/sas41/RedditVideoDownloader/blob/master/icons/RVD_icon_32.png?raw=true
  10. // @icon64URL https://github.com/sas41/RedditVideoDownloader/blob/master/icons/RVD_icon_64.png?raw=true
  11.  
  12. // @copyright 2018, Berk (sas41) Alyamach - https://github.com/sas41/
  13. // @license MIT
  14. // @grant none
  15.  
  16. // @contributionAmount €1.00
  17. // @contributionURL https://www.paypal.me/sas41/1
  18.  
  19. // @namespace reddit
  20. // @include *://*reddit.com/r/*/*/*/*
  21.  
  22. // @supportURL https://github.com/sas41/RedditVideoDownloader/issues
  23. // @version 1.0.5
  24. // ==/UserScript==
  25.  
  26. // ==OpenUserJS==
  27. // @author sas41
  28. // @contributionAmount €1.00
  29. // @contributionURL https://www.paypal.me/sas41/1
  30. // ==/OpenUserJS==
  31.  
  32. var jsonLink = document.location.href.split('?')[0] + '.json';
  33. var downloadLink = '';
  34.  
  35. function getJSON(url)
  36. {
  37. var xhr = new XMLHttpRequest();
  38. xhr.open('GET', url, true);
  39. xhr.responseType = 'json';
  40. xhr.onload = function()
  41. {
  42. var status = xhr.status;
  43. if (status === 200)
  44. {
  45. var response = xhr.response;
  46. if (!response[0].data.children[0].data.secure_media.reddit_video.is_gif)
  47. {
  48. if(confirm('Unfortunately, Reddit doesn\'t support saving of audio along with the video, Download without audio?'))
  49. {
  50. downloadLink = response[0].data.children[0].data.secure_media.reddit_video.fallback_url;
  51. downloadURI(downloadLink,'');
  52. }
  53. }
  54. else
  55. {
  56. downloadLink = response[0].data.children[0].data.secure_media.reddit_video.fallback_url;
  57. downloadURI(downloadLink,'');
  58. }
  59. }
  60. else
  61. {
  62. alert('Sorry, Download Failed');
  63. }
  64. };
  65. xhr.send();
  66. }
  67.  
  68. function downloadURI(url, n)
  69. {
  70. var save = document.createElement('a');
  71. save.href = url;
  72. save.download = n || url;
  73. var event = document.createEvent("MouseEvents");
  74. event.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  75. save.dispatchEvent(event);
  76.  
  77. try
  78. {
  79. document.body.removeChild(save);
  80. }
  81. catch(err){}
  82. }
  83.  
  84. document.addEventListener('keydown', (event) => {
  85. if (event.ctrlKey || event.metaKey)
  86. {
  87. if (event.key === 's' || event.key === 'S' )
  88. {
  89. event.preventDefault();
  90. getJSON(jsonLink);
  91. }
  92. }
  93. });