Skip to content

Commit c071ac1

Browse files
committed
feat: do some hacks to ensure MDN page toc anchor active as the page scrolls #14
1 parent 910e196 commit c071ac1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

toc-bar.user.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
// @grant GM_addStyle
5656
// @grant GM_setValue
5757
// @grant GM_getValue
58-
// @require https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.11.1/tocbot.min.js
58+
// @require https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.18.2/tocbot.min.js
5959
// @icon https://raw.githubusercontent.com/hikerpig/toc-bar-userscript/master/toc-logo.svg
6060
// @homepageURL https://github.com/hikerpig/toc-bar-userscript
6161
// @downloadURL https://raw.githubusercontent.com/hikerpig/toc-bar-userscript/master/toc-bar.user.js
@@ -73,6 +73,7 @@
7373
* @property {() => Boolean} shouldShow
7474
* @property {(ele) => HTMLElement} findHeaderId
7575
* @property {(e) => void} onClick
76+
* @property {(tocBar: TocBar) => void} onInit
7677
*/
7778

7879
/** @type {{[key: string]: Partial<SiteSetting>}} */
@@ -209,7 +210,12 @@
209210
}
210211
},
211212
'developer.mozilla.org': {
212-
contentSelector: '#content'
213+
contentSelector: '#content',
214+
onInit() {
215+
setTimeout(() => {
216+
tocbot.refresh()
217+
}, 2000)
218+
}
213219
},
214220
'learning.oreilly.com': {
215221
contentSelector: '#sbo-rt-content'
@@ -771,6 +777,7 @@ a.toc-link {
771777
},
772778
/**
773779
* @method TocBar
780+
* @param {SiteSetting} options
774781
*/
775782
initTocbot(options) {
776783
const me = this
@@ -811,6 +818,9 @@ a.toc-link {
811818
// console.log('tocbotOptions', tocbotOptions);
812819
try {
813820
tocbot.init(tocbotOptions)
821+
if (options.onInit) {
822+
options.onInit(this)
823+
}
814824
} catch (error) {
815825
console.warn('error in tocbot.init', error)
816826
}

0 commit comments

Comments
 (0)