46
46
// ==/UserScript==
47
47
48
48
( function ( ) {
49
+ /**
50
+ * @typedef {Object } SiteSetting
51
+ * @property {string } contentSelector
52
+ * @property {string } siteName
53
+ * @property {Object } style
54
+ * @property {Number } scrollSmoothOffset
55
+ * @property {Number } initialTop
56
+ * @property {Number } headingsOffset
57
+ * @property {() => Boolean } shouldShow
58
+ * @property {(ele) => HTMLElement } findHeaderId
59
+ * @property {(e) => void } onClick
60
+ */
61
+
62
+ /** @type {{[key: string]: Partial<SiteSetting>} } */
49
63
const SITE_SETTINGS = {
50
64
jianshu : {
51
65
contentSelector : '.ouvJEz' ,
108
122
const WIKI_CONTENT_SEL = '#wiki-body'
109
123
const ISSUE_CONTENT_SEL = '.comment .comment-body'
110
124
111
- let matchedContainer
112
125
const matchedSel = [ README_SEL , ISSUE_CONTENT_SEL , WIKI_CONTENT_SEL ] . find ( ( sel ) => {
113
126
const c = document . querySelector ( sel )
114
127
if ( c ) {
115
- matchedContainer = c
116
128
return true
117
129
}
118
130
} )
145
157
} : null
146
158
147
159
return {
160
+ siteName : 'github.com' ,
148
161
contentSelector : matchedSel ,
149
162
hasInnerContainers : isIssueDetail ? true : false ,
150
163
scrollSmoothOffset : isIssueDetail ? - ISSUE_DETAIL_HEADING_OFFSET : 0 ,
151
164
headingsOffset : isIssueDetail ? ISSUE_DETAIL_HEADING_OFFSET : 0 ,
152
165
initialTop : 500 ,
153
166
onClick,
167
+ findHeaderId ( ele ) {
168
+ let id
169
+ let anchor = ele . querySelector ( '.anchor' )
170
+ if ( anchor ) id = anchor . getAttribute ( 'id' )
171
+
172
+ if ( ! anchor ) {
173
+ anchor = ele . querySelector ( 'a' )
174
+ if ( anchor ) id = anchor . hash . replace ( / ^ # / , '' )
175
+ }
176
+ return id
177
+ } ,
154
178
}
155
179
} ,
156
180
'developer.mozilla.org' : {
@@ -461,8 +485,15 @@ a.toc-link {
461
485
462
486
const DARKMODE_KEY = 'tocbar-darkmode'
463
487
488
+ /**
489
+ * @typedef {Object } TocBarOptions
490
+ * @property {String } [siteName]
491
+ * @property {Number } [initialTop]
492
+ */
493
+
464
494
/**
465
495
* @class
496
+ * @param {TocBarOptions } options
466
497
*/
467
498
function TocBar ( options = { } ) {
468
499
this . options = options
@@ -486,6 +517,7 @@ a.toc-link {
486
517
tocElement . classList . add ( TOCBOT_CONTAINTER_CLASS )
487
518
this . element . appendChild ( tocElement )
488
519
520
+ POSITION_STORAGE . checkCache ( )
489
521
const cachedPosition = POSITION_STORAGE . get ( options . siteName )
490
522
if ( ! isEmpty ( cachedPosition ) ) {
491
523
this . element . style . top = `${ Math . max ( 0 , cachedPosition . top ) } px`
@@ -588,7 +620,11 @@ a.toc-link {
588
620
589
621
const refreshElement = header . querySelector ( '.toc-bar__refresh' )
590
622
refreshElement . addEventListener ( 'click' , ( ) => {
591
- tocbot . refresh ( )
623
+ try {
624
+ tocbot . refresh ( )
625
+ } catch ( error ) {
626
+ console . warn ( 'error in tocbot.refresh' , error )
627
+ }
592
628
} )
593
629
594
630
const toggleSchemeElement = header . querySelector ( '.toc-bar__scheme' )
@@ -623,7 +659,7 @@ a.toc-link {
623
659
this . element . style . top = `${ newTop } px`
624
660
}
625
661
626
- const onMouseUp = ( e ) => {
662
+ const onMouseUp = ( ) => {
627
663
Object . assign ( dragState , {
628
664
isDragging : false ,
629
665
} )
@@ -671,13 +707,18 @@ a.toc-link {
671
707
{
672
708
tocSelector : `.${ TOCBOT_CONTAINTER_CLASS } ` ,
673
709
scrollSmoothOffset : options . scrollSmoothOffset || 0 ,
674
- // hasInnerContainers: true,
675
710
headingObjectCallback ( obj , ele ) {
676
711
// if there is no id on the header element, add one that derived from hash of header title
677
712
if ( ! ele . id ) {
678
- const newId = me . generateHeaderId ( obj , ele )
679
- ele . setAttribute ( 'id' , newId )
680
- obj . id = newId
713
+ let newId
714
+ if ( options . findHeaderId ) {
715
+ newId = options . findHeaderId ( ele )
716
+ }
717
+ if ( ! newId ) {
718
+ newId = me . generateHeaderId ( obj , ele )
719
+ ele . setAttribute ( 'id' , newId )
720
+ }
721
+ if ( newId ) obj . id = newId
681
722
}
682
723
return obj
683
724
} ,
@@ -687,7 +728,11 @@ a.toc-link {
687
728
options
688
729
)
689
730
// console.log('tocbotOptions', tocbotOptions);
690
- tocbot . init ( tocbotOptions )
731
+ try {
732
+ tocbot . init ( tocbotOptions )
733
+ } catch ( error ) {
734
+ console . warn ( 'error in tocbot.init' , error )
735
+ }
691
736
} ,
692
737
generateHeaderId ( obj , ele ) {
693
738
const hash = doContentHash ( obj . textContent )
0 commit comments