MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
$('.cs-comment-form').show(); | $('.cs-comment-form').show(); | ||
}); | }); | ||
(function() { | (function() { | ||
Line 16: | Line 14: | ||
} | } | ||
fetch(`${wikihome}/api.php?action=query&list= | fetch(`${wikihome}/api.php?action=query&list=allcategories&aclimit=max&format=json`) | ||
.then(response => response.json()) | .then(response => response.json()) | ||
.then(data => { | .then(data => { | ||
const | const allCategories = data.query.allcategories | ||
.map(cat => cat['*']) | |||
.filter(category => category !== 'Pages with ignored display titles'); | |||
const | const shuffledCategories = shuffleArray(allCategories); | ||
return fetch(`${wikihome}/api.php?action=query& | const fetchCategoryMembersPromises = shuffledCategories.map(category => { | ||
return 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); | |||
return { category, articles }; | |||
}); | }); | ||
}); | }); | ||
const | return Promise.all(fetchCategoryMembersPromises); | ||
}) | |||
.then(categoriesData => { | |||
const container = document.getElementById('random-articles'); | |||
container.innerHTML = ''; | |||
categoriesData.forEach(({ category, articles }) => { | |||
const shuffledArticles = shuffleArray(articles); | |||
const shuffledArticles = shuffleArray( | |||
const div = document.createElement('div'); | const div = document.createElement('div'); | ||
Line 72: | Line 59: | ||
container.appendChild(div); | container.appendChild(div); | ||
}); | }); | ||
}) | }) | ||
.catch(error => { | .catch(error => { |
Revision as of 06:44, 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(`${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 fetchCategoryMembersPromises = shuffledCategories.map(category => {
return 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);
return { category, articles };
});
});
return Promise.all(fetchCategoryMembersPromises);
})
.then(categoriesData => {
const container = document.getElementById('random-articles');
container.innerHTML = '';
categoriesData.forEach(({ category, articles }) => {
const shuffledArticles = shuffleArray(articles);
const div = document.createElement('div');
div.className = 'category-group';
const categoryTitle = document.createElement('h2');
categoryTitle.textContent = category;
div.appendChild(categoryTitle);
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);
});
container.appendChild(div);
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
})();