跳转到内容

MediaWiki:Randownload.js

来自ubuntustudioCN
Brucekomike留言 | 贡献2026年5月13日 (三) 18:38的版本 (Brucekomike移动页面Randownload.jsMediaWiki:Randownload.js,不留重定向)

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5

// The function to download a single random link function downloadRandomLink(links) {

 if (!links || links.length === 0) {
   console.error("The provided links array is empty or invalid.");
   return;
 }
 const randomIndex = Math.floor(Math.random() * links.length);
 const randomLink = links[randomIndex];
 const linkElement = document.createElement('a');
 linkElement.href = randomLink;
 // You might want to extract a more meaningful filename from the URL
 linkElement.download = `random_download_${Date.now()}`;
 document.body.appendChild(linkElement);
 linkElement.click();
 setTimeout(() => {
   document.body.removeChild(linkElement);
 }, 100);

}

document.addEventListener('DOMContentLoaded', () => {

 // Select all paragraphs that are designated as link list containers
 const linkListContainers = document.querySelectorAll('.link-list-container');
 linkListContainers.forEach(container => {
   const linkElements = container.querySelectorAll('a'); // Select links *within this specific container*
   const extractedLinks = [];
   linkElements.forEach(link => {
     if (link.href) {
       extractedLinks.push(link.href);
     }
   });
   if (extractedLinks.length > 0) {
     console.log("Found links in container:", extractedLinks);
     // Create a button for this specific link list
     const downloadButton = document.createElement('button');
     downloadButton.textContent = 'Download Random Link from this list';
     downloadButton.style.marginLeft = '10px'; // Add some spacing
     // Add an event listener to the button
     downloadButton.onclick = () => {
       downloadRandomLink(extractedLinks);
     };
     // Append the button to the container (or near it)
     // We'll append it after the paragraph for better placement
     container.parentNode.insertBefore(downloadButton, container.nextSibling);
   } else {
     console.log("No links found within a .link-list-container.");
   }
 });

});