-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto-toc.user.js
172 lines (147 loc) · 3.56 KB
/
auto-toc.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// ==UserScript==
//
// @name Auto TOC
// @namespace letientai299
// @author Le Tien Tai
// @description Add a Table of Content on any site. Toggleable with Alt+q short cut.
// @include https://*/*
// @include http://*/*
// @include file://*
// @require https://code.jquery.com/jquery-3.1.1.min.js
// @require https://cdn.rawgit.com/jgallen23/toc/0.3.2/dist/toc.min.js
// @grant GM_addStyle
// @run-at document-idle
// @noframes
// @version 0.0.3
// @updateURL https://raw.githubusercontent.com/letientai299/userscript-auto-toc/master/auto-toc.user.js
// @downloadURL https://raw.githubusercontent.com/letientai299/userscript-auto-toc/master/auto-toc.user.js
// @license MIT License
//
// ==/UserScript==
/**
* ID of the TOC element. Used for css class prefix also.
*/
let tocId = "minetoc"
let getToc = function() {
return $(`#${tocId}`);
}
let includedSitePatterns = [
/.*.wiki.*\/.*/,
/.*medium.com\/.*/,
/.*.gh.neting.cc\/.*/,
/file:\/\/\/.*/
];
/**
* Test whehter the website location is excluded by default (TOC won't displaying).
*/
let isOnIncludedSites = function(location) {
return includedSitePatterns.some((pattern) => {
return pattern.test(location)
})
};
/**
* Keep track of TOC displaying status
*/
var isTocDisplaying = true;
let bodyElement = $("body");
let tocPadding = 10;
let bodyOriginalMargin = parseInt(bodyElement.css("margin-left"), 10);
let updateBodyMargin = function() {
let tocWidth = parseInt(getToc().css("width"), 10);
let bodyNewMargin = isTocDisplaying ?
tocWidth + bodyOriginalMargin + tocPadding :
bodyOriginalMargin;
bodyElement.css("margin-left", `${bodyNewMargin}px`);
}
let setTocDisplayMode = function(isDisplay) {
let cssDisplay = isDisplay ? "block" : "none";
getToc().css("display", cssDisplay);
// This trick make the updateBodyMargin be executed right after browser
// completely render the element with all css.
// See http://stackoverflow.com/a/21043017/3869533
setTimeout(updateBodyMargin, 0)
}
let toggleToc = function() {
isTocDisplaying = !isTocDisplaying;
setTocDisplayMode(isTocDisplaying);
}
/**
* Build the TOC using jquery and toc plugin.
*/
bodyElement.append(`<div id=\"${tocId}\"></div>`);
let tocConfig = {
'selectors': 'h1,h2,h3,h4,h5', //elements to use as headings
}
getToc().toc(tocConfig);
if (isOnIncludedSites(window.location.href)) {
isTocDisplaying = true;
setTocDisplayMode(isTocDisplaying);
}
/**
* Add shortcut to toggle the TOC
*/
document.addEventListener('keydown', function(e) {
// Alt+q
if (e.keyCode == 81 && !e.shiftKey && !e.ctrlKey && e.altKey && !e.metaKey) {
toggleToc();
}
}, false);
GM_addStyle(
`
#${tocId} {
display: none;
float: left;
overflow-y: auto;
resize: horizontal;
top: 0px;
left: 0px;
height: 100%;
position: fixed;
background: #222;
box-shadow: inset -5px 0 5px 0px #000;
width: 300px;
max-width: 600px;
padding-top: 80px;
color: #fff !important;
z-index: 99999;
}
#${tocId} ul {
margin: 0;
padding: 0;
}
#${tocId} li {
padding: 5px 10px;
}
#${tocId} a {
color: #fff;
text-decoration: none;
display: block;
}
#${tocId} .toc-h1 {
font-size: 1.5rem;
}
#${tocId} .toc-h2 {
padding-left: 10px;
font-size: 1.3rem;
opacity: 0.9;
}
#${tocId} .toc-h3 {
padding-left: 20px;
font-size: 1.1rem;
opacity: 0.8;
}
#${tocId} .toc-h4 {
padding-left: 30px;
font-size: 0.9rem;
opacity: 0.7;
}
#${tocId} .toc-h5 {
padding-left: 40px;
opacity: 0.6;
}
#${tocId} .toc-active {
background: #336699;
box-shadow: inset -5px 0px 10px -5px #000;
}
`
);