Recurbate download

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

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