// ==UserScript== // @name 压缩图免登录下载压缩后的图片 // @namespace https://www.yasuotu.com // @version v1.0.0 // @description yasuotu.com 压缩图片后免登录批量下载压缩后的图片 // @author kayking // @match https://www.yasuotu.com/* // @icon https://img.yasuotu.com/yasuotu/images/logo.png // @grant none // @require https://fastly.jsdelivr.net/npm/qmsg@1.7.2/dist/index.umd.min.js // @license MIT // ==/UserScript== /* global Qmsg */ (function() { 'use strict'; // 加载上次保存的位置 let pos = { top: 200, left: 0 }; const style = document.createElement('style'); style.textContent=` #tm-drag-btn { position: fixed; width: 60px; height: 40px; background: #2563eb; color: white; text-align: center; line-height:40px; border-radius: 0 8px 8px 0; cursor: pointer; z-index: 9999999; user-select: none; box-shadow: 0 2px 10px #0004; display: flex; align-items: center; justify-content: center; font-size: 16px; font-weight: bold; } #tm-drag-btn:hover { background: #1d4ed8; } `; document.head.appendChild(style); // 按钮HTML const btn = document.createElement('div'); btn.id = "tm-drag-btn"; btn.innerText = "下载"; btn.style.top = pos.top + "px"; btn.style.left = pos.left + "px"; document.body.appendChild(btn); // 按钮点击事件(你在这里写功能代码) btn.onclick = function(e){ const saveEles = Array.from(document.getElementsByClassName('successInfo')); if (!saveEles.length) { Qmsg.info('未获取到压缩后的图片'); return; } // 获取基础文件名(外层文件名,共用前缀) const baseFileName = document.querySelector('.text-c.mgbt6.lg24')?.textContent || '图片'; // 遍历每一项 successInfo saveEles.forEach((item, index) => { // 取出图片url,增加容错判断,防止DOM层级变化报错 const imgNode = item.lastChild?.firstChild; if (!imgNode) { console.warn(`第${index+1}项:找不到图片节点`); return; } const ysImgUrl = imgNode.getAttribute('url'); if (!ysImgUrl) { console.warn(`第${index+1}项:图片url为空`); return; } console.log(`第${index+1}项 ysImgUrl:`, ysImgUrl); // 拼接文件名,多个图片自动加序号区分 const fileName = saveEles.length > 1 ? `${baseFileName}_${index+1}` : baseFileName; Qmsg.success('下载任务已触发,请稍等...'); // 执行下载函数 downloadFile(ysImgUrl, fileName); }); }; function downloadFile(url, name) { const a = document.createElement('a'); a.href = url; a.download = name; // 下载保存的文件名 document.body.appendChild(a); a.click(); a.remove(); } })();