fatmanx / Inline Gallery for Reddit

// ==UserScript==
// @name           Inline Gallery for Reddit
// @namespace      http://google.dcom
// @description    Adds inline gallery for reddit.com
// @version        1.0.1
// @author         fatman
// @grant          unsafeWindow
// @grant          GM_xmlhttpRequest
// @include        http://www.reddit.com/*
// @include        http://*.reddit.com/*
// @exclude        http://www.reddit.com/ads/*


// ==/UserScript==
//

(function() {
  
    var imgStyle = "padding:.5vw; border:1px solid #d5d5d5;background:#e3e3e3;";

  $('div#siteTable a.thumbnail[href*="imgur.com"]').each(function(index) {
      //console.log($(this));
      //$(this).css({border:'12px solid red'});
      var h = $(this).attr('href').toString();
      //console.log(h);
      
      if(h.match(/i\.imgur/)){
            
            if(h.match(/\.gifv$/)){
                var hh = h.replace('.gifv', '.webm');
                $(this).next().next().replaceWith('<video controls  width="300"><source src="'+hh+'"></video>');
            }
            else{
                $(this).next().next().replaceWith('<a href="'+h+'" download><img style=" '+imgStyle+' max-height:90vh;max-width:60vw;" src="'+h+'"></a>');
            }
      }
      
      else if(h.match(/imgur\.(.{2,3})(\/a\/|\/gallery\/)/)){
          //console.log(h);
          var t = $(this);
          t.next().next().after('<div style="xheight:50vh;width:75vw;background:#eee;border:1px solid #ddd"></div>');
          var cont = t.next().next().next('div');
          GM_xmlhttpRequest({
              method: "GET", url: h, onload: function(xhr) {
                  var matches = xhr.responseText.match(/new Imgur.LazyLoadContainer[\s\S]*elements: ([\s\S]*?)\}\]/m);
                  var jsn = jQuery.parseJSON('' + matches[1] + '}]');
                  $(jsn).each(function(){
                      var src = '//imgur.com/' + $(this).attr('hash') + $(this).attr('ext');
                      cont.append('<a href="'+src+'" download><img style=" '+imgStyle+' margin:1vw;max-height:90vh" src="'+src+'"></a>');
                  });
                  
              }
          });
          
      }
      else if(h.match(/imgur\.(.{2,3})\/(.{3,})/)){
          var t = $(this);
          GM_xmlhttpRequest({
              method: "GET", url: h, onload: function(xhr) {
                  var matches = xhr.responseText.match(/link rel=\"image_src\"\s+href=\"(.*?)\"/m);
                  if(matches && matches.length>0)
                  var src = matches[1];
                  
                  t.next().next().replaceWith('<a href="'+src+'" download><img style=" '+imgStyle+' max-height:90vh;max-width:60vw;" src="'+src+'"></a>');        
              }
          });
          
          
      }
        
    });
    
    
    $('div#siteTable a.thumbnail[href$=".png"]:not([href*=imgur]),a.thumbnail[href$=".jpg"]:not([href*=imgur]),a.thumbnail[href$=".jpeg"]:not([href*=imgur]),a.thumbnail[href$=".gif"]:not([href*=imgur])').each(function(index) {
         var h = $(this).attr('href').toString();
         $(this).next().next().replaceWith('<a href="'+h+'" download><img style=" '+imgStyle+' max-height:90vh;max-width:60vw;" src="'+h+'"></a>');
     });
  
})();