imgur.direct

Adds image direct links for imgur uploads.

  1. // ==UserScript==
  2. // @name imgur.direct
  3. // @namespace imgurdir
  4. // @version 1.0.0
  5. // @description Adds image direct links for imgur uploads.
  6. // @author Jakub Rychecký <jakub@rychecky.cz>
  7. // @license WTFPL 2
  8. // @include *imgur.com*
  9. // ==/UserScript==
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. /*
  17. * Worker timeout in miliseconds, 500 ms is recommended
  18. * @type {Number}
  19. */
  20. var timeout = 500;
  21.  
  22.  
  23. /**
  24. * Prefix for direct link (choose http or https - https is recommended)
  25. * @type {String}
  26. */
  27. var prefix = 'https';
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. /**
  38. * DOM initialization runs script worker which
  39. * @returns {undefined}
  40. */
  41.  
  42. $(function(){
  43. imgur_direct_worker(); // The first run of script worker
  44. });
  45.  
  46.  
  47.  
  48.  
  49.  
  50. /**
  51. * Main script worker. Checks new images and put direct links to them. Repeats a few miliseconds again.
  52. * @returns {undefined}
  53. */
  54.  
  55. function imgur_direct_worker(){
  56. $('.post-image-container').each(function(){ // Every image container on imgur upload page...
  57. var img = $(this); // Image container itself
  58.  
  59. if(!has_direct_link(img) && (is_image_ready(img) || is_video(img))){ // Doesn't have direct link yet and image is fully uploaded (or it's video)...
  60. write_direct_link(img); // Create textarea with direct link
  61. }
  62. });
  63.  
  64. setTimeout(imgur_direct_worker, timeout); // Let's repeat
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. /**
  72. * Creates single direct link for image/video inside of its container.
  73. * @param {JQuery} img Image container jQuery element
  74. * @returns {String} Direct link to the image
  75. */
  76.  
  77. function get_direct_link(img){
  78. if(is_video(img)){ // Video direct link for videos...
  79. return get_direct_link_video(img);
  80. }else{ // Everything else is image, let's give it image direct link... ^^
  81. return get_direct_link_image(img);
  82. }
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. /**
  90. * Generates direct MP4 link to video of container.
  91. * @param {type} img Image container jQuery element (contents video in this case)
  92. * @returns {unresolved}
  93. */
  94.  
  95. function get_direct_link_video(img){
  96. return img.find('meta[itemprop="contentURL"]').attr('content'); // Returns content of video meta tag
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103. /**
  104. *
  105. * @param {type} img
  106. * @returns {String}
  107. */
  108.  
  109. function get_direct_link_image(img){
  110. var zoom = img.find('.image .zoom, .post-image .zoom'); // Zoom element for larger images (difference way to get direct link)
  111. if(zoom.length > 0){ // If it is larger image having zoom...
  112. var link = zoom.attr('href'); // Link from zoom iself
  113. }else{ // ...smaller image with no zoom...
  114. var link = img.find('.image img, .post-image img').attr('src'); // Link from image iself
  115. }
  116. link = 'https:'+link; // Link with https:
  117. return link; // Returns link
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124. /**
  125. * Is image ready? Is it fully uploaded yet?
  126. * @param {JQuery} img Image container jQuery element
  127. * @returns {Boolean} Is image ready?
  128. */
  129.  
  130. function is_image_ready(img){
  131. var link = get_direct_link(img); // Direct link from image
  132.  
  133. return link.indexOf('undefined') == -1 && link.indexOf('blob') == -1; // If it DOES NOT contain 'undefined' or 'blob' strings, it's ready
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140. /**
  141. * Checks if image container has direct link already. No need to write another one then.
  142. * @param {JQuery} img Image container jQuery element
  143. * @returns {Boolean} Has direct link already?
  144. */
  145.  
  146. function has_direct_link(img){
  147. return img.find('.direct').length > 0; // Is direct link inside image container?
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /**
  155. * Writes single textarea with image direct link inside imgur page.
  156. * @param {JQuery} img Image container jQuery element
  157. * @returns {undefined}
  158. */
  159.  
  160. function write_direct_link(img){
  161. var css = { // CSS for direct link textarea
  162. 'width': '100%',
  163. 'background': '#2C2F34',
  164. 'text-align': 'center',
  165. 'font-weight': 'bold',
  166. 'font-size': '0.8em',
  167. 'height': '28px',
  168. 'margin-bottom': '0px'
  169. };
  170. var html = $('<textarea onclick="$(this).select()">'+get_direct_link(img)+'</textarea>'); // Direct link textarea as JQuery element
  171. html.addClass('direct').css(css); // Putting class and CSS for textarea
  172. img.prepend(html); // Writes textarea
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179. /**
  180. * Checks if content of container is video (gifv, mp4..).
  181. * @param {JQuery} img Image container jQuery element
  182. * @returns {Boolean} Is this container of video?
  183. */
  184.  
  185. function is_video(img){
  186. return img.find('.video-container').length > 0; // Video has .video-container
  187. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址