GelbooruViewer

Eazy way to quickly view pics on Gelbooru.

  1. // ==UserScript==
  2. // @name GelbooruViewer
  3. // @name:zh-CN G站图片浏览工具
  4. // @namespace GelbooruViewerzifux
  5. // @version 0.3.1
  6. // @description Eazy way to quickly view pics on Gelbooru.
  7. // @description:zh-cn 方便的浏览G站图片
  8. // @author zifux
  9. // @match *://gelbooru.com/index.php?page=post&s=list*
  10. // @require https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js
  11. // @run-at document-end
  12. // ==/UserScript==
  13. var piclist=[];
  14. var picindex=-1;
  15. var perPage=42;
  16. var realHeight=0;
  17. var realWidth=0;
  18. var minWidthZoom=0.3;
  19. var img=null;
  20. function nowlist(){
  21. piclist=[];
  22. $(".thumbnail-preview span a")
  23. .each(function(index,a){
  24. var id=a.href.match(/id=([0-9]+)/)[1];
  25. piclist.push({id:id,url:a.href,src:''});
  26. a.href='#';
  27. $(a).attr("onclick",'showPic('+index+')');
  28. });
  29. }
  30. function LoadPic(index,cb){
  31. if(index<0)return;
  32. if(index>piclist.length-1)return;
  33. if(index>picindex+4)return;
  34. if(piclist[index].src==""){
  35. $.get(piclist[index].url,function(html){
  36. var h=$(html);
  37. var imgSrc=$("#image",h).attr("src");
  38. var isVideo=false;
  39. if(imgSrc){
  40. piclist[index].src=imgSrc;
  41. }else{
  42. var videoPlayer=$("#gelcomVideoPlayer",h)[0].outerHTML;
  43. piclist[index].src=videoPlayer;
  44. isVideo=true;
  45. }
  46. if(cb){
  47. cb(piclist[index].src,isVideo);
  48. LoadPic(index+1);
  49. }else{
  50. if(imgSrc){
  51. var imgObj = new Image();
  52. imgObj.src = imgSrc;
  53. imgObj.onload=function(){
  54. LoadPic(index+1);
  55. };
  56. }else{
  57. LoadPic(index+1);
  58. }
  59. }
  60. });
  61. }else{
  62. if(piclist[index].src.startsWith('<video')){
  63. if(cb){
  64. cb(piclist[index].src,true);
  65. }
  66. }
  67.  
  68. else{
  69. if(cb){
  70. cb(piclist[index].src,false);
  71. }
  72. }
  73.  
  74. LoadPic(index+1);
  75. }
  76. }
  77.  
  78. function init(){
  79. createViewer();
  80. realHeight=$(window).height()-40;
  81. realWidth=$(window).width()-40;
  82. img=document.getElementById("gViewerPic");
  83. img.style.maxWidth=realWidth+"px";
  84. img.style.minHeight=realHeight+"px";
  85. img.style.minWidth=realWidth*minWidthZoom+"px";
  86. nowlist();
  87. LoadPic(0);
  88. }
  89. function createViewer(){
  90. $('head').append('<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" >');
  91. $('body').append('<div class="modal fade" tabindex="-1" role="dialog" id="gViewer" style="background-color: rgba(0, 0, 0, 0.45);"> <div id="gViewerWindow" class="modal-dialog modal-lg" role="document"> <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="opacity: .7;color: #5bc0de;position: absolute;right: -20px;top: 2px;"> <span aria-hidden="true">&times;</span> </button> <div id="gViewerContent" class="modal-content" style="background-color: currentColor;overflow: hidden;"> <a id="gViewerA"href="#" onclick="showNextPic()" class="center-block"> <img id="gViewerPic"class="center-block" style="user-select: none;min-height:500px;" src="" onload="onPicLoad(this)"></img> </a> <div class="modal-footer" style="border-top: 1px solid #555;"> <a class="btn btn-success" id="gViewerInfo" href="#" target="_blank">INFO</a><button class="btn btn-success" onclick="showOriginalPic()">Original</button><button autocomplete="off" data-loading-text="Loading..." data-success-text="Success" data-failure-text="Failure" id="gFavor" class="btn btn-warning" onclick="favorPic()" >Favorites</button> <button class="btn btn-info" onclick="showPrePic()">Previous</button> <button class="btn btn-info" onclick="showNextPic()">Next</button> </div> </div> </div> </div>'
  92. +'');
  93. $('.navbar-default .navbar-nav>li>a').css('color','#fff');
  94. }
  95. function resetPic(){
  96. $("#gFavor").button("reset");
  97. var img=document.getElementById("gViewerPic");
  98. img.style.height=null;
  99. img.style.width=null;
  100. img.style.minHeight=realHeight+"px";
  101. img.style.minWidth=realWidth*minWidthZoom+"px";
  102. }
  103. (function() {
  104. 'use strict';
  105. document.showPic=function (index){
  106. img.style.opacity="0.5";
  107. resetPic();
  108. picindex=index;
  109. $('#gViewerInfo').attr("href",piclist[index].url);
  110. LoadPic(index,function(src,isVideo){
  111. $('#gViewerContent video').remove();
  112. if(isVideo){
  113. $('#gViewerA').hide();
  114. $('#gViewerContent').append(src);
  115. }else{
  116. $('#gViewer img').attr("src",src);
  117. //img.style.visibility="hidden";//.css("visibility","hidden");
  118. $('#gViewerA').show();
  119. }
  120. });
  121. $('#gViewer').modal({backdrop:false});
  122. };
  123. document.showNextPic=function(){
  124. if(picindex>=piclist.length-1){
  125. $('#gViewer').modal('hide');
  126. return;
  127. }
  128. document.showPic(++picindex);
  129. };
  130. document.showPrePic=function(){
  131. if(picindex<=0)return;
  132. document.showPic(--picindex);
  133. };
  134. document.showOriginalPic=function(){
  135. img.style.height=null;
  136. img.style.width=null;
  137. img.style.minHeight=null;
  138. img.style.minWidth=null;
  139. $('#gViewerWindow').width(img.width+2);
  140. };
  141. document.favorPic=function(){
  142. var btn=$("#gFavor");
  143. btn.button("loading");
  144. function res(d,s){
  145. if(d=='1'){
  146. btn.button("success");
  147. }else if(d=='3'){
  148. btn.button("success");
  149. }else if(d=='2'){
  150. alert("You are not logged in.");
  151. btn.button("failure");
  152. }else{
  153. btn.button("failure");
  154. }
  155. }
  156. $.get('/public/addfav.php?id='+piclist[picindex].id,res);
  157. $.get('/index.php?page=post&s=vote&type=up&id='+piclist[picindex].id);
  158. };
  159. document.onPicLoad=function(nimg){
  160. if(nimg.naturalWidth>realWidth*minWidthZoom&&nimg.naturalHeight>realHeight){
  161. var zoomWidth=nimg.naturalWidth*realHeight/nimg.naturalHeight;
  162. console.log(zoomWidth,realWidth*minWidthZoom+3);
  163. if(zoomWidth>realWidth*minWidthZoom+3){
  164. nimg.style.height=realHeight+"px";
  165. }else{
  166. nimg.style.width=realWidth*minWidthZoom+3+"px";
  167. }
  168. }
  169. $('#gViewerWindow').width(nimg.width+2);
  170. img.style.opacity="1";
  171. };
  172. init();
  173. })();