Advertisement
bnnie

Untitled

Mar 10th, 2023 (edited)
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // GUROCHAN GALLERY/IMAGE EXPANDER V [0.12], Jank Edition
  2. // Latest version always available at https://pastebin.com/whRbEqhw
  3. // [CHANGELOG]
  4. // V 0.12A
  5. //   Added sort by image count in gallery mode
  6. // [TODO]
  7. // - fix scrolling page to match gallery with expanded images
  8. // - account for auto update images
  9. // - add a 'hide posts without images' function
  10. // - save your spot as a cookie
  11.  
  12. settings={
  13.     fromLocation:true,
  14.     preloadImages:true,
  15.     keybinds:{
  16.         nextImage:"d",
  17.         prevImage:"a",
  18.         toggleGallery:"g",
  19.         expandAll:"x",
  20.         scrollToTop:"t",
  21.         scrollToBottom:"y"
  22.     }
  23. }
  24.  
  25. thumbGen=0;
  26. currentImg=0;
  27. img=[];
  28. imgC=0;
  29. preload=[];
  30. galleryOpen=0;
  31. expanded=0;
  32. expandedImg=[];
  33. firstOpen=1;
  34. start=0;
  35.  
  36. // CSS
  37. $("<style type='text/css'>"+
  38.     "#gallery{ width:100vw; height:100vh; position:fixed; left:0; top:0; z-index:999; } #bigimg{ display:inline-block; background:#a8a8a826; height:100vh; width:400px; } #bigimg>img{ width:100%; height:100%; object-fit:contain; } #thumbs{ display:inline-block; background:#0b0b0b57; width:200px; height:100vh; float:right; overflow:scroll; } .galleryThumb{ display:inline-block; width:100%; height:200px; background:grey; margin-bottom:0px; } .galleryThumb>img{ width:100%; height:100%; object-fit:contain; } .activeThumb{ background: #444242; }  #galleryStats { position: absolute; right: 210px; bottom: 10px; text-align:right; font-size:15px; }"+
  39.     "</style>").appendTo("head");
  40.  
  41. // HTML
  42. $("body").append('<div id="gallery">'+
  43.     '<div id="galleryStats"></div>'+
  44.     '<div id="bigimg"> <img src=""/> </div>'+
  45.     '<div id="thumbs"> </div>'+
  46.     '</div>');
  47.    
  48.    
  49. $("#gallery").hide();
  50. $("#bigimg").width($(window).width()-201);
  51.  
  52. // this could be a css flex element instead of an on resize function
  53. $( window ).resize(function() {
  54.     $("#bigimg").width($(window).width()-201);
  55. });
  56.  
  57. // hacky image preloading
  58. $.fn.preload = function() {
  59.     this.each(function(){
  60.         $('<img/>')[0].src = this;
  61.     });
  62. }
  63.  
  64. i=0;
  65. $(".post-image").each(function(){
  66.     img.push($(this));
  67.     preload.push($(this).parent().attr("href"));
  68.     image=$(this).context.attributes['src'].nodeValue;
  69.     $("#thumbs").append("<div id='gThumb-"+i+"'class='galleryThumb'><img src='"+image+"' /></div>");
  70.     i++;
  71. });
  72. imgC=i-1;
  73.  
  74.  
  75. function nextImg(){
  76.     start=$(window).scrollTop();
  77.     next="";
  78.     ii=0;
  79.     $(".post-image").each(function(){
  80.         ii++
  81.         if(this.y>start) {
  82.             next=this;
  83.             return false;
  84.         }
  85.     })
  86.     if(ii!=0) ii--
  87.     return ii;
  88. }
  89.  
  90.  
  91. function galleryGo(index,doScroll){
  92.     if(firstOpen){
  93.        if(settings.preloadImages) $(preload.slice(index)).preload();
  94.        firstOpen=0;
  95.     }
  96.     doScroll=(typeof(doScroll) === "undefined"?1:doScroll);
  97.     $("#bigimg>img").attr("src",img[index].parent().attr("href"));
  98.     $(".activeThumb").removeClass("activeThumb");
  99.     $("#gThumb-"+index).addClass("activeThumb");
  100.     currentImg=index;
  101.     if(doScroll) scrollTo(index);
  102.     $("html, body").animate({ scrollTop: img[index].offset().top-200+"px" }); // [TODO] this breaks with expand images because offset() is calculated at page load.
  103.     $("#galleryStats").text("Image "+(currentImg+1)+" of "+(imgC+1));
  104. }
  105.  
  106. function scrollTo(id){
  107.     $("#thumbs").scrollTop(id*220-200);
  108. }
  109.  
  110. function expandAll(){
  111.     if(expanded){
  112.         $(expandedImg).each(function(){
  113.             this.click();
  114.         });
  115.     }else{
  116.         start=(settings.fromLocation?$(window).scrollTop():0);
  117.         $(".post-image").each(function(){
  118.             if(this.y>start){
  119.                 $(this).click()
  120.                 expandedImg.push($(this)); // keep track of expanded images
  121.             }
  122.         })
  123.     }
  124.     expanded=(expanded?0:1);
  125. }
  126.  
  127. $("#thumbs").on("click", ".galleryThumb", function(){
  128.     id=this.id;
  129.     index=id.substr(id.indexOf("-")+1);
  130.     galleryGo(parseInt(index),0);
  131.     console.log(id,index);
  132. });
  133.  
  134. // Catalog
  135. if(active_page=="catalog"){
  136.     $(".mix").each(function(){
  137.         // add a data attribute for number of images
  138.         infoStr=$(this).find(".replies strong").text();
  139.         images=parseInt(infoStr.substring(infoStr.lastIndexOf(":")+2));
  140.         $(this).attr("data-images",images)
  141.     });
  142.     // add a sort option for the new data attribute, calls existing sorting function
  143.     $("#sort_by").append('<option value="images:desc">Image Count</option>');
  144.     // [TODO] Override default method for remembering sort to include script options
  145. }
  146.  
  147.  
  148. // Shortcuts
  149. $(document).keydown(function(e){
  150.     if( $("input, textarea").is(":focus")) return; // dont run if input is focussed
  151.     if(active_page=="catalog") return; // dont run in catalog
  152.     switch(e.key.toLowerCase()){
  153.         case settings.keybinds.nextImage:
  154.         case "arrowright":
  155.             if(!galleryOpen) break;
  156.             currentImg=(currentImg==imgC?0:currentImg+1);
  157.             galleryGo(currentImg)
  158.             break;
  159.         case settings.keybinds.prevImage:
  160.         case "arrowleft":
  161.             if(!galleryOpen) break;
  162.             currentImg=(currentImg==0?imgC:currentImg-1);
  163.             galleryGo(currentImg)
  164.             break;
  165.         case settings.keybinds.toggleGallery:
  166.             galleryOpen=(galleryOpen?0:1);
  167.             if(galleryOpen){
  168.                 galleryGo(nextImg());
  169.             }
  170.             $("#gallery").toggle()
  171.             break;
  172.         case "escape":
  173.             if(galleryOpen){
  174.                 $("#gallery").hide();
  175.                 galleryOpen=0;
  176.             }
  177.             break;
  178.         case settings.keybinds.expandAll:
  179.             expandAll();
  180.             break;
  181.         case settings.keybinds.scrollToTop:
  182.             $("html, body").animate({ scrollTop: "0px" });
  183.             break;
  184.         case settings.keybinds.scrollToBottom:
  185.         case "y":
  186.             $("html, body").animate({ scrollTop: $(document).height()+"px" });
  187.             break;
  188.     }
  189. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement