-
Notifications
You must be signed in to change notification settings - Fork 22
/
script.js
118 lines (97 loc) · 4.2 KB
/
script.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
const NAME = "John";
const WELCOME_MESSAGE_TEMPLATE = ["night", "morning", "afternoon", "evening"];
// All shortcuts are in a `SHORTCUT_STARTER+shortcutKey` format.
// So, for example, pressing `tab+q` would redirect you to https://google.com/?q=q
const SHORTCUT_STARTER = 'tab'
// How much time (in milliseconds) you have to press shortcutKey after pressing SHORTCUT_STARTER.
// Also change --SHORTCUT_TIMEOUT in styles.css if you change this option.
const SHORTCUT_TIMEOUT = 1500;
// The groups of links are generated from this object. Edit it to edit the page's contents.
// shortcutKey must hold an all-lowercase single button. Theoretically should work with values like `esc` and `f1`,
// but intended to be used with just regular latin letters.
const MASTER_MAP = [
{
"groupName": "Studies",
"items":[
{"name": "Item A", "shortcutKey": "q", "url": "https://google.com/?q=q"},
{"name": "Item B", "shortcutKey": "w", "url": "https://google.com/?q=w"},
{"name": "Item C", "shortcutKey": "e", "url": "https://google.com/?q=e"}
]
},
{
"groupName": "Work",
"items":[
{"name": "Item D", "shortcutKey": "a", "url": "https://google.com/?q=a"},
{"name": "Item E", "shortcutKey": "s", "url": "https://google.com/?q=s"},
{"name": "Item F", "shortcutKey": "d", "url": "https://google.com/?q=d"}
]
},
{
"groupName": "Personal",
"items":[
{"name": "Item I", "shortcutKey": "z", "url": "https://google.com/?q=z"},
{"name": "Item J", "shortcutKey": "x", "url": "https://google.com/?q=x"},
{"name": "Item K", "shortcutKey": "c", "url": "https://google.com/?q=c"}
]
}
]
let $container = document.getElementById("content");
let getUrl = {};
let $shortcutDisplayList = document.getElementsByClassName("shortcut");
let listeningForShortcut = false;
let listenerTimeout;
function setupWelcomeMessage(){
let curHours = new Date().getHours();
curHours = Math.floor(curHours/6); // Simply dividing current hours by 6 proves to be a good enough aproximation.
if (curHours == 4) curHours = 3;
let welcome = "Good " + WELCOME_MESSAGE_TEMPLATE[curHours] + ", " + NAME;
document.getElementById("welcome-string").innerHTML = welcome;
}
function setupGroups(){
for (let i = 0; i < MASTER_MAP.length; i++){
let curGroupData = MASTER_MAP[i];
let group = document.createElement("div");
group.className = "group";
$container.appendChild(group);
let header = document.createElement("h1");
header.innerHTML = curGroupData.groupName;
group.appendChild(header);
for (let j = 0; j < curGroupData.items.length; j++){
let curItemData = curGroupData.items[j];
let pContainer = document.createElement("p");
group.appendChild(pContainer);
let link = document.createElement("a");
link.innerHTML = curItemData.name;
link.setAttribute("href", curItemData.url);
pContainer.appendChild(link);
let shortcutDisplay = document.createElement("span");
shortcutDisplay.innerHTML = curItemData.shortcutKey;
shortcutDisplay.className = "shortcut";
shortcutDisplay.style.animation = "none";
pContainer.appendChild(shortcutDisplay);
getUrl[curItemData.shortcutKey] = curItemData.url
}
}
}
function shortcutListener(e) {
let key = e.key.toLowerCase();
if (listeningForShortcut && getUrl.hasOwnProperty(key)){
window.location = getUrl[key];
}
if (key === SHORTCUT_STARTER) {
clearTimeout(listenerTimeout);
listeningForShortcut = true;
// Animation reset
for (let i = 0; i < $shortcutDisplayList.length; i++){
$shortcutDisplayList[i].style.animation = "none";
setTimeout(function() { $shortcutDisplayList[i].style.animation = ''; }, 10);
}
listenerTimeout = setTimeout(function(){ listeningForShortcut = false; }, SHORTCUT_TIMEOUT);
}
}
function main(){
setupWelcomeMessage();
setupGroups();
document.addEventListener('keyup', shortcutListener, false);
}
main();