/* eslint-disable no-self-assign */ /* eslint-disable no-undef */ // @ts-nocheck /* eslint-disable no-unused-vars */ // ==UserScript== // @name TryGetAllPage // @namespace http://tampermonkey.net/ // @version 2026.6.17.1 // @description try to take over the world! // @author You // @include * // @require https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js // @grant GM_download // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // @grant GM_registerMenuCommand // @require https://scriptcat.org/lib/5415/2026.06.10.01/IframeManager.js?sha384-ugGvg/lqdGGbm9DDcbYtxMoZqHU2cck8s/cVCxplzRwdkkJgvz4gRgCWbRh3Bj8g // ==/UserScript== GM_registerMenuCommand('在当前网址和子网址运行', () => { scriptControler.add(window.location.href) window.location.reload() }); GM_registerMenuCommand('移除当前网址运行了', () => { scriptControler.remove(window.location.href) window.location.reload() }); $(function(){ if(!scriptControler.contains(window.location.href)){return;} const IM = new MyIM(); const TA = new TryGetAllPage(); class Enumerable{ constructor(){ this.TA = TA; } GetPage(){ TA.GetHrefs(); return this; } async forEach(callback){ while(true){ const href = TA.Pop(); if(href){ const rs = await callback(href); if(rs=== false) break; } else break; } } get length(){ return TA.hrefs.size; } toArray(){ return [...TA.hrefs]; } } const EM = new Enumerable(); IM.GetPage = ()=> { TA.GetHrefs(); return [...TA.hrefs]; }; }) class ScriptControler{ constructor(){ if(GM_getValue('TryGetAllPage')){ this.list = new Set(GM_getValue('TryGetAllPage').split(' ')) }else this.list = new Set(); } add(url){ this.list.add(new URL(url).origin); GM_setValue('TryGetAllPage',[...this.list].join(' ')) } contains(url){ try{ return this.list.has(new URL(url).origin); }catch{console.error(url)} } remove(url){ url = new URL(url).origin; this.list.delete(url); GM_setValue('TryGetAllPage',[...this.list].join(' ')) } } const scriptControler = new ScriptControler(); class Mutex { constructor() { this._locked = false; this._queue = []; } lock() { return new Promise(resolve => { if (!this._locked) { this._locked = true; resolve(); } else { this._queue.push(resolve); } }); } unlock() { if (this._queue.length) { const next = this._queue.shift(); next(); } else { this._locked = false; } } } const mutex = new Mutex(); async function MutexG(callback) { await mutex.lock(); await callback(); mutex.unlock(); } class TryGetAllPage{ constructor(arg) { this.hrefs = new Set(); } GetHrefs(){ const _this = this; const atag = $('a[href]').filter((index,a)=>{ const isNum = /^ ?\d+ ?$/.test($(a).text())|| $(a).text().match(/上一页|下一页/g); const isNotSelf = a.href != window.location.href || window.location.href.indexOf(a.href)<0; return isNum && isNotSelf; }); atag.each(function(){ _this.hrefs.add(this.href); }) //console.log(this.hrefs); } Pop(){ if(this.hrefs.size==0) return null; const [first] = this.hrefs; this.hrefs.delete(first);console.log(first); return first; } Add(href){ this.hrefs.add(href); } } class MyIM{ constructor(){ const _this = this; this.srcs = new Set(); this.hrefs = []; this.IM = new IframeManagerSimple(window.location.href); $('body').append(` `) $('.iframe-manager').off('click', '.item .retry'); $('.iframe-manager').on('click','.item .retry',function(event){ const iframe = $(event.target).nextAll("iframe"); if(event.target.classList.contains('fontRed')){ if(iframe.length==1) iframe[0].src = iframe[0].src; event.target.classList.remove('fontRed'); }else{ event.target.classList.add('fontRed'); if(iframe.length!=1) return; if(iframe[0].contentDocument.readyState != "complete") iframe[0].contentWindow.stop(); else _this.IM.OnLoaded(iframe[0],{gettingPage : true}); } }) let actionCount = 0; this.IM.IframeAction = function(url,args){console.log(`iframe action : ${url}`) if(args.gettingPage){ let hrefs = _this.GetPage(); return {srcs : _this.GetSrcs() , url : url , getedSrcs : true , hrefs:hrefs} }else if(args.showImg){ // await _this.Download(url); } } this.IM.GetInfoFromIframe = function(args){ if(args.getedSrcs && args.srcs){ $('.iframe-manager .container button.tool.bu1').css('--action',`"${++actionCount}"`); args.srcs.forEach(i=>_this.srcs.add(i)); $('button.tool.bu5').text(`img (${_this.srcs.size})`) } if(args.hrefs){ _this.AddHrefs(args.hrefs); } } let closeHS = new Set(); this.IM.Close = async function(url){ if(closeHS.has(url)) throw(url); else closeHS.add(url); this.private.count--; this.box.RemoveIframe(url); await MutexG(()=>_this.pageCount--);console.log(`page count ${_this.pageCount}`); } //修复原基类的bug--------------------------------------------------------------------- this.IM._ParentPageLinstener = function(){ const __this = this; const onFromIframe = function(data){ if(!data.ActionDone) return; if(data.info) __this.GetInfoFromIframe(info); if(__this.autoClose) __this.Close(data.ActionDone); } this.Post.onFromIframe = onFromIframe; } this.AddBU(); this.state = "click img download"; this.dcount = 0; this.pageCount = 0; this.maxPage = 2; this.stop = true; } AddBU(){ const _this = this; $(".iframe-manager .container").append(`
`) $('body').append(` `) let loadedPage = 0; $('button.tool.bu1').click(async function(){ if(!_this.stop){ _this.stop = true; $(this).removeClass('fontRed'); return; } _this.stop = false; $(this).addClass('fontRed'); if(_this.hrefs.length == 0) _this.hrefs = _this.GetPage(); if(_this.srcs.size == 0) _this.srcs = new Set(_this.GetSrcs()); for(let i = loadedPage;i<_this.hrefs.length;i++){ const h = _this.hrefs[i]; if(_this.stop) break; await _this.IM.Open(h,{gettingPage : true}); _this.CheckIframeComplete() $('button.tool.bu1').text(`${loadedPage+1} / ${_this.hrefs.length}`); MutexG(()=>_this.pageCount++); loadedPage++; await new Promise(rs=>{ const wait = function(){ if(_this.pageCount >= _this.maxPage){ setTimeout(wait,500); }else rs(); } wait(); }) if(loadedPage%5==0) break; } _this.stop = true; $(this).removeClass('fontRed'); }) $('button.tool.bu3').click(function(){ if(_this.state != "select del img") { _this.state = "select del img"; $(this).addClass('del'); }else{ _this.state = "click img download"; $(this).removeClass('del'); $('img.del').remove(); } $('button.tool.bu4').text(`Download (${$('img.showImg').length})`) }) let downloadedNum = 0; let downloadStop = true; $('button.tool.bu4').click(async function(){ if(!downloadStop){ downloadStop = true; $(this).removeClass('fontRed'); return; } downloadStop = false; $(this).addClass('fontRed'); const $img = $('img.showImg'); for(let i=0;i<$img.length;i++){ if(downloadStop) break; downloadedNum++; await _this.Download($img.eq(i)[0]); $('button.tool.bu4').text(`Download (${$('img.showImg').length})`); if(downloadedNum%10==0) break; } downloadStop = true; $(this).removeClass('fontRed'); }) let showImgNum = 0; $('button.tool.bu5').click(function(){ const srcs = [..._this.srcs] for(let i=showImgNum;i.to-home').click(function(){ $(this).parent()[0].scrollTo(0,0); }) } CheckIframeComplete(){ let i = 1; $('.iframe-manager iframe:visible').each(function(){ if(this.contentDocument.readyState=="complete"){ $(this).parent().find('.retry').addClass('Green'); setTimeout(()=>{$(this).parent().find('.retry').click();},i++*1000); } else $(this).parent().find('.retry').removeClass('Green'); }) } AddHrefs(arr){ const hrefs = new Set(this.hrefs); arr.forEach(a=>hrefs.add(a)); this.hrefs = [...hrefs]; } AddImg(src){ const _this = this; const $img = $('').attr('src',src) $('.iframe-manager .container').append($img); $img.click(async function(){ if(_this.state == "click img download"){ $(this).addClass('downloading'); await _this.Download(this); }else if(_this.state == "select del img"){ $(this).toggleClass('del'); } }) $img[0].onload = function(){ if(this.naturalWidth <= 100 || this.naturalHeight <= 100) $(this).remove(); else{ const ps = this.naturalWidth / this.naturalHeight; if(ps<0.1 || ps>10) $(this).remove(); else $(this).css("--size",this.naturalWidth + " x " + this.naturalHeight); } } $('button.tool.bu4').text(`Download (${$('img.showImg').length})`) } GetPage(){ } GetSrcs(){ const urls = new Set(); // 1. 获取 标签的 src 和 data-src $('*:not(a[href*="jpg|jpeg|png|wepb|gif"]) img').each(function(){ let data; for (const attr of this.attributes) { if (attr.name.includes('data')) { data = attr.value;//console.log(data); break; } } const src = data || this.src; if(src) urls.add(src); }); // 2. 获取所有元素的计算后背景图片 document.querySelectorAll('*').forEach(el => { const bg = window.getComputedStyle(el).backgroundImage; // backgroundImage 的格式是 url("http://...") 或 none if (bg && bg !== 'none') { // 提取 url("...") 中的 URL 部分 const match = bg.match(/url$["']?([^"')]+)["']?$/); if (match) urls.add(match); } }); $('a[href*="jpg|jpeg|png|wepb|gif"]').each(function(){ urls.add(this.href); }) // 去重 return [...urls]; } async Download(img){ const name = document.title + (this.dcount++) + ".png"; return new Promise(rs=>{ GM_download({ url:img.src, name:name, onload:()=>{ this.OnDownloaded(img); rs(); }, onerror:()=>{ this.OnDownloadError(img); rs(); } }) }) } OnDownloaded(img){ $(img).remove(); } OnDownloadError(img){ console.log(`download error: ${img.src}`); } }