-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
179 lines (141 loc) · 4.58 KB
/
content.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
173
174
175
176
177
178
179
// På Vei - Input Focus Improvements
// Copyright © 2022 Adaline Valentina Simonian
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const getMainContainer = () => document.querySelector('app-main-app main')
const getLessonElement = () =>
document.evaluate(
'//*[contains(@class, "oppgavesamling-main")]/*[starts-with(local-name(), "app-")]',
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue
const getFirstFocusable = () =>
document
.querySelector('.r4-exercise__task')
?.querySelector(
'a[href], button, input, textarea, select, details, [tabindex]:not([tabindex="-1"])'
)
const getPrimaryButton = () => {
const buttons = document.querySelector('.r4-exercise-footer__buttons')
if (!buttons) {
return null
}
let primaryButton = null
let tryAgainButton = null
let wasWrong = false
for (const button of buttons.children) {
// Match the try again button
if (button.textContent?.includes('Prøv igjen')) {
tryAgainButton = button
}
// Match the see solution button. If it exists, the user was wrong.
if (button.textContent?.includes('Vis fasit')) {
wasWrong = true
}
// Match the next button (primary button)
if (button.classList.contains('r4-button--primary')) {
primaryButton = button
}
}
// If no lesson navigation buttons are found, return null. This is the case
// when the user has not started typing or making selections yet.
if (!primaryButton && !tryAgainButton && !wasWrong) {
return null
}
// If the user was wrong, return the try again button if it exists, otherwise
// return the primary button
if (wasWrong) {
return tryAgainButton ?? primaryButton
}
// If the user was right, return the primary button if it exists.
if (primaryButton) {
return primaryButton
}
// If there is no primary button, return the next or summary button for the
// chapter
const lessonNavButtons = document.querySelectorAll(
'.r4-lesson-navigation-bottom button'
)
for (const button of lessonNavButtons) {
// If the button is the next or summary button for the chapter, return it
if (
button.textContent?.includes('Neste') ||
button.textContent?.includes('Oppsummering')
) {
return button
}
}
return null
}
const mainContainerObserver = new MutationObserver(() =>
requestAnimationFrame(applyHooks)
)
const mainContainerObserverConfig = { childList: true, subtree: true }
// Wait for the main container to be added to the app. When it is, start
// observing it.
const waitForMainContainer = () => {
const mainContainer = getMainContainer()
if (!mainContainer) {
setTimeout(waitForMainContainer, 100)
return
}
mainContainerObserver.observe(mainContainer, mainContainerObserverConfig)
}
waitForMainContainer()
let lastLesson = null
let lastFocusable = null
let lastDisabled = null
function applyHooks() {
const lesson = getLessonElement()
if (!lesson) {
return
}
// console.debug('applyHooks')
// console.debug('lessonElement', lesson)
if (lastLesson !== lesson) {
lastLesson = lesson
// console.debug('new lesson')
}
const focusable = getFirstFocusable()
if (
focusable &&
(lastFocusable !== focusable || lastDisabled !== focusable.disabled)
) {
lastFocusable = focusable
lastDisabled = focusable.disabled
// console.debug('focusable', focusable)
// If the element is disabled, focus the primary button instead
if (focusable.disabled) {
getPrimaryButton()?.focus?.()
return
}
focusable.focus()
}
}
// Add event listener for the enter key to click the primary button
document.addEventListener('keydown', e => {
// Don't run if a button or a link is focused
if (
document.activeElement?.tagName === 'BUTTON' ||
(document.activeElement?.tagName === 'A' && document.activeElement?.href)
) {
return
}
if (e.key === 'Enter') {
getPrimaryButton()?.click?.()
}
})
console.debug('init extension')