-
Notifications
You must be signed in to change notification settings - Fork 1
/
unlockpage.js
66 lines (60 loc) · 2.07 KB
/
unlockpage.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
let countdown;
let running = true;
let query;
let timeout;
async function init() {
let options = await getOptions();
countdown = options.frictionTime;
query = parseQuery(window.location.search);
document.getElementById('url').innerText = query.url;
document.getElementById('unlockbutton').addEventListener('click', () => unlock());
document.getElementById('unlockbutton').disabled = true;
document.getElementById('countdown').innerText = '' + countdown;
window.addEventListener('blur', () => {
countdown = options.frictionTime;
document.getElementById('countdown').innerText = '' + countdown;
document.getElementById('unlockbutton').disabled = true;
running = false;
});
window.addEventListener('focus', () => {
running = true;
countdown = options.frictionTime;
clearTimeout(timeout);
timeout = setTimeout(() => doCountdown(), 1000);
});
timeout = setTimeout(() => doCountdown(), 1000);
}
async function doCountdown() {
if (!running) {
let options = await getOptions();
countdown = options.frictionTime;
document.getElementById('countdown').innerText = '' + countdown;
document.getElementById('unlockbutton').disabled = true;
return;
}
countdown--;
if (countdown <= 0) {
document.getElementById('countdown').innerText = '0';
document.getElementById('unlockbutton').disabled = false;
} else {
document.getElementById('countdown').innerText = '' + countdown;
setTimeout(() => doCountdown(), 1000);
}
}
function unlock() {
chrome.storage.sync.set({'lastunlock': Date.now() / 1000}, () => {
getOptions().then((options) => {
window.location = query.url;
});
});
}
function parseQuery(search) {
let vars = search.substring(1).split('&');
let result = {};
for (let i=0; i<vars.length; i++) {
let pair = vars[i].split('=');
result[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return result;
}
window.onload = () => init();