Skip to content

Commit 05bfdd1

Browse files
committed
修正giscus评论不跟路由映射的问题
1 parent 448cd03 commit 05bfdd1

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

text/.vuepress/enhanceApp.js

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
function updateGiscus(path) {
2+
const iframe = document.querySelector('iframe.giscus-frame')
3+
if (iframe) {
4+
iframe?.contentWindow?.postMessage(
5+
{ giscus: { setConfig: { term: path } } },
6+
'https://giscus.app',
7+
);
8+
}
9+
}
10+
function addHistoryEvent() {
11+
const _historyWrap = function(type) {
12+
const orig = history[type];
13+
const e = new Event(type);
14+
return function() {
15+
const rv = orig.apply(this, arguments);
16+
e.arguments = arguments;
17+
window.dispatchEvent(e);
18+
return rv;
19+
};
20+
};
21+
history.pushState = _historyWrap('pushState');
22+
history.replaceState = _historyWrap('replaceState');
23+
24+
window.addEventListener('pushState', function(e) {
25+
console.log('change pushState', e);
26+
updateGiscus(e.arguments[2])
27+
});
28+
window.addEventListener('replaceState', function(e) {
29+
console.log('change replaceState', e);
30+
updateGiscus(e.arguments[2])
31+
});
32+
}
33+
134
function addGiscus () {
235
const script = document.createElement('script')
336
// use local file
@@ -42,6 +75,7 @@ function addGiscus () {
4275
console.log('Error occurred while loading script');
4376
};
4477
document.querySelector('.page').appendChild(script)
78+
addHistoryEvent()
4579
}
4680
export default ({
4781
Vue, // VuePress 正在使用的 Vue 构造函数
@@ -89,10 +123,7 @@ export default ({
89123
}
90124
}
91125
}
92-
// setTimeout(() => {
93-
// addGiscus()
94-
// }, 3000)
95-
// 选择一个要监听的节点
126+
96127
const targetNode = document.body
97128

98129
// 创建一个新的 MutationObserver

0 commit comments

Comments
 (0)