惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
罗磊的独立博客
T
Tailwind CSS Blog
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - Franky
B
Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs

碎言

不懂就别瞎掰掰:【程序员的常识】写的什么玩意? AI 辅助编程下的程序设计与代码编写 使用 Next.js 和 Tailwind CSS 构建可编辑和删除的 ToDo 待办事项应用 探索编程新境界:MarsCode 助你一臂之力 使用 Next.js 和 Tailwind CSS 搭建静态图片展示站点并部署到 Vercel AI 辅助编程:免费工具的优缺点及官网一览 GitHub push更新总是失败,写个python脚本解决 把博客从GitHub迁移到到了vercel ComfyUI 和 Flux.1 安装与使用教程 Flux.1 入门必知:硬件、环境、模型 Git项目的子文件夹中的内容无法同步到远程仓库的解决方法 blender流体Fluid使用中没有流体、流体穿模等一些问题的解决方法 博客聚合网站:积薪,竟然关闭了! blender日常使用中的一些技巧 虽然只有我自己在用,但还是更新了碎言博客的源代码, 与其在迷茫中困惑,不如在努力中前进 差点忘了我还有一个博客... 秧歌、博客和AI 终于熬到了新手上路 好久没有更新博客了。。。 老妈大腿骨骨折,最近一直在医院护理 Hello, September! Ubuntu开机自动启动Docker容器运行WordPress Docker 简单快速安装部署WordPress Docker下安装MySQL 加碘盐能防核辐射的话,还怕什么核战争? shields.io 一个简洁、一致、清晰的徽章 Ubuntu下使用root登录ssh的设置 GitHub Actions 构建、部署 Next.js项目 我又用回了"IE"--edge
网页加载的时候获取giscus当前页面的评论数
J.sky · 2026-01-30 · via 碎言

前几天看到有个博客使用giscus,而且他的文章页打开后可以获得giscus的评论数,很是感兴趣,也就照猫画虎的实现了这个功能。

giscus

当然,写代码还是得先AI驱动。

核心属性

必须开启

属性说明
data-emit-metadata1必须开启,否则不会发送评论数信息

选择字段,需要那个用哪个。

字段含义
totalCommentCount顶级评论数(不含回复)
totalReplyCount回复数
totalCount评论 + 回复 总数

参考代码:

// 评论数获取模块
const GiscusCommentCount = {
    count: 0,
    callback: null,
    
    init(callback) {
        this.callback = callback;
        
        window.addEventListener('message', (event) => {
            if (event.origin !== 'https://giscus.app') return;
            
            const { giscus } = event.data || {};
            if (!giscus) return;
            
            // 获取评论数
            if (giscus.discussion) {
                this.count = giscus.discussion.totalCommentCount;
                if (this.callback) this.callback(this.count);
            }
            
            // 处理错误
            if (giscus.error) {
                console.error('Giscus error:', giscus.error);
                if (this.callback) this.callback(0);
            }
        });
        
        // 超时处理(5秒后仍未获取)
        setTimeout(() => {
            if (this.count === 0 && this.callback) {
                this.callback(0);
            }
        }, 5000);
    }
};

// 使用
GiscusCommentCount.init((count) => {
    const el = document.getElementById('comment-count');
    el.textContent = count > 0 ? `${count} 条评论` : '暂无评论';
});

是坑?

改了半天,貌似只能获取顶级评论数(不含回复) ,获取评论+回复没有调试出来。有没有高人指点下?

总结

你把这些喂给AI,他就知道怎么配合你的编程语言和框架来修改代码了。