MyShowBox

修复touch就滚两页的bug

当前为 2025-01-23 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/515677/1525705/MyShowBox.js

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