Sleazy Fork is available in English.

GfyCat adds link to mp4 file

Adds a link to the mp4 file of gfycat webm, redirects from detail page to actual file page

  1. // ==UserScript==
  2. // @name GfyCat adds link to mp4 file
  3. // @namespace gfycataddmp4link
  4. // @description Adds a link to the mp4 file of gfycat webm, redirects from detail page to actual file page
  5. // @version 0.15
  6. // @author https://greasyfork.org/scripts/32493-gfycat-redirect-to-webm-video-file forked by glub
  7. // @match http*://*.gfycat.com/*
  8. // @match http*://*.redgifs.com/*
  9. // @match http*://*.gifdeliverynetwork.com/*
  10. // @grant none
  11. // @run-at document-idle
  12. // ==/UserScript==
  13.  
  14. //HACK: in case we just want to redirect from this
  15. // if(window.location.hostname.indexOf("gifdeliverynetwork.com") > 0)
  16. // {
  17. // window.location = window.location.href.replace(/gifdeliverynetwork.com/i, "redgifs.com/watch");
  18. // }
  19.  
  20. //FIXME: broken code here, currently not used
  21. function add_detail_element(url){
  22. var detailelement = document.getElementById('block-2');
  23. var myanchor = document.createElement("a");
  24. if (url !== null){
  25. let mydetail = document.createTextNode(url);
  26. myanchor.appendChild(mydetail);
  27. myanchor.href = url;
  28. }
  29. else{
  30. let mydetail = document.createTextNode("No Source");
  31. myanchor.appendChild(mydetail);
  32. }
  33. myanchor.style.backgroundColor = "black";
  34. detailelement.appendChild(myanchor);
  35. }
  36.  
  37. /* need to wait a few seconds otherwise gfycrap scripts overwrite us
  38. we could also try using $(document).ready(function(){[...]}); */
  39. function add_details(url, type, parentnode){
  40. let newdiv = document.createElement("div");
  41. // let mp4text = document.createTextNode("Download MP4");
  42. // let webmtext = document.createTextNode("Download WEBM");
  43. // newdiv.appendChild(mp4text);
  44. // newdiv.appendChild(webmtext);
  45. let final_link = document.createElement("a");
  46. final_link.innerText = "Download " + type.toUpperCase() + " here.";
  47. final_link.setAttribute('href', url);
  48. final_link.setAttribute('class', 'value');
  49. final_link.setAttribute('target', '_blank');
  50. parentnode.appendChild(document.createElement("br"));
  51. parentnode.appendChild(final_link);
  52.  
  53. // grey out visited links
  54. var css = 'a:visited { color: #4c4c5daa }';
  55. var style = document.createElement("style");
  56. style.appendChild(document.createTextNode(css));
  57. document.head.appendChild(style);
  58. }
  59.  
  60. function sleep(ms) {
  61. return new Promise(resolve => setTimeout(resolve, ms));
  62. }
  63.  
  64. // var webmlink, mp4link;
  65. var links = {};
  66.  
  67. // This is the function that does everything currently
  68. function updateInfoFrom(videoPlayer) {
  69. var children = videoPlayer.childNodes;
  70. // console.log("DEBUG: player child nodes: ", children);
  71. children.forEach((child) => {
  72. console.log("Child of children:", child);
  73. let _link;
  74. if (child.type == "video/webm") {
  75. links.webm = child.src;
  76. _link = child.src;
  77. }
  78. if (child.type == "video/mp4") {
  79. if (child.src.indexOf('-mobile') != -1) {
  80. return;
  81. }
  82. links.mp4 = child.src;
  83. _link = child.src;
  84. }
  85. });
  86.  
  87. if (!links.webm) {
  88. // GFYCrap have removed webm and only display mp4, but webm link might still work
  89. // so reconstruct the link from the mp4 link
  90. // webmlink = mp4link.split('.mp4')[0].concat('.webm');
  91. links.webm = links.mp4.split('.mp4').shift() + ".webm";
  92. // console.log(`Rebuilt missing webm to "${links.webm}"`);
  93. }
  94.  
  95. // console.log(`Adding links MP4: ${links.mp4}, WEBM: ${links.webm}`);
  96. addLinks();
  97. }
  98.  
  99. var parentnode;
  100.  
  101. function __delay__(timer) {
  102. // https://stackoverflow.com/questions/7307983
  103. return new Promise(resolve => {
  104. timer = timer || 3000;
  105. setTimeout(function () {
  106. var gifinfo = document.getElementsByClassName('gif-info');
  107. if (gifinfo.length === 0)
  108. {
  109. // try different method as a fallback for gifdeliverynetwork (redirected) pages as they have no gif-info
  110. parentnode = document.getElementsByClassName('wrapper')[0];
  111. }
  112. else
  113. {
  114. parentnode = gifinfo[0].children[0];
  115. }
  116. //console.log("parentnode is: ", parentnode);
  117. resolve();
  118. }, timer);
  119. });
  120. };
  121. // Gfycat has been redirecting to the detail page when trying to access a file page directly
  122. // gifycat.com/gifs/detail/id
  123. // work around that by adding a timer to redirect after 5 seconds
  124. if (window.location.href.indexOf("gifs/detail") > 0) {
  125. sleep(500).then(() => {
  126. window.location = window.location.href.replace(/gifs\/detail\//,"");
  127. });
  128. }
  129. // Normal video page parsing now
  130. else
  131. {
  132. var videoPlayer = document.getElementsByTagName('video')[0];
  133. if (videoPlayer) {
  134. videoPlayer.addEventListener("change", updateInfoFrom(videoPlayer));
  135. }
  136. else {
  137. console.log("DEBUG: no videoplayer found!");
  138. // we don't have a videoplayer (yet?)
  139. // FIXME: broken old code here, currently not used
  140. var superAwesomeGlobalGfyJSON = document.getElementById('webmSource').src;
  141. links.webm = document.getElementById('webmSource').src;
  142. //webm = superAwesomeGlobalGfyJSON.webmUrl;
  143. links.mp4 = document.getElementById('mp4Source').src;
  144. //mp4link = superAwesomeGlobalGfyJSON.mp4Url;
  145. //var caption = document.getElementsByTagName('figcaption')[0].innerHTML;
  146. var source_url = superAwesomeGlobalGfyJSON.url;
  147. if (source_url === null){
  148. add_detail_element(source_url);
  149. }
  150. else if (source_url.indexOf('http') !== -1){ //found http, valid url
  151. //console.log("source_url: ", source_url);
  152. //var sourceurl = source_url.substr(source_url.indexOf('http')); //setting whatever is http in figcaption as sourceurl
  153. add_detail_element(source_url);
  154. }
  155. else{
  156. add_detail_element(null);
  157. }
  158. }
  159. //addLinks();
  160. }
  161.  
  162. function addLinks() {
  163. if (Object.keys(links).length == 0) {
  164. console.log("Didn't find any mp4 link to display!");
  165. return;
  166. }
  167. Object.values(links).forEach((link, idx) => {
  168. if (link) {
  169. setTimeout(async function() {
  170. while (!parentnode) { await __delay__(1000); }
  171. add_details(link, Object.keys(links)[idx] , parentnode);
  172. }, 1);
  173. }
  174. /*sleep(6000).then(() => {
  175. // go to mp4 directly after 6 seconds (not used)
  176. location.assign(mp4);
  177. });*/
  178. });
  179. }