Get_all_img_Library

Example library script for use with @require

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.sleazyfork.org/scripts/480132/1534996/Get_all_img_Library.js

  1. // ==UserScript==
  2. // @name Get_all_img_Library
  3. // @namespace http://tampermonkey.net/
  4. // @version 2025.02.11.01
  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. console.log('before_reflash')
  44. window.onbeforeunload = function(){
  45. if(!oldimg){return}
  46. if(sessionStorage.state){return}
  47. sessionStorage.img = $('<div></div>').append(oldimg).html()
  48. sessionStorage.reflash = 'reflash'
  49. sessionStorage.num = i
  50. }
  51. }
  52. function get_img_obo_ajax_href(key,nums,geturl,getimg,putimg){
  53. var obo = function(i){
  54. var href = geturl(i)
  55. console.log(href)
  56. if(!href){return}
  57. $.ajax({
  58. url:href,
  59. success:function(data){
  60. var img = getimg(data)
  61. putimg(img,i)
  62. showmass(i+'/'+nums)
  63. i+=1
  64. if(i>=nums){return}
  65. obo(i)
  66. },
  67. error:function(){
  68. console.log('error: '+href)
  69. i+=1
  70. if(i>=nums){return}
  71. obo(i)
  72. }
  73. })
  74. };obo(0)
  75. }
  76. function new_bottom_bu(text){
  77. var bu = $('<button></button>').text(text).css({
  78. position:'fixed',
  79. bottom:0,
  80. width:'100vw',
  81. height:'10vh',
  82. 'font-size':'5vh'
  83. })
  84. return bu
  85. }
  86. function obo_sessionStorage_start_bu(){
  87. var bu = new_bottom_bu("start")
  88. bu.click(function(){
  89. if(sessionStorage.state!='start'){
  90. sessionStorage.clear()
  91. sessionStorage.setItem('state','start')
  92. location.reload()
  93. }else{
  94. sessionStorage.clear()
  95. }
  96. })
  97. return bu
  98. }
  99. function showmass(ms) {
  100. if($('.mass_top').attr('class')){$('.mass_top').text(ms);return}
  101. $('body').append($('<div class="mass_top"></div>').css({
  102. 'font-size':'2vw',
  103. 'color':'rgba(0, 102, 0, 0.5)',
  104. 'position':'fixed',
  105. 'top':'10px',
  106. 'left':'10px',
  107. 'font-weight':'bold',
  108. 'z-index':99999999,
  109. }).click(function(){$(this).hide()}))
  110. $('.mass_top').text(ms)
  111. }
  112. function add_css(tag,object){
  113. var style = $('<style></style>').text(tag+JSON.stringify(object).replace(/\"/g,'').replace(/,/g,";"))
  114. $('body').append(style)
  115. }
  116. function remove_sameimg(){
  117. var hs = new Array()
  118. $('img').each(function(){
  119. if(hs.indexOf(this.src)<0){
  120. hs.push(this.src)
  121. }else{
  122. $(this).remove()
  123. }
  124. })
  125. }
  126. function reflash_unloadimg(){
  127. var check = setInterval(function(){
  128. if(sessionStorage.reflashlock == 'yes'){return}
  129. var img = $('img:visible').filter(function(){return this.naturalWidth==0})
  130. img.each(function(){
  131. if(this.naturalWidth==0){
  132. //console.log(this.src)
  133. var clone = $(this).clone(true)
  134. var parent = $(this).parent()
  135. var pre = $(this).prev()
  136. var next = $(this).next()
  137. $(this).remove()
  138. setTimeout(function(){
  139. if(pre[0]){pre.after(clone);return}
  140. if(next[0]){next.before(clone);return}
  141. if(parent[0]){parent.append(clone);return}
  142. },500)
  143. }
  144. })
  145. },1000)
  146. }
  147. let addedKeyControl = false;
  148. function AddKeyControl(downItem,upItem,leftItem,rightItem,closew){
  149. if(addedKeyControl){return;}else{addedKeyControl = true;}
  150. if(!downItem){
  151. downItem = $('<a></a>').click(function(){
  152. // 获取当前窗口的滚动位置
  153. var currentScroll = document.documentElement.scrollTop;
  154. // 设置每次 PageDown 后滚动的距离,可以根据需要调整
  155. var scrollDistance = $(window).height()/2;
  156. console.log(currentScroll);
  157. // 使用 scrollTo 方法将窗口滚动到当前位置加上设定的滚动距离
  158. window.scrollTo(0,currentScroll + scrollDistance);
  159. })
  160. }
  161. if(!upItem){
  162. upItem = $('<a></a>').click(function(){
  163. // 获取当前窗口的滚动位置
  164. var currentScroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
  165. // 设置每次 PageDown 后滚动的距离,可以根据需要调整
  166. var scrollDistance = window.innerHeight/2;
  167. // 使用 scrollTo 方法将窗口滚动到当前位置加上设定的滚动距离
  168. window.scrollTo(0,currentScroll - scrollDistance);
  169. })
  170. }
  171. document.addEventListener('keydown', function(event) {
  172. if (event.key === 'ArrowLeft' && leftItem && leftItem.length>0) {
  173. // 用户按下了左箭头键
  174. leftItem[0].click();
  175. console.log('Left arrow key pressed');
  176. // 执行左方向键对应的操作
  177. } else if (event.key === 'ArrowRight' && rightItem && rightItem.length>0 ) {
  178. // 用户按下了右箭头键
  179. rightItem[0].click();
  180. console.log('Right arrow key pressed');
  181. // 执行右方向键对应的操作
  182. } else if (event.key === 'ArrowUp' && upItem && upItem.length>0) {
  183. // 用户按下了上箭头键
  184. upItem[0].click();
  185. console.log('Up arrow key pressed');
  186. // 执行上方向键对应的操作
  187. } else if (event.key === 'ArrowDown' && downItem && downItem.length>0) {
  188. // 用户按下了下箭头键
  189. downItem[0].click();
  190. console.log('Down arrow key pressed');
  191. // 执行下方向键对应的操作
  192. } else if (event.key === '0' && closew) {
  193. // 用户按下了下箭头键
  194. window.close();
  195. // 执行下方向键对应的操作
  196. }
  197. });
  198. }
  199. async function hold_and_zoom(img,name,GetBigImg){
  200. if(!$('.holdbox:first')[0]){
  201. $('body').append($('<div class="holdbox"></div>').css({
  202. 'z-index':'9999999',
  203. width:'100vw',
  204. height:'100vh',
  205. position:'fixed',
  206. top:0,
  207. left:0,
  208. display:'flex',
  209. 'justify-content':'center',
  210. }))
  211. $('.holdbox').hide()
  212. $('.holdbox').on('mousedown',function(e){
  213. if(e.button!=0){return}
  214. if(showimg){clearTimeout(showimg)}
  215. if(showimg){showimg = null}
  216. $('.holdbox:first').hide()
  217. }).on('mousewheel',function(){
  218. if(showimg){clearTimeout(showimg)}
  219. if(showimg){showimg = null}
  220. $('.holdbox:first').hide()
  221. })
  222. }
  223. var showimg = null
  224. var state = 'mouseout'
  225. img.on('mouseout',function(){
  226. state = 'mouseout'
  227. })
  228. let getting = false;
  229. img.on('mouseover',async function(event){
  230. state = 'mousemove'
  231. if(showimg){clearTimeout(showimg)}
  232. if(showimg){showimg = null}
  233. let _img = $(this);
  234. let iimg = $(this).clone();
  235. showimg = setTimeout(function(){
  236. if(state=='mouseout'){return}
  237. if(GetBigImg && img.attr('big-src')==null && !getting){
  238. new Promise((resolve,reject)=>{
  239. getting = true;
  240. GetBigImg(function(src){
  241. if(!src || src==""){console.log('src error');resolve()}else{
  242. getting = false;
  243. _img.attr('big-src',src).attr('src',src);
  244. let mass = $('<p>loading...</p>');
  245. _img.after(mass);
  246. let ioo = new Image();
  247. ioo.onload = function(){
  248. iimg.attr('src',src);
  249. console.log('src success');
  250. mass.remove();
  251. resolve();
  252. }
  253. ioo.src = src;
  254. }
  255. })
  256. })
  257. }
  258. $('.holdbox img').remove()
  259. $('.holdbox:first').append(iimg.css({
  260. width:'auto',
  261. height:'100vh',
  262. }).attr('name',name(_img)).on('mousedown',function(){
  263. var src = this.src
  264. var n = $(this).attr('name')
  265. if($(this).attr('big-src')){
  266. src = $(this).attr('big-src')
  267. }
  268. GM_download({
  269. url:src,
  270. name:n,
  271. })
  272. }))
  273. console.log('show');
  274. $('.holdbox').fadeIn(200)
  275. },500)
  276. })
  277. }
  278. function Add_css(cssString){
  279. var style = document.createElement('style');
  280. style.type = 'text/css';
  281. style.innerHTML = cssString;
  282. document.body.appendChild(style);
  283. }
  284. /**
  285. * 通过iframe获取图片的类
  286. * @class
  287. * @param {string} key -跳过的条件
  288. * @param {number} nums -总页数
  289. * @param {function({index: number}):string} Geturl -获取每一页的地址的方法
  290. * @param {function({document: document}):JQuery} Getimg -获取每一页的图片的方法
  291. * @param {function({img: JQuery},{index:number})} Putimg -图片加载到主页面的方法
  292. * @example
  293. const key = "reader";
  294. const atag;
  295. const nums = atag.length;
  296. const Geturl = (i)=>{
  297. return atag.eq(i)[0].href;
  298. }
  299. const Getimg = (doc)=>{
  300. return $(doc).find('img');
  301. }
  302. const Putimg = (img,i)=>{
  303. atag.eq(i).after(img);
  304. }
  305. new Get_img_obo_iframe(key,nums,Geturl,Getimg,Putimg).Start()
  306. */
  307. function Get_img_obo_iframe(key,nums,Geturl,Getimg,Putimg){
  308. const gi = this;
  309. gi.Start = async()=>{
  310. for(let i=0;i<nums;i++){
  311. let url = await Geturl(i);
  312. //if(url.indexOf(key)>=0){continue;}
  313. let iframe;
  314. try {
  315. iframe = await gi.Add_iframe(url);
  316. } catch (error) {
  317. console.error('发生错误:', error); // 如果 Add_iframe 被 reject,错误会被捕获
  318. }
  319. let img;
  320. if(iframe){
  321. let My_getimg = ()=>{
  322. return Getimg(iframe.contentWindow.document);
  323. }
  324. await gi.WaitingElement(My_getimg).then(ele=>img = ele).catch(()=>img = $());
  325. await Putimg(img,i);
  326. gi.show_mass((i+1)+"/"+nums);
  327. $(iframe).remove();
  328. }
  329. }
  330. }
  331. gi.Add_iframe=(url)=>{
  332. return new Promise((resolve,reject)=>{
  333. // 创建一个iframe元素
  334. var iframe = document.createElement('iframe');
  335. // 设置iframe的宽度和高度
  336. iframe.width = '1px';
  337. iframe.height = '1px';
  338. // 设置iframe的src属性,指向你想要打开的URL
  339. iframe.src = url; // 请替换为你想要加载的URL
  340. // 将iframe添加到页面的body中
  341. document.body.appendChild(iframe);
  342. // 等待iframe加载完成后再操作它
  343. iframe.onload = function() {
  344. resolve(iframe);
  345. };
  346. iframe.onerror = function(){
  347. reject(url);
  348. }
  349. })
  350. }
  351. gi.timeout = 5000;
  352. gi.WaitingElement=(GetEle)=>{
  353. return new Promise((resolve,reject)=>{
  354. let ele = GetEle();
  355. if(ele.length>0){resolve(ele);}else{
  356. let timeout = gi.timeout;
  357. let check = setInterval(()=>{
  358. let ele = GetEle();
  359. if(ele.length>0){
  360. resolve(ele);
  361. clearInterval(check);
  362. }
  363. timeout -= 100;
  364. if(timeout<=0){reject();clearInterval(check);}
  365. },100)
  366. }
  367. })
  368. }
  369. }
  370. Get_img_obo_iframe.prototype.show_mass = (mass)=>{
  371. window.GAIL.showmass(mass);
  372. $('.mass_top').css('font-size','10vmin')
  373. }
  374. window.GAIL = {
  375. get_img_obo_sessionStorage : get_img_obo_sessionStorage,
  376. get_img_obo_ajax_href : get_img_obo_ajax_href,
  377. Get_img_obo_iframe : Get_img_obo_iframe,
  378. new_bottom_bu : new_bottom_bu,
  379. obo_sessionStorage_start_bu : obo_sessionStorage_start_bu,
  380. showmass : showmass,
  381. add_css : add_css,
  382. remove_sameimg : remove_sameimg,
  383. reflash_unloadimg : reflash_unloadimg,
  384. AddKeyControl : AddKeyControl,
  385. hold_and_zoom : hold_and_zoom,
  386. Add_css:Add_css,
  387. }