Skip to content

Commit 4db05b5

Browse files
committed
feat: guess theme color from document.head meta
1 parent 5ebdde0 commit 4db05b5

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

toc-bar.user.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// @license MIT
66
// @description A floating table of content widget
77
// @description:zh-CN 在页面右侧展示一个浮动的文章大纲目录
8-
// @version 1.0.3
8+
// @version 1.1.0
99
// @match *://www.jianshu.com/p/*
1010
// @match *://cdn2.jianshu.io/p/*
1111
// @match *://zhuanlan.zhihu.com/p/*
@@ -16,12 +16,13 @@
1616
// @match *://web.dev/*
1717
// @match *://medium.com/*
1818
// @match *://css-tricks.com/*
19-
// @match *://www.smashingmagazine.com/*
19+
// @match *://www.smashingmagazine.com/*/*
2020
// @match *://distill.pub/*
2121
// @match *://github.com/*/*
2222
// @match *://developer.mozilla.org/*/docs/*
2323
// @run-at document-idle
2424
// @grant GM_getResourceText
25+
// @grant GM_addStyle
2526
// @require https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.11.1/tocbot.min.js
2627
// @homepageURL https://github.com/hikerpig/toc-bar-userscript
2728
// @downloadURL https://raw.githubusercontent.com/hikerpig/toc-bar-userscript/master/toc-bar.user.js
@@ -123,7 +124,12 @@
123124
}
124125
}
125126

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+
}
127133
}
128134

129135
/**
@@ -138,6 +144,8 @@
138144
// ---------------- TocBar ----------------------
139145
const TOC_BAR_STYLE = `
140146
.toc-bar {
147+
--toc-bar-active-color: #54BC4B;
148+
141149
position: fixed;
142150
z-index: 9000;
143151
right: 5px;
@@ -240,7 +248,8 @@
240248
.toc-bar__toc > .toc-list {
241249
margin: 0;
242250
overflow: hidden;
243-
position: relative
251+
position: relative;
252+
padding-left: 5px;
244253
}
245254
246255
.toc-bar__toc>.toc-list li {
@@ -251,26 +260,26 @@
251260
252261
a.toc-link {
253262
color: currentColor;
254-
height: 100%
263+
height: 100%;
255264
}
256265
257266
.is-collapsible {
258267
max-height: 1000px;
259268
overflow: hidden;
260-
transition: all 300ms ease-in-out
269+
transition: all 300ms ease-in-out;
261270
}
262271
263272
.is-collapsed {
264-
max-height: 0
273+
max-height: 0;
265274
}
266275
267276
.is-position-fixed {
268277
position: fixed !important;
269-
top: 0
278+
top: 0;
270279
}
271280
272281
.is-active-link {
273-
font-weight: 700
282+
font-weight: 700;
274283
}
275284
276285
.toc-link::before {
@@ -281,11 +290,11 @@ a.toc-link {
281290
left: 0;
282291
margin-top: -1px;
283292
position: absolute;
284-
width: 2px
293+
width: 2px;
285294
}
286295
287296
.is-active-link::before {
288-
background-color: #54BC4B
297+
background-color: var(--toc-bar-active-color);
289298
}
290299
/* end tocbot related */
291300
`
@@ -439,16 +448,22 @@ a.toc-link {
439448
}
440449
this.visible = shouldShow
441450
},
451+
refreshStyle() {
452+
const themeColor = guessThemeColor()
453+
if (themeColor) {
454+
this.element.style.setProperty('--toc-bar-active-color', themeColor);
455+
}
456+
},
442457
}
443458
// ----------------end TocBar -------------------
444459

445460
function main() {
446461
const options = getPageTocOptions()
447462
if (options) {
448-
loadStyles()
449463

450464
const tocBar = new TocBar()
451465
tocBar.initTocbot(options)
466+
tocBar.refreshStyle()
452467
}
453468
}
454469

0 commit comments

Comments
 (0)