Recurbate downloa

Adds a download button for each video on recurbate.com/cc

  1. // ==UserScript==
  2. // @name Recurbate downloa
  3. // @version 1.1.4
  4. // @description Adds a download button for each video on recurbate.com/cc
  5. // @author razorwax
  6. // @match https://recu.me/
  7. // @match https://es.recu.me/
  8. // @license MIT
  9. // @namespace https://greasyfork.org/users/724632
  10. // ==/UserScript==
  11.  
  12. function ErrorPrint(errorReason)
  13. {
  14. var error = "Recurbate Download Error! [reason = " + errorReason + "]";
  15. console.error(error);
  16. window.alert(error);
  17. }
  18.  
  19. function DownloadVideo(url)
  20. {
  21. if (!url)
  22. {
  23. ErrorPrint("find_video_src_failure");
  24. return;
  25. }
  26. window.open(url, '_blank');
  27. }
  28.  
  29. function FindSourceFromUnstartedVideo(videoBtn)
  30. {
  31. // Find token and id
  32. var token = videoBtn.attr("data-token");
  33. var id = videoBtn.attr("data-video-id");
  34. if (token && id)
  35. {
  36. // Send video request to server
  37. var url = "/api/get.php?video="+id+"&token="+token;
  38. $.get(url, function(data)
  39. {
  40. // Find src from video tag in the response
  41. if (data.includes("<video") && data.includes("src="))
  42. {
  43. var src = data.match(/src=".*"/m);
  44. if (src.length > 0)
  45. {
  46. // Get the source and download it
  47. src = src[0];
  48. src = src.substring(5, src.length - 1);
  49. DownloadVideo(src);
  50. }
  51. }
  52. else
  53. {
  54. window.alert("Server blocked request, reason = " + data);
  55. }
  56. });
  57. }
  58. else
  59. {
  60. ErrorPrint("token_and_id_failure");
  61. }
  62. }
  63.  
  64. function FindSourceFromStartedVideo()
  65. {
  66. var foundVideo;
  67. var searchVideos = $("video");
  68. if (searchVideos.length == 1)
  69. {
  70. // Guess this is the video we are looking for
  71. foundVideo = searchVideos[0];
  72. }
  73. else
  74. {
  75. // Multiple videos found, search for the correct one
  76. var videoIdRegex = /video_\d+/;
  77. for (var video of searchVideos)
  78. {
  79. if (video.id.match(videoIdRegex))
  80. {
  81. foundVideo = video;
  82. break;
  83. }
  84. }
  85. }
  86.  
  87. if (foundVideo)
  88. {
  89. var source = foundVideo.src;
  90. if (!source)
  91. {
  92. // Try to find video source by source element
  93. var sourceElem = $(foundVideo).find("source");
  94. if (sourceElem && sourceElem.length > 0)
  95. {
  96. source = sourceElem[0].src;
  97. }
  98. }
  99. DownloadVideo(source);
  100. }
  101. else
  102. {
  103. ErrorPrint("find_video_failure");
  104. }
  105. }
  106.  
  107. function AddDownloadButton(appendToElem)
  108. {
  109. var downloadBtn = $("<button style=\"margin-left: 10px;\" class=\"btn btn-warning btn-sm\"><b style=\"color:#212528\">Download</b></button>").appendTo(appendToElem);
  110. downloadBtn.on("click", function()
  111. {
  112. // Download button press
  113. var videoBtns = $("#play_button");
  114. if (videoBtns.length > 0)
  115. {
  116. // Found the unstarted video player, find src from it
  117. FindSourceFromUnstartedVideo($(videoBtns[0]));
  118. }
  119. else
  120. {
  121. // Video must have been started
  122. FindSourceFromStartedVideo();
  123. }
  124. });
  125. }
  126.  
  127. // Create download button
  128. $(function ()
  129. {
  130. var addTo = $("a.bookmark");
  131. if (addTo.length == 1)
  132. {
  133. addTo = addTo.parent();
  134. }
  135. else
  136. {
  137. addTo = $("div.video-info").children("div");
  138. }
  139.  
  140. if (addTo.length > 0)
  141. {
  142. AddDownloadButton($(addTo[0]));
  143. }
  144. else
  145. {
  146. ErrorPrint("add_download_btn_failure");
  147. }
  148. });