Skip to content

Commit

Permalink
修正giscus评论不跟路由映射的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyang2024 committed Apr 30, 2024
1 parent 448cd03 commit 05bfdd1
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions text/.vuepress/enhanceApp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
function updateGiscus(path) {
const iframe = document.querySelector('iframe.giscus-frame')
if (iframe) {
iframe?.contentWindow?.postMessage(
{ giscus: { setConfig: { term: path } } },
'https://giscus.app',
);
}
}
function addHistoryEvent() {
const _historyWrap = function(type) {
const orig = history[type];
const e = new Event(type);
return function() {
const rv = orig.apply(this, arguments);
e.arguments = arguments;
window.dispatchEvent(e);
return rv;
};
};
history.pushState = _historyWrap('pushState');
history.replaceState = _historyWrap('replaceState');

window.addEventListener('pushState', function(e) {
console.log('change pushState', e);
updateGiscus(e.arguments[2])
});
window.addEventListener('replaceState', function(e) {
console.log('change replaceState', e);
updateGiscus(e.arguments[2])
});
}

function addGiscus () {
const script = document.createElement('script')
// use local file
Expand Down Expand Up @@ -42,6 +75,7 @@ function addGiscus () {
console.log('Error occurred while loading script');
};
document.querySelector('.page').appendChild(script)
addHistoryEvent()
}
export default ({
Vue, // VuePress 正在使用的 Vue 构造函数
Expand Down Expand Up @@ -89,10 +123,7 @@ export default ({
}
}
}
// setTimeout(() => {
// addGiscus()
// }, 3000)
// 选择一个要监听的节点

const targetNode = document.body

// 创建一个新的 MutationObserver
Expand Down

0 comments on commit 05bfdd1

Please sign in to comment.