-
Notifications
You must be signed in to change notification settings - Fork 1
/
enhance_page.js
89 lines (74 loc) · 2.4 KB
/
enhance_page.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
function generateSectionsMenu() {
var h1_list = document.getElementsByTagName("H1");
var menu_div = document.createElement("DIV");
menu_div.style.position = "fixed";
menu_div.style.top = "0em";
menu_div.style.left = "0em";
menu_div.style.width = setMenyDivWidth();
menu_div.style.backgroundColor = "#888";
menu_div.style.borderRadius = "1em";
menu_div.style.margin = "0.5em";
var menu = document.createElement("UL");
menu.style.listStyleType = "none";
menu.style.padding = "0em 0.5em 0.5em 0.5em";
// // Andreas, 2016-11-16, I like it better without heading.
// // The menu should be self-explaining.
// var header = createLI();
// header.textContent = "Sections";
// header.style.textDecoration = "underline";
// menu.appendChild(header);
menu_div.appendChild(menu);
for (var i = 1; i < h1_list.length; i++) {
var bookmark = "section"+i;
var label = createLI();
var anchor = document.createElement("a");
h1_list[i].id = bookmark;
anchor.href = "#"+bookmark;
anchor.textContent = h1_list[i].textContent;
anchor.style.textDecoration = "none";
label.appendChild(anchor);
menu.appendChild(label);
}
document.body.insertBefore(menu_div, document.body.firstChild);
function createLI() {
var label = document.createElement("li");
label.style.margin = "0.8em 0em 0.8em 0em";
return label;
}
addEvent('resize', () => setMenyDivWidth());
function setMenyDivWidth() {
menu_div.style.width = document.body.getBoundingClientRect().left - 2*0.5*getEmDouble();
}
function getEmDouble() {
return parseFloat(getComputedStyle(document.body).fontSize);
}
}
function addScrollListener() {
addEvent("keydown", function(e) {
if (e.which == 74) {
window.scrollBy(0,20);
}
else if (e.which == 75) {
window.scrollBy(0,-20);
}
}, false);
}
function init() {
generateSectionsMenu();
addScrollListener()
}
function addEvent(eventStr, listener) {
if (window.addEventListener) { // W3C standard
window.addEventListener(eventStr, listener); // NB **not** 'onload'
} else if (window.attachEvent) { // Microsoft
window.attachEvent(eventStr, init);
}
}
if (document.readyState === "complete") {
init();
} else
if (window.addEventListener) { // W3C standard
window.addEventListener("load", init); // NB **not** 'onload'
} else if (window.attachEvent) { // Microsoft
window.attachEvent("onload", init);
}