-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefreshContentScript.js
69 lines (56 loc) · 2.32 KB
/
refreshContentScript.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
// store timestamp when user clicks button on task list
var taskButtons = document.querySelectorAll('.ewok-rater-task-option .button');
taskButtons.forEach(function(button) {
button.onclick = (element) => {
let timestamp = new Date().getTime();
chrome.storage.local.set({'clickTimestamp': timestamp});
}
});
/*
*Checks the web-page for work and sends message to the background script with its result.
*/
function workIsAvailable() {
let taskListText = document.querySelector('div.container').innerText;
return (taskListText.includes('Acquire if available') || taskListText.includes('Incomplete Tasks'))
}
function weeklyHoursCompleted() {
let taskListText = document.querySelector('div.container').innerText;
return (taskListText.includes('No tasks are currently available. Please try again after'))
}
function playWorkAlert() {
chrome.storage.sync.get(['refreshSoundSetting', 'refreshSoundVolumeSetting'], (settings) => {
let soundEnabled = settings['refreshSoundSetting'];
if (soundEnabled) {
let taskURL = chrome.runtime.getURL('sounds/taskaccept1.mp3');
let taskSound = new Audio(taskURL);
taskSound.volume = parseInt(settings['refreshSoundVolumeSetting']) / 100.0;
taskSound.addEventListener('canplaythrough', event => {
taskSound.play();
});
}
});
}
function loadRefreshTimer() {
chrome.storage.sync.get(['refreshSetting', 'minTime', 'maxTime', 'refreshTimerSetting'], (settings) => {
let refreshEnabled = settings['refreshSetting'];
if (refreshEnabled) {
let minTime = settings['minTime'];
let maxTime = settings['maxTime'];
let waitTime = Math.ceil( (Math.random() * (maxTime - minTime)) + minTime + 1);
setTimeout(() => {window.location.reload()}, waitTime * 1000);
// check if timer should be displayed in the extension icon
if (settings['refreshTimerSetting']) {
chrome.runtime.sendMessage({status : 'refresh-timer', time: waitTime}); // notify background script to display refresh timer
}
}
});
}
function initialize() {
if (weeklyHoursCompleted()) return;
if (workIsAvailable()) {
playWorkAlert();
} else {
loadRefreshTimer();
}
}
initialize();