|
| 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 | + |
1 | 34 | function addGiscus () {
|
2 | 35 | const script = document.createElement('script')
|
3 | 36 | // use local file
|
@@ -42,6 +75,7 @@ function addGiscus () {
|
42 | 75 | console.log('Error occurred while loading script');
|
43 | 76 | };
|
44 | 77 | document.querySelector('.page').appendChild(script)
|
| 78 | + addHistoryEvent() |
45 | 79 | }
|
46 | 80 | export default ({
|
47 | 81 | Vue, // VuePress 正在使用的 Vue 构造函数
|
@@ -89,10 +123,7 @@ export default ({
|
89 | 123 | }
|
90 | 124 | }
|
91 | 125 | }
|
92 |
| - // setTimeout(() => { |
93 |
| - // addGiscus() |
94 |
| - // }, 3000) |
95 |
| - // 选择一个要监听的节点 |
| 126 | + |
96 | 127 | const targetNode = document.body
|
97 | 128 |
|
98 | 129 | // 创建一个新的 MutationObserver
|
|
0 commit comments