Get_all_img_Library

添加了downloader和Add_css;

Od 28.10.2024.. Pogledajte najnovija verzija.

Ovu skriptu ne treba izravno instalirati. To je biblioteka za druge skripte koje se uključuju u meta direktivu // @require https://update.sleazyfork.org/scripts/480132/1473184/Get_all_img_Library.js

  1. // ==UserScript==
  2. // @name Get_all_img_Library
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024.10.28
  5. // @description 添加了downloader和Add_css;
  6. // @author You
  7. // @grant none
  8. // ==/UserScript==
  9. function get_img_obo_sessionStorage(key,nums,geturl,getimg,putimg){
  10. if(window.location.href.indexOf(key)<0){return}
  11. var i = 2
  12. if(sessionStorage.num){i = Number(sessionStorage.num)}
  13. var oldimg = $(sessionStorage.img)
  14. before_reflash(i,oldimg,putimg)
  15. var state = sessionStorage.state
  16. if(state!='start'){putimg($(sessionStorage.img),0);sessionStorage.clear();return}
  17. if(i<=nums){
  18. var href = geturl(i)
  19. if(!href){putimg($(sessionStorage.img),0);sessionStorage.clear();return}
  20. $.ajax({
  21. url:href,
  22. success:function(){
  23. var img = getimg('')
  24. if(sessionStorage.img==undefined){sessionStorage.img=""}
  25. sessionStorage.img += $('<div></div>').append(img.clone()).html()
  26. window.location.href = href
  27. i+=1
  28. sessionStorage.num = i
  29. },
  30. error:function(){
  31. alert(href+' error')
  32. putimg($(sessionStorage.img),0)
  33. sessionStorage.clear()
  34. },
  35. })
  36. }else{
  37. putimg($(sessionStorage.img),0)
  38. sessionStorage.clear()
  39. }
  40. }
  41. function before_reflash(i,oldimg,putimg){
  42. if(sessionStorage.reflash){putimg($(sessionStorage.img),0)}
  43.  
  44. console.log('before_reflash')
  45. window.onbeforeunload = function(){
  46. if(!oldimg){return}
  47. if(sessionStorage.state){return}
  48. sessionStorage.img = $('<div></div>').append(oldimg).html()
  49. sessionStorage.reflash = 'reflash'
  50. sessionStorage.num = i
  51. }
  52. }
  53. function get_img_obo_ajax_href(key,nums,geturl,getimg,putimg){
  54. var obo = function(i){
  55. var href = geturl(i)
  56. console.log(href)
  57. if(!href){return}
  58. $.ajax({
  59. url:href,
  60. success:function(data){
  61. var img = getimg(data)
  62. putimg(img,i)
  63. showmass(i+'/'+nums)
  64. i+=1
  65. if(i>=nums){return}
  66. obo(i)
  67. },
  68. error:function(){
  69. console.log('error: '+href)
  70. i+=1
  71. if(i>=nums){return}
  72. obo(i)
  73. }
  74. })
  75. };obo(0)
  76. }
  77. function new_bottom_bu(text){
  78. var bu = $('<button></button>').text(text).css({
  79. position:'fixed',
  80. bottom:0,
  81. width:'100vw',
  82. height:'10vh',
  83. 'font-size':'5vh'
  84. })
  85. return bu
  86. }
  87. function obo_sessionStorage_start_bu(){
  88. var bu = new_bottom_bu("start")
  89. bu.click(function(){
  90. if(sessionStorage.state!='start'){
  91. sessionStorage.clear()
  92. sessionStorage.setItem('state','start')
  93. location.reload()
  94. }else{
  95. sessionStorage.clear()
  96. }
  97. })
  98. return bu
  99. }
  100. function showmass(ms) {
  101. if($('.mass_top').attr('class')){$('.mass_top').text(ms);return}
  102. $('body').append($('<div class="mass_top"></div>').css({
  103. 'font-size':'2vw',
  104. 'color':'rgba(0, 102, 0, 0.5)',
  105. 'position':'fixed',
  106. 'top':'10px',
  107. 'left':'10px',
  108. 'font-weight':'bold',
  109. 'z-index':99999999,
  110. }).click(function(){$(this).hide()}))
  111. $('.mass_top').text(ms)
  112. }
  113. function add_css(tag,object){
  114. var style = $('<style></style>').text(tag+JSON.stringify(object).replace(/\"/g,'').replace(/,/g,";"))
  115. $('body').append(style)
  116. }
  117. function remove_sameimg(){
  118. var hs = new Array()
  119. $('img').each(function(){
  120. if(hs.indexOf(this.src)<0){
  121. hs.push(this.src)
  122. }else{
  123. $(this).remove()
  124. }
  125. })
  126. }
  127. function reflash_unloadimg(){
  128. var check = setInterval(function(){
  129. if(sessionStorage.reflashlock == 'yes'){return}
  130. var img = $('img:visible').filter(function(){return this.naturalWidth==0})
  131. img.each(function(){
  132. if(this.naturalWidth==0){
  133. //console.log(this.src)
  134. var clone = $(this).clone(true)
  135. var parent = $(this).parent()
  136. var pre = $(this).prev()
  137. var next = $(this).next()
  138. $(this).remove()
  139. setTimeout(function(){
  140. if(pre[0]){pre.after(clone);return}
  141. if(next[0]){next.before(clone);return}
  142. if(parent[0]){parent.append(clone);return}
  143. },500)
  144. }
  145. })
  146. },1000)
  147. }
  148.  
  149. let addedKeyControl = false;
  150. function AddKeyControl(downItem,upItem,leftItem,rightItem,closew){
  151. if(addedKeyControl){return;}else{addedKeyControl = true;}
  152. if(!downItem){
  153. downItem = $('<a></a>').click(function(){
  154. // 获取当前窗口的滚动位置
  155. var currentScroll = document.documentElement.scrollTop;
  156. // 设置每次 PageDown 后滚动的距离,可以根据需要调整
  157. var scrollDistance = $(window).height()/2;
  158. console.log(currentScroll);
  159. // 使用 scrollTo 方法将窗口滚动到当前位置加上设定的滚动距离
  160. window.scrollTo(0,currentScroll + scrollDistance);
  161. })
  162. }
  163. if(!upItem){
  164. upItem = $('<a></a>').click(function(){
  165. // 获取当前窗口的滚动位置
  166. var currentScroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
  167. // 设置每次 PageDown 后滚动的距离,可以根据需要调整
  168. var scrollDistance = window.innerHeight/2;
  169. // 使用 scrollTo 方法将窗口滚动到当前位置加上设定的滚动距离
  170. window.scrollTo(0,currentScroll - scrollDistance);
  171. })
  172. }
  173. document.addEventListener('keydown', function(event) {
  174. if (event.key === 'ArrowLeft' && leftItem && leftItem.length>0) {
  175. // 用户按下了左箭头键
  176. leftItem[0].click();
  177. console.log('Left arrow key pressed');
  178. // 执行左方向键对应的操作
  179. } else if (event.key === 'ArrowRight' && rightItem && rightItem.length>0 ) {
  180. // 用户按下了右箭头键
  181. rightItem[0].click();
  182. console.log('Right arrow key pressed');
  183. // 执行右方向键对应的操作
  184. } else if (event.key === 'ArrowUp' && upItem && upItem.length>0) {
  185. // 用户按下了上箭头键
  186. upItem[0].click();
  187. console.log('Up arrow key pressed');
  188. // 执行上方向键对应的操作
  189. } else if (event.key === 'ArrowDown' && downItem && downItem.length>0) {
  190. // 用户按下了下箭头键
  191. downItem[0].click();
  192. console.log('Down arrow key pressed');
  193. // 执行下方向键对应的操作
  194. } else if (event.key === '0' && closew) {
  195. // 用户按下了下箭头键
  196. window.close();
  197. // 执行下方向键对应的操作
  198. }
  199. });
  200. }
  201. async function hold_and_zoom(img,name,GetBigImg){
  202. if(!$('.holdbox:first')[0]){
  203. $('body').append($('<div class="holdbox"></div>').css({
  204. 'z-index':'9999999',
  205. width:'100vw',
  206. height:'100vh',
  207. position:'fixed',
  208. top:0,
  209. left:0,
  210. display:'flex',
  211. 'justify-content':'center',
  212. }))
  213. $('.holdbox').hide()
  214. $('.holdbox').on('mousedown',function(e){
  215. if(e.button!=0){return}
  216. if(showimg){clearTimeout(showimg)}
  217. if(showimg){showimg = null}
  218. $('.holdbox:first').hide()
  219. }).on('mousewheel',function(){
  220. if(showimg){clearTimeout(showimg)}
  221. if(showimg){showimg = null}
  222. $('.holdbox:first').hide()
  223. })
  224. }
  225. var showimg = null
  226. var state = 'mouseout'
  227. img.on('mouseout',function(){
  228. state = 'mouseout'
  229. })
  230. let getting = false;
  231. img.on('mouseover',async function(event){
  232. state = 'mousemove'
  233. if(showimg){clearTimeout(showimg)}
  234. if(showimg){showimg = null}
  235. let _img = $(this);
  236. let iimg = $(this).clone();
  237.  
  238. showimg = setTimeout(function(){
  239. if(state=='mouseout'){return}
  240. if(GetBigImg && img.attr('big-src')==null && !getting){
  241. new Promise((resolve,reject)=>{
  242. getting = true;
  243. GetBigImg(function(src){
  244. if(!src || src==""){console.log('src error');resolve()}else{
  245. getting = false;
  246. _img.attr('big-src',src).attr('src',src);
  247. let mass = $('<p>loading...</p>');
  248. _img.after(mass);
  249. let ioo = new Image();
  250. ioo.onload = function(){
  251. iimg.attr('src',src);
  252. console.log('src success');
  253. mass.remove();
  254. resolve();
  255. }
  256. ioo.src = src;
  257. }
  258. })
  259. })
  260. }
  261. $('.holdbox img').remove()
  262. $('.holdbox:first').append(iimg.css({
  263. width:'auto',
  264. height:'100vh',
  265. }).attr('name',name(_img)).on('mousedown',function(){
  266. var src = this.src
  267. var n = $(this).attr('name')
  268. if($(this).attr('big-src')){
  269. src = $(this).attr('big-src')
  270. }
  271. GM_download({
  272. url:src,
  273. name:n,
  274. })
  275. }))
  276. console.log('show');
  277. $('.holdbox').fadeIn(200)
  278. },500)
  279. })
  280. }
  281. function Add_css(cssString){
  282. var style = document.createElement('style');
  283. style.type = 'text/css';
  284. style.innerHTML = cssString;
  285. document.body.appendChild(style);
  286. }
  287.  
  288. class Downloader{
  289. constructor(){
  290. this.downloadType = "";
  291. this.downloaded = [];
  292. this.downloading = 0;
  293. this.downloadError = [];
  294. this.imgs = "";
  295. this.maxDownloadingCounts = 10;
  296. this.timeout = null;
  297. this.AllComplete = null;
  298. this.OneSuccess = null;
  299. this.OneError = null;
  300. }
  301. async Download_img(imgs){
  302. let self = this;
  303. if(this.downloadType==""){
  304. await this.Test_downloadType(imgs.eq(0));
  305. }
  306. this.Set_download(imgs)
  307. if(this.downloadType=="GM_download"){
  308. this.Donwload_img_by_GM();
  309. }else if(this.downloadType=="atag"){
  310. this.Donwload_img_by_atag();
  311. }else if(this.downloadType=="blob"){
  312. this.Donwload_img_by_blob();
  313. }
  314. }
  315. async Test_downloadType(img){
  316. return new Promise((resolve)=>{
  317. let timeout = 3000;
  318. let isOk = false;
  319. GM_download({
  320. url:img[0].src,
  321. name:"test.png",
  322. onload:()=>{isOk = true;this.downloadType = "GM_download";resolve()},
  323. })
  324. setTimeout(()=>{
  325. if(!isOk){this.downloadType = "atag";}
  326. resolve()
  327. },timeout)
  328. })
  329. }
  330. Set_download(imgs){
  331. this.downloaded = [];
  332. this.downloading = 0;
  333. this.downloadError = [];
  334. this.imgs = imgs;
  335. }
  336. Donwload_img_by_GM(){
  337. let self = this;
  338. async function Download_one(i){
  339. if(i>=self.imgs.length){if(self.AllComplete){self.AllComplete()};return;}
  340. if(self.downloading>=self.maxDownloadingCounts){setTimeout(()=>{Download_one(i)},1000);return;}
  341. let name = '';
  342. let src = '';
  343. self.downloading++;
  344. try{
  345. await self.Check_and_get_nameAndsrc({img:self.imgs.eq(i),checkSrc:true})
  346. .then((m)=>{
  347. name = m.name;
  348. src = m.src;
  349. });
  350. }catch(error){
  351. console.log(error)
  352. console.log('imgsrc is error:');
  353. self.downloadError.push(i);
  354. self.downloading--;
  355. Download_one(i+1);
  356. return;
  357. }
  358. console.log(name)
  359. console.log(document.title)
  360. let timeout = false;
  361. setTimeout(() => {timeout = true;},10000);
  362. const donwimg = self.imgs.eq(i);
  363. GM_download({
  364. url:src,
  365. name:name,
  366. onload:function(){
  367. self.downloaded.push(downimg);
  368. self.downloading--;
  369. if(window.GAIL.showmass){
  370. window.GAIL.showmass((self.downloaded.length+self.downloadError.length)+"/"+self.imgs.length);
  371. }
  372. self.imgs.eq(i).remove();
  373. if(self.OneSuccess){self.OneSuccess(downimg);}
  374. },
  375. onerror:function(){
  376. self.downloading--;
  377. self.downloadError.push(downimg);
  378. if(self.OnError){self.OnError(downimg);}
  379. },
  380. onprogress:function(){
  381. if(timeout){return false;}
  382. }
  383. });
  384. setTimeout(function() {Download_one(i+1);}, 10);
  385. }
  386. Download_one(0);
  387. }
  388. Check_and_get_nameAndsrc(args){
  389. let self = this;
  390. console.log(args);
  391. return new Promise(async (resolve,reject)=>{
  392. if(!args || !args.img){return reject();}
  393. let src = args.img.attr('big_src');
  394. if(!src){src = args.img.attr('src');}
  395. if(!src){reject();}
  396. console.log("check:"+src)
  397. if(args.checkSrc){
  398. try{
  399. await self.check_src_is_right(src);
  400. }catch(error){
  401. reject();
  402. }
  403. }
  404. let ext = src.match(/\.jpg|\.png|\.webp|\.gif|\.bmp/g);
  405. if(!ext){ext = '.png';}else{ext = ext[0];}
  406. let name = args.img.attr('name');
  407. if(!name){name = document.title + new Date().getTime() + ext;}
  408. resolve({name:name,src:src});
  409. });
  410. }
  411. Donwload_img_by_atag(){
  412. let self = this;
  413. let myWindow = window.open('_blank');
  414. async function Download_one(i){
  415. if(i>=imgs.length){myWindow.alert('end');myWindow.history.go(0-myWindow.history.length+1);myWindow.close();return;}
  416. let name = '';
  417. let src = '';
  418. self.downloading++;
  419. try{
  420. await self.Check_and_get_nameAndsrc({img:self.imgs.eq(i),checkSrc:true})
  421. .then((m)=>{
  422. name = m.name;
  423. src = m.src;
  424. });
  425. }catch(error){
  426. console.log('imgsrc is error:');
  427. self.downloadError.push(i);
  428. self.downloading--;
  429. Download_one(i+1);
  430. return;
  431. }
  432. if(!myWindow){alert('windows is closed');return;}
  433. myWindow.location.href = src;
  434. let a = $('<a></a>').attr({
  435. 'href':$(myWindow.document.body).find('img:first').attr('src'),
  436. 'download':name,
  437. })
  438. a[0].click();
  439. setTimeout(function() {Download_one(i+1);self.imgs.eq(i).remove()}, 1000);
  440. self.downloaded.push(i);
  441. self.downloading--;
  442. }
  443. Download_one(0);
  444. }
  445. check_src_is_right(src){
  446. let self = this;
  447. return new Promise((resolve,reject)=>{
  448. let iimg = new Image();
  449. iimg.onload = function(){
  450. if(this.width*this.height*this.naturalWidth*this.naturalHeight==0){reject();}else{resolve();}
  451. }
  452. iimg.onerror = function(){reject();}
  453. iimg.src = src;
  454. setTimeout(function() {iimg.abort();reject();}, 2000);
  455. })
  456. }
  457. Donwload_img_by_blob(){
  458. if(!this.imgs || this.imgs.length==0){return;}
  459. const _this = this;
  460. const imgs = this.imgs;
  461. const obo = (i)=>{
  462. if(i>=imgs.length){return;}
  463. if(_this.maxDownloadingCounts>1 && _this.downloading >= _this.maxDownloadingCounts){
  464. setTimeout(function() {obo(i)}, 1000);
  465. }
  466. let src = imgs.eq(i).attr('big_src');
  467. if(!src){src = imgs.eq(i).attr('src');}
  468. const checkimg = imgs.eq(i);
  469. _this.downloading++;
  470. _this.UrlToBlob({url:src,timeout:_this.timeout})
  471. .then(blob=>{
  472. _this.Check_and_get_nameAndsrc({img:checkimg}).then(args=>{
  473. const name = args.name;
  474. let a = $('<a></a>').attr({
  475. download:name,
  476. href:blob
  477. })
  478. a[0].click();
  479. _this.downloaded.push(checkimg);
  480. _this.downloading--;
  481. checkimg.attr('src',blob);
  482. OneSuccess?.(checkimg);
  483. })
  484. if(_this.maxDownloadingCounts==1){
  485. obo(++i);
  486. }else{
  487. window.GAIL.showmass((_this.downloaded.length+_this.downloadError.length)+"/"+_this.imgs.length);
  488. }
  489. })
  490. .catch(er=>{
  491. console.log(er);
  492. _this.downloadError.push(imgs.eq(i));
  493. _this.downloading--;
  494. if(_this.maxDownloadingCounts==1){
  495. obo(++i);
  496. }else{
  497. window.GAIL.showmass((_this.downloaded.length+_this.downloadError.length)+"/"+_this.imgs.length);
  498. }
  499. });
  500. if(_this.maxDownloadingCounts>1){
  501. setTimeout(function() {obo(++i);}, 10);
  502. }
  503. }
  504. obo(0);
  505. }
  506. /*
  507. args = {url:url,timeout:timeout}
  508. */
  509. async UrlToBlob(args) {
  510. let _this = this;
  511. return new Promise((resolve,reject)=>{
  512. if(!args.url){reject("no url");}
  513. if(args.timeout){
  514. const timeout = setTimeout(function() {reject("fetch timeout")}, args.timeout);
  515. }
  516. fetch(args.url)
  517. .then(response => {
  518. const contentLength = response.headers.get('Content-Length');
  519. const total = parseInt(contentLength, 10);
  520. let loaded = 0;
  521. // 克隆响应以便分别读取流和获得 Blob
  522. const clonedResponse = response.clone();
  523. const reader = clonedResponse.body.getReader();
  524. // 更新进度的函数
  525. function updateProgress({ done, value }) {
  526. if (done) {
  527. return; // 如果读取完毕,直接返回
  528. }
  529. loaded += value.byteLength; // 累加已加载字节
  530. const progress = (loaded / total) * 100; // 计算进度百分比
  531. console.log(`Loading: ${progress.toFixed(2)}%`);
  532. _this.FetchShowProgress?.(progress);
  533. // 继续读取下一块数据
  534. return reader.read().then(updateProgress);
  535. }
  536. // 开始读取流以更新进度
  537. return reader.read().then(updateProgress).then(() => {
  538. // 完成后返回原始响应的 Blob
  539. return response.blob();
  540. });
  541. })
  542. .then(blob => {
  543. const blobUrl = URL.createObjectURL(blob);
  544. resolve(blobUrl);
  545. })
  546. .catch(error => {
  547. console.error('Error caching video:', error);
  548. reject(error);
  549. });
  550. });
  551. }
  552. FetchShowProgress(pro){
  553. if(this.maxDownloadingCounts==1 && this.imgs.length==1){
  554. window.GAIL.showmass(pro);
  555. $(".mass_top").css('font-size',"10vw");
  556. }
  557. }
  558. }
  559.  
  560. window.GAIL = {
  561. get_img_obo_sessionStorage : get_img_obo_sessionStorage,
  562. get_img_obo_ajax_href : get_img_obo_ajax_href,
  563. new_bottom_bu : new_bottom_bu,
  564. obo_sessionStorage_start_bu : obo_sessionStorage_start_bu,
  565. showmass : showmass,
  566. add_css : add_css,
  567. remove_sameimg : remove_sameimg,
  568. reflash_unloadimg : reflash_unloadimg,
  569. AddKeyControl : AddKeyControl,
  570. hold_and_zoom : hold_and_zoom,
  571. Add_css:Add_css,
  572. Downloader:Downloader,
  573. }