// ==UserScript== // @name 森空岛图片下载器 // @namespace https://greasyfork.org/zh-CN/users/1002415-%E5%B0%8F%E6%97%A6 // @version 1.0.0 // @description 在文章详情页右上添加一个下载按钮,用于下载所有swiper-item类下的图片(除首图),若无swiper-item则下载特定来源的webp图片 // @author 小旦 // @match https://www.skland.com/article?id=* // @grant GM_download // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 添加下载按钮 const downloadButton = document.createElement('button'); downloadButton.textContent = '下载原图'; downloadButton.style.position = 'fixed'; downloadButton.style.right = '50px'; downloadButton.style.top = '11vh'; downloadButton.style.zIndex = 9999; downloadButton.style.padding = '25px 10px'; downloadButton.style.backgroundColor = 'rgb(55, 55, 55)'; downloadButton.style.border = '5px solid rgb(200, 235, 33)'; downloadButton.style.borderRadius = '37px'; downloadButton.style.marginTop = '-2px'; downloadButton.style.color = 'rgb(255, 255, 255)'; downloadButton.style.fontSize = '12px'; downloadButton.style.lineHeight = '16px'; downloadButton.style.fontWeight = '700'; downloadButton.style.fontFamily = 'akrobat'; downloadButton.style.cursor = 'pointer'; document.body.appendChild(downloadButton); // 绑定点击事件到下载按钮 downloadButton.addEventListener('click', function() { downloadImages(); }); // 下载图片函数 function downloadImages() { // 尝试获取所有 swiper-item 类的 div 元素 const containers = document.querySelectorAll('.swiper-item'); if (!containers.length) { // 如果没有找到 .swiper-item 则尝试下载特定来源的 webp 图片 const imgs = document.querySelectorAll('img[src^="https://bbs.hycdn.cn/image/"][src$=".webp"]'); if (!imgs.length) { console.error('未找到包含指定来源的 webp 图片'); return; } const now = new Date(); const timestamp = now.toISOString().slice(0, 19).replace(/[-:T]/g, ''); let count = 1; imgs.forEach(img => { handleImageDownload(img, timestamp, count++); }); } else { // 如果找到了 .swiper-item,则按原逻辑下载图片 const now = new Date(); const timestamp = now.toISOString().slice(0, 19).replace(/[-:T]/g, ''); let count = 1; containers.forEach((container, index) => { if (index === 0) { const firstImage = container.querySelector('img'); if (firstImage) { const images = Array.from(container.querySelectorAll('img')).slice(1); // 跳过第一个 images.forEach(img => { handleImageDownload(img, timestamp, count++); }); } else { console.error('未找到首张图片'); } } else { container.querySelectorAll('img').forEach(img => { handleImageDownload(img, timestamp, count++); }); } }); } } // 处理单个图片下载 function handleImageDownload(img, timestamp, count) { const src = img.src; const filename = `${timestamp}_${count}.webp`; console.log(`Downloading image: ${filename}`); GM_download({ url: src, name: filename, onload: function(response) { console.log(`Downloaded: ${filename}`); }, onerror: function(error) { console.error(`Failed to download: ${filename}, error: ${error}`); } }); } })();