🏠 Home 

MyShowBox

显示图片的库

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/515677/1525859/MyShowBox.js

  1. // ==UserScript==
  2. // @name MyShowBox
  3. // @version 2025.01.24.04
  4. // @description 修复touch就滚两页的bug
  5. // @author You
  6. // @grant none
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @grant GM_deleteValue
  10. // @grant GM_download
  11. // @require https://update.greasyfork.org/scripts/480132/1476440/Get_all_img_Library.js
  12. // @require https://update.greasyfork.org/scripts/515674/1518464/MyDownloader.js
  13. // ==/UserScript==
  14. /**
  15. * 把图片全部添加到一个Showbox里的类,只能new一次
  16. * @class
  17. * @example
  18. const showBox = new ShowBox();
  19. showBox.Add(imgs);
  20. @ps Add函数只能调用一次
  21. */
  22. function ShowBox(){
  23. let imgs = null;
  24. let num = 10;
  25. let showNum = num;
  26. let nowIndex = 0;
  27. /**
  28. * @type {Downloader}
  29. */
  30. let downloader = window.Downloader ? new window.Downloader() : (() => { throw new DOMException("Downloader does not exist"); })();
  31. let box = $('.clickShowBox').length > 0 ? $('.clickShowBox') : CreateShowBox()
  32. downloader.downloadType.value="GM_download";
  33. this.downloader = {get obj(){return downloader;}}
  34. /**
  35. * 把图片全部添加到一个Showbox里
  36. * @example Add(imgs)
  37. * @param {JQuery} iimgs
  38. * @ps 只能调用一次,等图片获取完了再调用
  39. */
  40. this.Add = (iimgs)=>{
  41. imgs = iimgs;
  42. AddImgs();
  43. }
  44. /**
  45. * @example controlType = "mouse"
  46. */
  47. this.controlType = "mouse";
  48. /**
  49. * 设置一次预加载多少图片,默认是10
  50. * @example SetShowNum = 20
  51. * @param {number} n
  52. * @ps 未加载的图片src为空,只有small_src和big_src
  53. */
  54. this.SetShowNum = (n) => {showNum = num = n;}
  55. /**
  56. * @param {string} type - {GM_download / atag / blob}
  57. */
  58. this.Set_donwloadType = (type)=>{downloader.downloadType=type;}
  59. /**
  60. * 重写下载图片的方法
  61. * @example SetDonloadFunction((imgs)=>{...})
  62. * @param {function(JQuery)} foo - (imgs)=>{}
  63. */
  64. this.SetDonloadFunction = (foo)=>{downloader.Download_img = foo;}
  65. function AddImgs(){
  66. imgs.each(function(){
  67. const item = $('<div class="item"></div>')
  68. .append($('<img>').attr({'small_src':this.src,src:"",'big_src':$(this).attr('big_src')}));
  69. $('.clickShowBox').append(item);
  70. })
  71. ClickShowNext({img:$('img'),onlyDown:false});
  72. }
  73. function CreateShowBox(){
  74. let box = `
  75. <div class="clickShowBox">
  76. <p class="pages">1/10</p>
  77. <button class="close">x</button>
  78. <div class="downloadBU">
  79. <button class="download">↓</button>
  80. <button class="downloadall">↓↓</button>
  81. </div>
  82. </div>
  83. <div class="clickShowBox_ShowBu"></div>
  84. `
  85. box = $(box);
  86. $('body').prepend(box);
  87. $('.clickShowBox .close').click(function(){
  88. $('.clickShowBox').fadeOut();
  89. $('.clickShowBox_ShowBu').show()
  90. })
  91. $('.clickShowBox_ShowBu').click(function(){
  92. $('.clickShowBox').fadeIn();
  93. $(this).hide();
  94. Show_imgs(num);
  95. })
  96. $('.clickShowBox .download').click(function(){
  97. BU_nomal($(this))
  98. const img = $('.clickShowBox .item img').eq(nowIndex);
  99. let src = img[0].src;
  100. if(img.attr('big_src')){
  101. src = img.attr('big_src');
  102. img[0].src = src;
  103. }
  104. let name = document.title + new Date().getTime() + src.match(/\.jpg|\.jpeg|\.webp|\.png/g)[0];
  105. if(img.attr('name')){
  106. name = img.attr('name');
  107. }
  108. BU_busy($(this))
  109. try{
  110. GM_download({
  111. url:src,
  112. name:name,
  113. onload:function(){
  114. BU_done($('.download'));
  115. },
  116. error:function(){
  117. BU_error($('.download'));
  118. }
  119. })
  120. }catch(error){
  121. console.log(error);
  122. BU_error($('.download'));
  123. }
  124. })
  125. $('.clickShowBox .downloadall').click(function(){
  126. BU_busy($(this));
  127. try{
  128. console.log(downloader)
  129. downloader.Download_img($('.clickShowBox .item img').clone());
  130. }catch(error){
  131. console.log(error);
  132. BU_error($(this));
  133. }
  134. })
  135. downloader.AllComplete(()=>{
  136. BU_done($('.clickShowBox .downloadall'));
  137. });
  138. downloader.OneSuccess((img)=>{
  139. $('.clickShowBox .item img').filter(function(){return $(this).attr('big_src')||$(this).attr('small_src') == img[0].src})
  140. .attr('src',img[0].src);
  141. });
  142. Add_ClickShowBox_css();
  143. $('.clickShowBox').hide();
  144. Add_keyControl()
  145. return box;
  146. }
  147. function Add_ClickShowBox_css(){
  148. let css = `
  149. .clickShowBox{
  150. width: 100%;
  151. height: 100%;
  152. background-color: #2d2d2d;
  153. overflow: hidden;
  154. border-radius: 0vw;
  155. position: fixed;
  156. z-index: 9999;
  157. }
  158. .clickShowBox .item{
  159. width: 100%;
  160. height: 100%;
  161. background-color: #2D2D2D;
  162. display: flex;
  163. align-items: center;
  164. justify-content: center;
  165. }
  166. .clickShowBox .item img{
  167. max-width: 100%;
  168. height: auto;
  169. max-height: 100%;
  170. }
  171. .clickShowBox .pages{
  172. font-size: 5vw;
  173. color: rgba(255,255,255,0.5);
  174. position: fixed;
  175. top: 1.5vw;
  176. margin: 2vw;
  177. right:12vw
  178. }
  179. .clickShowBox .close{
  180. width: 10vw;
  181. height:10vw;
  182. font-size: 6vw;
  183. border-radius: 10vw;
  184. background-color: rgba(255,255,255,0.1);
  185. color: rgba(255,255,255,0.1);
  186. position: fixed;
  187. right: 0;
  188. top:0;
  189. margin: 2vw;
  190. font-weight: bold;
  191. border: none;
  192. }
  193. .clickShowBox .close:active{
  194. filter:invert(100%);
  195. }
  196. .clickShowBox .downloadBU{
  197. display: flex;
  198. flex-direction: row;
  199. position: fixed;
  200. bottom:0;
  201. }
  202. .clickShowBox .download
  203. ,.clickShowBox .downloadall{
  204. width:100%;
  205. font-size: 5vmin;
  206. aspect-ratio: 1/1;
  207. border-radius: 2vmin;
  208. background-color: #ff8a17;
  209. color: white;
  210. margin: 0 0 2vw 2vw;
  211. border: none;
  212. opacity: .4;
  213. position: relative;
  214. }
  215. .clickShowBox .download:active
  216. ,.clickShowBox .downloadall:active{
  217. opacity: .6;
  218. }
  219. .clickShowBox .busy{
  220. animation: BU_busy infinite 1s linear;
  221. }
  222. @keyframes BU_busy{
  223. 0%{top:0}
  224. 25%{top:2vw}
  225. 75%{top:-2vw}
  226. 100%{top:0}
  227. }
  228. .clickShowBox .error{
  229. background-color: red;
  230. }
  231. .clickShowBox_ShowBu{
  232. width: 10vw;
  233. height: 10vw;
  234. border-radius: 10vw;
  235. background-color: orange;
  236. position: fixed;
  237. bottom: 30%;
  238. right: -5vw;
  239. z-index: 999999;
  240. display: flex;
  241. align-items: center;
  242. justify-content: center;
  243. }
  244. .clickShowBox_ShowBu::after{
  245. content: "";
  246. width: 70%;
  247. height: 70%;
  248. background-image: url('data:image/svg+xml;utf8,<svg width="10" height="10" xmlns="http://www.w3.org/2000/svg"><path d="M 10 10 L 0 5 L 10 0 Z" fill="White"/></svg>');
  249. background-size: cover;
  250. background-repeat: no-repeat;
  251. transform: scaleX(0.8);
  252. }
  253. `
  254. Add_css(css)
  255. }
  256. function BU_busy(bu){
  257. bu.addClass('busy');
  258. }
  259. function BU_done(bu){
  260. bu.removeClass('busy');
  261. }
  262. function BU_error(bu){
  263. bu.removeClass('busy');
  264. bu.addClass('error');
  265. }
  266. function BU_nomal(bu){
  267. bu.removeClass('busy').removeClass('error');
  268. }
  269. function Add_keyControl(){
  270. let downItem = $('<button><button>').click(function(){
  271. simulateClick($(window).width()/2,$(window).height()*0.8);
  272. })
  273. let upItem = $('<button><button>').click(function(){
  274. simulateClick($(window).width()/2,$(window).height()*0.2);
  275. })
  276. window.GAIL.AddKeyControl(downItem,upItem,null,null,true);
  277. }
  278. function ClickShowNext({img,onlyDown}){
  279. if(!$){return;}
  280. if(img.length<=1){console.log('only one img');return;}
  281. let item = $('.clickShowBox .item');
  282. $('.clickShowBox .pages').text(1+"/"+item.length);
  283. item.on("touchstart mousedown",function(event){
  284. if ($(window).height()>$(window).width() && event.type === 'mousedown'){return;}
  285. let y = event.clientY;
  286. if(event.touches){y = event.touches[0].clientY;}
  287. let index = item.index($(this));
  288. index = !onlyDown && y<$(this).height()/2 ? index-1:index+1;
  289. index = index>=0?Math.min(index,item.length-1):0
  290. item.eq(index)[0].scrollIntoView();
  291. $('.clickShowBox .pages').text((index+1)+"/"+item.length);
  292. nowIndex = index;
  293. Show_imgs(showNum);
  294. });
  295. }
  296. function Show_imgs(i){
  297. let img = $('.clickShowBox .item img[small_src][src=""]');
  298. let start = Math.max(0,nowIndex-i);
  299. let end = Math.min(img.length,nowIndex+i);
  300. img.slice(start,end).each(function(){
  301. this.src = $(this).attr('small_src');
  302. });
  303. console.log(`${start} ${end} ${img.length}`)
  304. }
  305. function Add_css(cssString){
  306. var style = document.createElement('style');
  307. style.type = 'text/css';
  308. style.innerHTML = cssString;
  309. document.body.appendChild(style);
  310. }
  311. }
  312. window.ShowBox = ShowBox;
  313. window.ShowBox.ShowInNewPage = (url)=>{
  314. GM_setValue('ShowBoxInNewPage','yes');
  315. window.open(url);
  316. const img = $('.clickShowBox img');
  317. GM_setValue('ShowBoxInNewPage_img',img.eq(0)[0].src);
  318. GM_setValue('ShowBoxInNewPage_num',img.length);
  319. let i = 1;
  320. let obo = setInterval(()=>{
  321. if(i==img.length){clearInterval(obo);return;}
  322. if(!GM_getValue('ShowBoxInNewPage_img')){
  323. GM_setValue('ShowBoxInNewPage_img',img.eq(++i).html());
  324. }
  325. },100);
  326. }
  327. window.ShowBox.Linsening_ShowInNewPage = ()=>{
  328. if(!GM_getValue('ShowBoxInNewPage')){
  329. return;
  330. }
  331. GM_deleteValue('ShowBoxInNewPage');
  332. const box = new window.ShowBox();
  333. let i = 0;
  334. let img = ''
  335. const num = Number(GM_getValue('ShowBoxInNewPage_num'));
  336. let obo = setInterval(()=>{
  337. if(i==num){GM_deleteValue('ShowBoxInNewPage_num');box.AddImgs($(img));clearInterval(obo);return;}
  338. if(GM_getValue('ShowBoxInNewPage_img')){
  339. img += GM_getValue('ShowBoxInNewPage_img')
  340. GM_deleteValue('ShowBoxInNewPage_img');
  341. }
  342. },100);
  343. }
  344. $(function(){
  345. window.ShowBox.Linsening_ShowInNewPage();
  346. })