优化结构
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user