forked from ipcjs/bilibili-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderscore_toggle_sidebar.user.js
141 lines (132 loc) · 4.29 KB
/
underscore_toggle_sidebar.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
// ==UserScript==
// @name Toggle Sidebar For underscorejs.org & liaoxuefeng
// @namespace https://tampermonkey.net/
// @version 0.9.1
// @description RT.
// @author ipcjs
// @include http://underscorejs.org/
// @include http://learningcn.com/underscore/
// @include http://www.liaoxuefeng.com/wiki/*
// @include https://www.liaoxuefeng.com/wiki/*
// @grant none
// @require https://rawgit.com/jakiestfu/Snap.js/develop/snap.min.js
// ==/UserScript==
(function () {
'use strict';
var defaultConfig = {
sidebar: '#sidebar', container: 'body > .container', // element selector
maxPosition: 266, minPosition: -266, // Snap setting
top: '0px', width: '200px', // sidebar attr
marginLeft: '260px', // container attr
func: null // to run funcation
}, configMap = {
'underscorejs.org': defaultConfig,
'learningcn.com': defaultConfig,
'www.liaoxuefeng.com': {
sidebar: '#main .x-sidebar-left', container: '#main .x-center',
maxPosition: 270, minPosition: -270,
top: '48px', width: '216px',
marginLeft: '220px',
func: function () {
document.querySelector('#footer').style.display = 'none';
var id = window.location.pathname.split('/').reverse()[0];
$(sidebar).scrollTop($('#' + id).offset().top - $(sidebar).offset().top);
}
}
}, config = configMap[location.host];
var toggleBtn,
sidebar = document.querySelector(config.sidebar),
container = document.querySelector(config.container),
snapper = new Snap({
element: sidebar,
maxPosition: config.maxPosition,
minPosition: config.minPosition
}),
body = document.querySelector('body'),
marginLeft = container.style.marginLeft,
position = container.style.position;
console.log(sidebar, container);
/* set container attr */
container.style.marginLeft = config.marginLeft;
snapper.on('open', function () { // to close...
container.style.marginLeft = '0px';
container.style.position = 'absolute';
});
snapper.on('close', function () { // to open...
// container.style.marginLeft = config.maxPosition;
// container.style.marginLeft = marginLeft;
container.style.marginLeft = config.marginLeft;
container.style.position = position;
});
/* set sidebar attr */
sidebar.style.display = 'block';
sidebar.style.width = config.width;
sidebar.style.float = 'left';
sidebar.style.overflowY = 'auto';
sidebar.style.top = config.top;
sidebar.style.bottom = '0px';
sidebar.style.position = 'fixed';
sidebar.className += ' hide-scrollbar-ipcjs';
/* set toggleBtn attr */
toggleBtn = document.createElement('a');
toggleBtn.id = 'toggle_btn';
toggleBtn.innerText = '>';
body.appendChild(toggleBtn);
toggleBtn.addEventListener('click', function () {
if (snapper.state().state == "right") {
snapper.close();
} else {
snapper.open('right');
}
});
/* other */
config.func && config.func();
let style = document.createElement('style');
style.innerHTML = `
/* css.start */
#toggle_btn {
position: fixed;
left: 0px;
top: 0px;
z-index: 1000;
width: 25px;
height: 25px;
border-radius: 50%;
background-color: transparent;
text-align: center;
color: black;
line-height: 25px;
}
#toggle_btn:hover {
background-color: black;
color: white;
text-decoration: none;
}
pre .comment {
font-style: normal;
}
.hide-scrollbar-ipcjs::-webkit-scrollbar {
width: 10px;
height: 10px;
}
.hide-scrollbar-ipcjs::-webkit-scrollbar-thumb {
background: transparent;
border-radius: 5px;
}
.hide-scrollbar-ipcjs:hover::-webkit-scrollbar-thumb {
background: #f1f1f1;
}
/* css.end */
`;
document.querySelector('head').appendChild(style);
/* auto show/hide sidebar */
window.addEventListener('resize', resetSnap);
resetSnap();
function resetSnap() {
if (window.innerWidth < 768) {
snapper.open('right'); // open() to close...
} else {
snapper.close(); // close() to open...
}
}
})();