MediaWiki:Common.js
外观
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
// 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);
}
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 = '使用随机镜像源下载';
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.");
}
});