MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
Line 14: | Line 14: | ||
} | } | ||
// Fetch all categories except "Pages with ignored display titles" | |||
fetch(`${wikihome}/api.php?action=query&list=allcategories&aclimit=max&format=json`) | fetch(`${wikihome}/api.php?action=query&list=allcategories&aclimit=max&format=json`) | ||
.then(response => response.json()) | .then(response => response.json()) | ||
Line 23: | Line 24: | ||
const shuffledCategories = shuffleArray(allCategories); | const shuffledCategories = shuffleArray(allCategories); | ||
const container = document.getElementById('random-articles'); | const container = document.getElementById('random-articles'); | ||
container.innerHTML = ''; | container.innerHTML = ''; | ||
shuffledCategories.forEach(category => { | |||
// Create the category div and loading message | |||
const div = document.createElement('div'); | const div = document.createElement('div'); | ||
div.className = 'category-group'; | div.className = 'category-group'; | ||
Line 48: | Line 36: | ||
div.appendChild(categoryTitle); | div.appendChild(categoryTitle); | ||
const loadingMessage = document.createElement('h3'); | |||
loadingMessage.textContent = 'loading'; | |||
div.appendChild(loadingMessage); | |||
container.appendChild(div); | container.appendChild(div); | ||
// Fetch articles for each category | |||
fetch(`${wikihome}/api.php?action=query&list=categorymembers&cmtitle=Category:${encodeURIComponent(category)}&cmtype=page&cmlimit=10&format=json`) | |||
.then(response => response.json()) | |||
.then(data => { | |||
const articles = data.query.categorymembers.map(member => member.title); | |||
const shuffledArticles = shuffleArray(articles); | |||
// Remove loading message | |||
div.removeChild(loadingMessage); | |||
shuffledArticles.forEach(articleTitle => { | |||
const articleTitleElement = document.createElement('h3'); | |||
const link = document.createElement('a'); | |||
link.href = `${wikihome}/index.php/${articleTitle.replace(/ /g, "_")}`; | |||
link.textContent = articleTitle; | |||
articleTitleElement.appendChild(link); | |||
div.appendChild(articleTitleElement); | |||
}); | |||
}) | |||
.catch(error => { | |||
console.error('Error fetching category members:', error); | |||
// Optional: handle errors by replacing the loading message with an error message | |||
loadingMessage.textContent = 'Error loading articles'; | |||
}); | |||
}); | }); | ||
}) | }) | ||
.catch(error => { | .catch(error => { | ||
console.error('Error fetching | console.error('Error fetching categories:', error); | ||
}); | }); | ||
})(); | })(); |
Revision as of 09:23, 8 August 2024
const wikihome = 'http://localhost/wikidef';
$(document).ready(function() {
$('.cs-comment-form').show();
});
(function() {
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
// Fetch all categories except "Pages with ignored display titles"
fetch(`${wikihome}/api.php?action=query&list=allcategories&aclimit=max&format=json`)
.then(response => response.json())
.then(data => {
const allCategories = data.query.allcategories
.map(cat => cat['*'])
.filter(category => category !== 'Pages with ignored display titles');
const shuffledCategories = shuffleArray(allCategories);
const container = document.getElementById('random-articles');
container.innerHTML = '';
shuffledCategories.forEach(category => {
// Create the category div and loading message
const div = document.createElement('div');
div.className = 'category-group';
const categoryTitle = document.createElement('h2');
categoryTitle.textContent = category;
div.appendChild(categoryTitle);
const loadingMessage = document.createElement('h3');
loadingMessage.textContent = 'loading';
div.appendChild(loadingMessage);
container.appendChild(div);
// Fetch articles for each category
fetch(`${wikihome}/api.php?action=query&list=categorymembers&cmtitle=Category:${encodeURIComponent(category)}&cmtype=page&cmlimit=10&format=json`)
.then(response => response.json())
.then(data => {
const articles = data.query.categorymembers.map(member => member.title);
const shuffledArticles = shuffleArray(articles);
// Remove loading message
div.removeChild(loadingMessage);
shuffledArticles.forEach(articleTitle => {
const articleTitleElement = document.createElement('h3');
const link = document.createElement('a');
link.href = `${wikihome}/index.php/${articleTitle.replace(/ /g, "_")}`;
link.textContent = articleTitle;
articleTitleElement.appendChild(link);
div.appendChild(articleTitleElement);
});
})
.catch(error => {
console.error('Error fetching category members:', error);
// Optional: handle errors by replacing the loading message with an error message
loadingMessage.textContent = 'Error loading articles';
});
});
})
.catch(error => {
console.error('Error fetching categories:', error);
});
})();