跳转到内容

MediaWiki:Randownload.js:修订间差异

来自ubuntustudioCN
Brucekomike将页面MediaWiki:Randownload.js的内容模型从“wikitext”更改为“JavaScript”
无编辑摘要
第1行: 第1行:
// The function to download a single random link
// The function to download a single random link (remains the same)
function downloadRandomLink(links) {
function downloadRandomLink(links) {
   if (!links || links.length === 0) {
   if (!links || links.length === 0) {
第9行: 第9行:
   const linkElement = document.createElement('a');
   const linkElement = document.createElement('a');
   linkElement.href = randomLink;
   linkElement.href = randomLink;
  // You might want to extract a more meaningful filename from the URL
   linkElement.download = `random_download_${Date.now()}`; // Example filename
   linkElement.download = `random_download_${Date.now()}`;
   document.body.appendChild(linkElement);
   document.body.appendChild(linkElement);
   linkElement.click();
   linkElement.click();
第19行: 第18行:


document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('DOMContentLoaded', () => {
   // Select all paragraphs that are designated as link list containers
   // Select all divs that are designated as random link generators
   const linkListContainers = document.querySelectorAll('.link-list-container');
   const linkGeneratorDivs = document.querySelectorAll('.random-link-generator');


   linkListContainers.forEach(container => {
   linkGeneratorDivs.forEach(containerDiv => {
     const linkElements = container.querySelectorAll('a'); // Select links *within this specific container*
    // Select all anchor (<a>) tags *within this specific div*
     const linkElements = containerDiv.querySelectorAll('a');
     const extractedLinks = [];
     const extractedLinks = [];


第33行: 第33行:


     if (extractedLinks.length > 0) {
     if (extractedLinks.length > 0) {
       console.log("Found links in container:", extractedLinks);
       console.log("Found links in div:", extractedLinks);


       // Create a button for this specific link list
       // Create a button for this specific div's link list
       const downloadButton = document.createElement('button');
       const downloadButton = document.createElement('button');
       downloadButton.textContent = 'Download Random Link from this list';
       downloadButton.textContent = 'Download Random Link from this block';
       downloadButton.style.marginLeft = '10px'; // Add some spacing
       downloadButton.style.marginLeft = '10px'; // Add some spacing


第45行: 第45行:
       };
       };


       // Append the button to the container (or near it)
       // Append the button to the DOM.
       // We'll append it after the paragraph for better placement
       // We'll append it after the div for better placement.
       container.parentNode.insertBefore(downloadButton, container.nextSibling);
      // If the div contains other elements you want the button after,
      // you might need a more specific insertion point.
       containerDiv.parentNode.insertBefore(downloadButton, containerDiv.nextSibling);
     } else {
     } else {
       console.log("No links found within a .link-list-container.");
       console.log("No links found within a .random-link-generator div.");
     }
     }
   });
   });
});
});

2026年5月13日 (三) 18:47的版本

// The function to download a single random link (remains the same)
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;
  linkElement.download = `random_download_${Date.now()}`; // Example filename
  document.body.appendChild(linkElement);
  linkElement.click();
  setTimeout(() => {
    document.body.removeChild(linkElement);
  }, 100);
}

document.addEventListener('DOMContentLoaded', () => {
  // Select all divs that are designated as random link generators
  const linkGeneratorDivs = document.querySelectorAll('.random-link-generator');

  linkGeneratorDivs.forEach(containerDiv => {
    // Select all anchor (<a>) tags *within this specific div*
    const linkElements = containerDiv.querySelectorAll('a');
    const extractedLinks = [];

    linkElements.forEach(link => {
      if (link.href) {
        extractedLinks.push(link.href);
      }
    });

    if (extractedLinks.length > 0) {
      console.log("Found links in div:", extractedLinks);

      // Create a button for this specific div's link list
      const downloadButton = document.createElement('button');
      downloadButton.textContent = 'Download Random Link from this block';
      downloadButton.style.marginLeft = '10px'; // Add some spacing

      // Add an event listener to the button
      downloadButton.onclick = () => {
        downloadRandomLink(extractedLinks);
      };

      // Append the button to the DOM.
      // We'll append it after the div for better placement.
      // If the div contains other elements you want the button after,
      // you might need a more specific insertion point.
      containerDiv.parentNode.insertBefore(downloadButton, containerDiv.nextSibling);
    } else {
      console.log("No links found within a .random-link-generator div.");
    }
  });
});