1
+ function addGiscus ( ) {
2
+ const script = document . createElement ( 'script' )
3
+ // use local file
4
+ // script.src = 'script.js';
5
+ script . src =
6
+ 'https://giscus.app/client.js' ;
7
+ script . async = true ;
8
+ script . crossOrigin = 'anonymous' ;
9
+ /**
10
+ * data-repo="yunnysunny/nodebook"
11
+ data-repo-id="MDEwOlJlcG9zaXRvcnk0MTAyNjM2Ng=="
12
+ data-category="Q&A"
13
+ data-category-id="MDE4OkRpc2N1c3Npb25DYXRlZ29yeTMyMTU4NDA2"
14
+ data-mapping="pathname"
15
+ data-strict="0"
16
+ data-reactions-enabled="1"
17
+ data-emit-metadata="0"
18
+ data-input-position="bottom"
19
+ data-theme="preferred_color_scheme"
20
+ data-lang="zh-CN"
21
+ */
22
+ const attrs = {
23
+ 'data-repo' : 'yunnysunny/nodebook' ,
24
+ 'data-repo-id' : 'MDEwOlJlcG9zaXRvcnk0MTAyNjM2Ng==' ,
25
+ 'data-category' : 'Q&A' ,
26
+ 'data-category-id' : 'MDE4OkRpc2N1c3Npb25DYXRlZ29yeTMyMTU4NDA2' ,
27
+ 'data-mapping' : 'pathname' ,
28
+ 'data-strict' : '0' ,
29
+ 'data-reactions-enabled' : '1' ,
30
+ 'data-emit-metadata' : '0' ,
31
+ 'data-input-position' : 'bottom' ,
32
+ 'data-theme' : 'preferred_color_scheme' ,
33
+ 'data-lang' : 'zh-CN' ,
34
+ }
35
+ for ( const key in attrs ) {
36
+ script . setAttribute ( key , attrs [ key ] )
37
+ }
38
+ script . onload = ( ) => {
39
+ console . log ( 'Script loaded successfuly' ) ;
40
+ } ;
41
+ script . onerror = ( ) => {
42
+ console . log ( 'Error occurred while loading script' ) ;
43
+ } ;
44
+ document . querySelector ( '.page' ) . appendChild ( script )
45
+ }
1
46
export default ( {
2
47
Vue, // VuePress 正在使用的 Vue 构造函数
3
48
options, // 附加到根实例的一些选项
@@ -44,4 +89,22 @@ export default ({
44
89
}
45
90
}
46
91
}
92
+ // setTimeout(() => {
93
+ // addGiscus()
94
+ // }, 3000)
95
+ // 选择一个要监听的节点
96
+ const targetNode = document . body
97
+
98
+ // 创建一个新的 MutationObserver
99
+ const observer = new MutationObserver ( ( ) => {
100
+ if ( document . querySelector ( '.page' ) ) {
101
+ addGiscus ( ) ;
102
+ observer . disconnect ( ) ; // 销毁监视者
103
+ }
104
+ } )
105
+
106
+ const config = { childList : true , subtree : true } // 对哪些更改做出反应
107
+
108
+ // 绑定目标节点并启动监视者
109
+ observer . observe ( targetNode , config )
47
110
}
0 commit comments