获得徽章 0
赞了这篇沸点
//抽取随机评论
function getComments() {
const comments = [];
// 选择器需要根据实际网页的HTML结构调整
const commentItems = document.querySelectorAll('.comment-card');
commentItems.forEach(item => {
const userName = item.innerText.slice(0,-5);
if (userName) {
comments.push(userName);
}
});
return comments;
}
// 随机抽取一位用户
function pickRandomUser(comments) {
if (comments.length === 0) {
console.log("没找到幸运儿");
return null;
}
const randomIndex = Math.floor(Math.random() * comments.length);
return [comments[randomIndex],randomIndex+1];
}
// 主函数
function main() {
const comments = getComments();
if (comments.length > 0) {
const randomUser = pickRandomUser(comments);
console.log(`幸运儿是:第${randomUser[1]}楼, ${randomUser[0]}`);
} else {
console.log("没找到幸运儿");
}
}
// 运行主函数
main();
function getComments() {
const comments = [];
// 选择器需要根据实际网页的HTML结构调整
const commentItems = document.querySelectorAll('.comment-card');
commentItems.forEach(item => {
const userName = item.innerText.slice(0,-5);
if (userName) {
comments.push(userName);
}
});
return comments;
}
// 随机抽取一位用户
function pickRandomUser(comments) {
if (comments.length === 0) {
console.log("没找到幸运儿");
return null;
}
const randomIndex = Math.floor(Math.random() * comments.length);
return [comments[randomIndex],randomIndex+1];
}
// 主函数
function main() {
const comments = getComments();
if (comments.length > 0) {
const randomUser = pickRandomUser(comments);
console.log(`幸运儿是:第${randomUser[1]}楼, ${randomUser[0]}`);
} else {
console.log("没找到幸运儿");
}
}
// 运行主函数
main();
展开
1
2
![[不失礼貌的微笑]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_16.9d17f6d.png)
![[无辜呆]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_96.4fb9d71.png)
![[尬笑]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_59.8a63cb5.png)
![[捂脸]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_28.8981538.png)