-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
155 lines (126 loc) · 4.83 KB
/
background.js
File metadata and controls
155 lines (126 loc) · 4.83 KB
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
async function func() {
let k = ""
let c = ""
let to, tr
const b = "https://atenea.upc.edu/lib/ajax/service.php"
/* In seconds, threshold to trigger session update */
/* Max value = 3600 */
const th = 3600 / 2
/* In seconds, session update delay */
/* Max value = 3600 */
const ri = 15 * 60
/* Refresh cookie and sess keys */
const refresh_keys = () => {
k = M.cfg.sesskey
c = document.cookie.split("MoodleSessionate6=")[1].trim()
}
/* Get session time remaining */
/* Current session must be valid */
const core_session_time_remaining = async () => {
try {
const r = await (await fetch(b + "?sesskey=" + k, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Cookie": "MoodleSessionate6=" + c
},
body: JSON.stringify([{
index: 0,
methodname: "core_session_time_remaining",
args: {}
}])
})).json()
return r[0].data.timeremaining
} catch(e) {
return 0
}
}
/* Update current session */
/* Current session must be valid */
const core_session_touch = async () => {
try {
const r = await (await fetch(b + "?sesskey=" + k, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Cookie": "MoodleSessionate6=" + c
},
body: JSON.stringify([{
index: 0,
methodname: "core_session_touch",
args: {}
}])
})).json()
return { error: r[0].error }
} catch(e) {}
}
/* Inject active status indicator to Atenea */
const injectActiveStatus = () => {
let nav = document.querySelector("#page-wrapper > nav.navbar")
if (!nav.innerHTML.includes("Atenear")) {
let div = document.createElement("div")
div.id = 'atenear-id'
div.classList.add("mr-auto")
div.classList.add("ml-auto")
div.innerHTML = "<a role=\"menuitem\" target=\"_blank\" href=\"https://www.driescode.dev/atenear\" tabindex=\"-1\">Atenear</a><span style=\"font-size:12px;background-color:green;color:white;border-radius:40%;padding:3px;\">ON</span>"
div.style = "display:flex;justify-content:center;align-items:center;gap:8px;"
nav.insertBefore(div, nav.children.item(nav.children.length - 1))
}
}
/* Inject inactive status indicator to Atenea */
const injectInactiveStatus = () => {
let nav = document.querySelector("#page-wrapper > nav.navbar")
if (nav.innerHTML.includes("Atenear")) {
document.getElementById("atenear-id").remove()
}
let div = document.createElement("div")
div.id = 'atenear-id'
div.classList.add("mr-auto")
div.classList.add("ml-auto")
div.innerHTML = "<a role=\"menuitem\" target=\"_blank\" href=\"https://www.driescode.dev/atenear\" tabindex=\"-1\">Atenear</a><span style=\"font-size:12px;background-color:red;color:white;border-radius:40%;padding:3px;\">OFF</span> (Refresh Page)"
div.style = "display:flex;justify-content:center;align-items:center;gap:8px;"
nav.insertBefore(div, nav.children.item(nav.children.length - 1))
}
/* Refresh session wrapper to update status indicator */
const core_session_touch_wrapper = async () => {
if ((await core_session_touch()).error) {
injectInactiveStatus()
} else {
injectActiveStatus()
refresh_keys()
}
}
const log = (t) => { console.log("atenear > " + t) }
refresh_keys()
log("injected")
log("got auth c=" + c + ", k=" + k)
const t = await core_session_time_remaining()
/* Current session is already expired */
if (t <= 0) {
c = ""
k = ""
return
}
log("remaining_time=" + t)
if (t < th) {
log("below threshold, updating...")
await core_session_touch_wrapper()
}
log("safe session age value")
injectActiveStatus()
/* At this point, we can be sure we are in a safe session age value */
/* Create an interval which will trigger session update at given rate */
to = setInterval(async () => {
log("updating session...")
await core_session_touch_wrapper()
}, ri * 1000);
}
chrome.runtime.onMessage.addListener(async function(req, sender, sendResponse) {
if (req.type == "loaded") {
chrome.scripting.executeScript({
target: { tabId: sender.tab.id },
world: "MAIN",
func
}).catch(e => { console.error(e) })
}
});