-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteamup.com.js
85 lines (72 loc) · 2.48 KB
/
teamup.com.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
// When the "Today" button is clicked,
// and that button is disabled (not changing from a different day),
// Scroll to the current time in center of view
const addScrollToNowBehavior = (element) => {
element.addEventListener('click', () => {
scrollToNowLine()
})
}
const keyMap = {
i: '.sprite-event-rec-single',
u: '.sprite-event-rec-future',
a: '.sprite-event-rec-all',
t: '#title',
s: '.save-button .button',
6: '.date-from-value .datepicker .form-control.input',
7: '.date-from-value .timepicker input',
8: '.date-to-value .datepicker .input',
9: '.date-to-value .timepicker input',
}
const addKeyboardShortcuts = () => {
// TODO: to make this faster, create an input in the document with tab index -1
// and make it focusable with the shortcut
// then focus and clear its text on shortcut
// then bind an event when enter is pressed on that input that will trigger a focus change
// this way there is no waiting for the browser to render a prompt, and no visual terribleness
// <input type="text" style="width: 1px">
document.addEventListener('keydown', (event) => {
if (event.metaKey && event.shiftKey && event.ctrlKey && event.key === 'I') {
event.preventDefault()
const result = window.prompt('Focus?')
if (!result) {
return
}
const key = result.toLowerCase()
const selector = keyMap[key]
if (!selector) {
return
}
document.querySelector(selector).focus()
}
})
}
const scrollToNowLine = () => {
document.querySelector('.time-indicator-line').scrollIntoView({
behavior: 'instant',
block: 'center',
})
}
const clickTodayTwiceAfterDelay = (todayButton, delay) => {
setTimeout(() => {
todayButton.click()
}, delay)
setTimeout(() => {
todayButton.click()
}, delay + 30)
}
// NOTE: The observer is required to make sure the element is loaded
// before attempting to attach an event listener to that element
const observer = new MutationObserver((mutations, obs) => {
const todayButton = document.querySelector('.toolbar-button.today')
if (todayButton) {
addScrollToNowBehavior(todayButton)
addKeyboardShortcuts()
clickTodayTwiceAfterDelay(todayButton, 500)
obs.disconnect()
}
})
// executes immediately
observer.observe(document, {
childList: true,
subtree: true,
})