Instantly finds and clicks the download button on Anna's Archive download pages using a smart observer.
目前為
// ==UserScript==
// @name Anna's Archive Auto-Download (Pro Version)
// @namespace http://tampermonkey.net/
// @version 2.0
// @description Instantly finds and clicks the download button on Anna's Archive download pages using a smart observer.
// @author Your Best IT Guy Friend
// @match *://annas-archive.org/slow_download/*
// @match *://annas-archive.org/md5/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// --- Configuration ---
// These are the "selectors" we'll use to find the button. They are like a precise address for an element on a webpage.
// I've included several common ones for different download pages on the site.
const DOWNLOAD_BUTTON_SELECTORS = [
'a.btn.btn-primary.btn-lg', // The main "Download now" button
'button[type="submit"]', // A common fallback for form-based downloads
'a[href*="/download/"]' // A general selector for links containing "/download/"
];
/**
* Finds and clicks the first available download button from our list of selectors.
* @returns {boolean} - True if a button was found and clicked, otherwise false.
*/
const clickDownloadButton = () => {
for (const selector of DOWNLOAD_BUTTON_SELECTORS) {
const downloadBtn = document.querySelector(selector);
if (downloadBtn) {
console.log(`Found download button with selector: "${selector}". Clicking it now!`);
downloadBtn.click();
return true; // Found and clicked, our job is done.
}
}
console.log('Waiting for a download button to appear...');
return false; // No button found yet.
};
// --- The Magic: MutationObserver ---
// This is our smart listener. It waits for changes in the webpage's HTML.
const observer = new MutationObserver((mutationsList, obs) => {
// We run our click function every time the page changes.
if (clickDownloadButton()) {
console.log('Success! Disconnecting the observer to save resources.');
obs.disconnect(); // Stop watching once we've clicked the button.
}
});
// --- Script Execution ---
// Try to click the button immediately when the script runs.
// This handles cases where the page loads fast and the button is already there.
if (!clickDownloadButton()) {
// If the button isn't there yet, we start our observer.
// We tell it to watch the entire body of the page for new elements being added.
console.log('Button not found immediately. Starting MutationObserver to wait for it.');
observer.observe(document.body, {
childList: true, // Watch for direct children being added or removed
subtree: true // Watch all descendants, not just direct children
});
}
})();