// ==UserScript==
// @name AuntMia Gallery Image Downloader
// @namespace http://tampermonkey.net/
// @version 0.2.0
// @description Add a download button to save images locally in AuntMia gallery slideshows
// @author 伟哥
// @match https://www.auntmia.com/gallery/*
// @grant GM_xmlhttpRequest
// @grant GM_download
// @connect pics.auntmia.com
// @connect img.doppiocdn.com
// @connect *
// ==/UserScript==
(function() {
'use strict';
let downloadBtn = null;
let currentImageUrl = '';
// 初始化
function init() {
// 监听Slideshow按钮点击
const slideshowBtn = document.getElementById('start-slideshow');
if (slideshowBtn) {
slideshowBtn.addEventListener('click', () => {
setTimeout(addDownloadButton, 800);
});
}
// 监听DOM变化
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
const toolbar = document.querySelector('.lg-toolbar');
if (toolbar && !downloadBtn) {
addDownloadButton();
}
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
// 添加下载按钮
function addDownloadButton() {
if (downloadBtn) return;
const toolbar = document.querySelector('.lg-toolbar');
if (!toolbar) {
setTimeout(addDownloadButton, 500);
return;
}
// 创建下载按钮
downloadBtn = document.createElement('button');
downloadBtn.type = 'button';
downloadBtn.className = 'lg-icon';
downloadBtn.setAttribute('aria-label', 'Download to local');
downloadBtn.innerHTML = `
`;
downloadBtn.style.cssText = `
background: transparent;
border: none;
cursor: pointer;
padding: 8px;
color: #fff;
display: inline-flex;
align-items: center;
justify-content: center;
`;
// 插入到工具栏中现有下载按钮之后
const existingDownload = toolbar.querySelector('.lg-download');
if (existingDownload && existingDownload.nextSibling) {
toolbar.insertBefore(downloadBtn, existingDownload.nextSibling);
} else {
toolbar.appendChild(downloadBtn);
}
// 监听图片切换
setupImageMonitoring();
// 点击事件
downloadBtn.addEventListener('click', handleDownload);
}
// 设置图片监控
function setupImageMonitoring() {
// 监听lightgallery的slide事件
const lgContainer = document.querySelector('.lg');
if (lgContainer) {
lgContainer.addEventListener('lgBeforeSlide', updateCurrentImageUrl);
lgContainer.addEventListener('lgAfterSlide', updateCurrentImageUrl);
}
// 定期检查图片变化
setInterval(updateCurrentImageUrl, 1000);
// 初始化当前图片URL
updateCurrentImageUrl();
}
// 更新当前图片URL
function updateCurrentImageUrl() {
const lgImage = document.querySelector('.lg-current .lg-image');
if (lgImage) {
const src = lgImage.src || lgImage.getAttribute('data-src');
if (src && src !== currentImageUrl) {
currentImageUrl = src;
}
}
}
// 处理下载
async function handleDownload() {
if (!currentImageUrl) {
updateCurrentImageUrl();
if (!currentImageUrl) {
alert('No image found!');
return;
}
}
const btn = downloadBtn;
const originalContent = btn.innerHTML;
// 显示加载状态
btn.innerHTML = '⏳';
btn.disabled = true;
try {
// 获取文件名
const url = new URL(currentImageUrl);
const pathname = url.pathname;
let filename = pathname.split('/').pop() || 'image.jpg';
// 使用GM_xmlhttpRequest下载
await new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url: currentImageUrl,
responseType: 'blob',
onload: function(response) {
try {
const blob = response.response;
const blobUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = blobUrl;
a.download = filename;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(() => URL.revokeObjectURL(blobUrl), 100);
resolve();
} catch (e) {
reject(e);
}
},
onerror: function(error) {
reject(error);
},
ontimeout: function() {
reject(new Error('Request timeout'));
}
});
});
// 显示成功状态
btn.innerHTML = '✓';
setTimeout(() => {
btn.innerHTML = originalContent;
btn.disabled = false;
}, 1500);
} catch (error) {
console.error('Download failed:', error);
// 失败时尝试备用方法:直接打开图片
btn.innerHTML = '!';
const confirmed = confirm('Direct download failed. Open image in new tab instead?\n\n' + currentImageUrl);
if (confirmed) {
window.open(currentImageUrl, '_blank');
}
setTimeout(() => {
btn.innerHTML = originalContent;
btn.disabled = false;
}, 1000);
}
}
// 启动脚本
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();
// https://www.auntmia.com/gallery/hot-blond-mature-iris-in-panties-exposes-big-titties-and-pussy-2176947/