5
5
// @license MIT
6
6
// @description A floating table of content widget
7
7
// @description :zh-CN 在页面右侧展示一个浮动的文章大纲目录
8
- // @version 1.0.3
8
+ // @version 1.1.0
9
9
// @match *://www.jianshu.com/p/*
10
10
// @match *://cdn2.jianshu.io/p/*
11
11
// @match *://zhuanlan.zhihu.com/p/*
16
16
// @match *://web.dev/*
17
17
// @match *://medium.com/*
18
18
// @match *://css-tricks.com/*
19
- // @match *://www.smashingmagazine.com/*
19
+ // @match *://www.smashingmagazine.com/*/*
20
20
// @match *://distill.pub/*
21
21
// @match *://github.com/*/*
22
22
// @match *://developer.mozilla.org/*/docs/*
23
23
// @run -at document-idle
24
24
// @grant GM_getResourceText
25
+ // @grant GM_addStyle
25
26
// @require https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.11.1/tocbot.min.js
26
27
// @homepageURL https://github.com/hikerpig/toc-bar-userscript
27
28
// @downloadURL https://raw.githubusercontent.com/hikerpig/toc-bar-userscript/master/toc-bar.user.js
123
124
}
124
125
}
125
126
126
- function loadStyles ( ) {
127
+
128
+ function guessThemeColor ( ) {
129
+ const meta = document . head . querySelector ( 'meta[name="theme-color"]' )
130
+ if ( meta ) {
131
+ return meta . getAttribute ( 'content' )
132
+ }
127
133
}
128
134
129
135
/**
138
144
// ---------------- TocBar ----------------------
139
145
const TOC_BAR_STYLE = `
140
146
.toc-bar {
147
+ --toc-bar-active-color: #54BC4B;
148
+
141
149
position: fixed;
142
150
z-index: 9000;
143
151
right: 5px;
240
248
.toc-bar__toc > .toc-list {
241
249
margin: 0;
242
250
overflow: hidden;
243
- position: relative
251
+ position: relative;
252
+ padding-left: 5px;
244
253
}
245
254
246
255
.toc-bar__toc>.toc-list li {
251
260
252
261
a.toc-link {
253
262
color: currentColor;
254
- height: 100%
263
+ height: 100%;
255
264
}
256
265
257
266
.is-collapsible {
258
267
max-height: 1000px;
259
268
overflow: hidden;
260
- transition: all 300ms ease-in-out
269
+ transition: all 300ms ease-in-out;
261
270
}
262
271
263
272
.is-collapsed {
264
- max-height: 0
273
+ max-height: 0;
265
274
}
266
275
267
276
.is-position-fixed {
268
277
position: fixed !important;
269
- top: 0
278
+ top: 0;
270
279
}
271
280
272
281
.is-active-link {
273
- font-weight: 700
282
+ font-weight: 700;
274
283
}
275
284
276
285
.toc-link::before {
@@ -281,11 +290,11 @@ a.toc-link {
281
290
left: 0;
282
291
margin-top: -1px;
283
292
position: absolute;
284
- width: 2px
293
+ width: 2px;
285
294
}
286
295
287
296
.is-active-link::before {
288
- background-color: #54BC4B
297
+ background-color: var(--toc-bar-active-color);
289
298
}
290
299
/* end tocbot related */
291
300
`
@@ -439,16 +448,22 @@ a.toc-link {
439
448
}
440
449
this . visible = shouldShow
441
450
} ,
451
+ refreshStyle ( ) {
452
+ const themeColor = guessThemeColor ( )
453
+ if ( themeColor ) {
454
+ this . element . style . setProperty ( '--toc-bar-active-color' , themeColor ) ;
455
+ }
456
+ } ,
442
457
}
443
458
// ----------------end TocBar -------------------
444
459
445
460
function main ( ) {
446
461
const options = getPageTocOptions ( )
447
462
if ( options ) {
448
- loadStyles ( )
449
463
450
464
const tocBar = new TocBar ( )
451
465
tocBar . initTocbot ( options )
466
+ tocBar . refreshStyle ( )
452
467
}
453
468
}
454
469
0 commit comments