-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSS Caffeinate.js
57 lines (48 loc) · 1.68 KB
/
JSS Caffeinate.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
// ==UserScript==
// @name JSS Caffeinate
// @version 0.3
// @description Simulates mousedown events every few minutes, to activate keepalive call of JSS
// @match https://<yourJssUrlWithoutPort>/*
// @updateURL https://github.com/fveja/JSSCaffeinate/raw/main/JSS%20Caffeinate.js
// @noframes
// Florin Veja 2022
// ==/UserScript==
console.log('Started JSS Caffeinate. Running on ' + location.href)
// delay, in milliseconds, before we send mousedown event (120000 milliseconds = 2 min)
const delay = 120000
var last = Date.now()
const name = "JSS Caffeinate"
// set to true for debug console output
const jssCaffeinateDebug = false
// function to send us back to the
const login = () => {
debug("logging in")
location = location.origin
}
const debug = (m) => {
typeof jssCaffeinateDebug == 'boolean' &&
jssCaffeinateDebug &&
console.log(name + " debug: " + m)
}
const authExpiration = () => {
return JSON.parse(localStorage.authToken).expires - Date.now()
}
const getDelay = (max) => {
const min = 10000
return Math.random() * (max - min) + min
}
// if we are on the logout page, log back in
// randomize to not create a race
if (location.pathname == '/logout.html') {
setTimeout(login(), getDelay(10000))
}
const café = setInterval(() => {
var start = Date.now()
const mousedown = new Event('mousedown')
debug('JSS Caffeinate keepalive ' + start)
document.dispatchEvent(mousedown)
debug("authToken expires in " + authExpiration())
debug(last + " - " + start + "=" + (start - last))
last = start
if (authExpiration() < 1) { login() }
}, delay);