@@ -38,21 +38,22 @@ export default function GiscusComment() {
3838 script . setAttribute ( 'data-repo-id' , 'R_kgDOOI9EFg' )
3939 script . setAttribute ( 'data-category' , 'Announcements' )
4040 script . setAttribute ( 'data-category-id' , 'DIC_kwDOOI9EFs4Cr9K0' )
41- script . setAttribute ( 'data-mapping' , 'specific' )
42- script . setAttribute ( 'data-term' , pathname )
43- script . setAttribute ( 'data-strict' , '1' )
41+ script . setAttribute ( 'data-mapping' , 'pathname' )
42+ script . setAttribute ( 'data-strict' , '0' )
4443 script . setAttribute ( 'data-reactions-enabled' , '1' )
4544 script . setAttribute ( 'data-emit-metadata' , '0' )
4645 script . setAttribute ( 'data-input-position' , 'top' )
47- script . setAttribute (
48- 'data-theme' ,
49- resolvedTheme === 'dark' ? 'transparent_dark' : 'light' ,
50- )
46+ script . setAttribute ( 'data-theme' , 'preferred_color_scheme' )
5147 script . setAttribute ( 'data-loading' , 'lazy' )
52- script . setAttribute ( 'data-lang' , 'zh-CN ' )
48+ script . setAttribute ( 'data-lang' , 'en ' )
5349 script . crossOrigin = 'anonymous'
5450 script . async = true
5551
52+ // 错误处理
53+ script . onerror = ( ) => {
54+ console . error ( 'Failed to load Giscus script' )
55+ }
56+
5657 // 找到Giscus容器并添加脚本
5758 const giscusContainer = document . querySelector ( '.giscus' )
5859 if ( giscusContainer ) {
@@ -75,46 +76,29 @@ export default function GiscusComment() {
7576 } // 依赖 resolvedTheme 是正确的
7677 } , [ pathname , resolvedTheme ] ) // eslint-disable-line react-hooks/exhaustive-deps
7778
78- // 监听主题变化,实时更新Giscus主题
79+ // 使用 preferred_color_scheme 后,Giscus 会自动根据系统主题切换
80+ // 如果需要手动同步主题,可以保留以下代码
7981 useEffect ( ( ) => {
8082 if ( ! isGiscusLoaded ) return
8183
8284 const updateGiscusTheme = ( ) => {
8385 const iframe = document . querySelector (
8486 'iframe.giscus-frame' ,
8587 ) as HTMLIFrameElement
86- if ( iframe ) {
87- const theme = resolvedTheme === 'dark' ? 'transparent_dark' : 'light'
88- iframe . contentWindow ? .postMessage (
89- { giscus : { setConfig : { theme } } } ,
88+ if ( iframe && iframe . contentWindow ) {
89+ // 使用 preferred_color_scheme 让 giscus 自动跟随系统主题
90+ iframe . contentWindow . postMessage (
91+ { giscus : { setConfig : { theme : 'preferred_color_scheme' } } } ,
9092 'https://giscus.app' ,
9193 )
9294 }
9395 }
9496
95- // 监听Giscus消息
96- const handleMessage = ( event : MessageEvent ) => {
97- if (
98- event . origin === 'https://giscus.app' &&
99- event . data ?. giscus ?. discussion
100- ) {
101- updateGiscusTheme ( )
102- }
103- }
104-
105- window . addEventListener ( 'message' , handleMessage )
106-
107- // 轮询尝试更新主题,确保iframe加载后应用正确主题
108- let attempts = 0
109- const interval = setInterval ( ( ) => {
110- updateGiscusTheme ( )
111- attempts ++
112- if ( attempts >= 10 ) clearInterval ( interval )
113- } , 500 )
97+ // 给 iframe 一些时间加载
98+ const timer = setTimeout ( updateGiscusTheme , 1000 )
11499
115100 return ( ) => {
116- window . removeEventListener ( 'message' , handleMessage )
117- clearInterval ( interval )
101+ clearTimeout ( timer )
118102 }
119103 } , [ resolvedTheme , isGiscusLoaded ] )
120104
0 commit comments