Files
blog/static/js/random.js
T
2026-06-18 20:36:03 +08:00

23 lines
758 B
JavaScript

function randomPost() {
fetch('/api/posts.json')
.then(res => {
if (!res.ok) throw new Error(`Failed to load posts: ${res.status}`);
return res.json();
})
.then(posts => {
const postLinks = posts
.map(post => post && post.href)
.filter(href => typeof href === 'string' && href.startsWith('/posts/'));
if (postLinks.length === 0) {
console.warn('No posts available for random navigation.');
return;
}
location.href = postLinks[Math.floor(Math.random() * postLinks.length)];
})
.catch(error => {
console.error('Random post navigation failed:', error);
});
}