-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
174 lines (154 loc) · 5.32 KB
/
background.js
File metadata and controls
174 lines (154 loc) · 5.32 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// ***************************************************************
// variables
// ***************************************************************
let timePlayedToday = 0;
let timerInterval = null;
let activeTab = null;
let currentDate = new Date().toDateString();
let timeLimits = {};
const LINK_GAME = "repuls.io/";
const LINK_GAME_BETA = "repuls.io/beta/";
const DEFAULT_TIME_LIMITS = {
monday: 30,
tuesday: 30,
wednesday: 45,
thursday: 30,
friday: 45,
saturday: 45,
sunday: 0
};
const DEFAULT_STATE_DISPLAYING_USELESS_ELEMENTS = true;
// ***************************************************************
// functions
// ***************************************************************
function handleURLChange(tab) {
if (isRootRepulsIo(tab.url)) {
activeTab = tab.id;
startTimer();
}
else {
if (activeTab !== null) {
stopTimer();
activeTab = null;
}
}
}
function handleFocusChange(windowId) {
if (windowId === browser.windows.WINDOW_ID_NONE) {
stopTimer();
}
else {
browser.windows.get(windowId, { populate: true }).then((window) => {
const activeTab = window.tabs.find(tab => tab.active);
if(activeTab) handleURLChange(activeTab);
});
}
}
function getDailyTimeLimit() {
const daysOfWeek = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
const currentDay = daysOfWeek[new Date().getDay()];
return timeLimits[currentDay] || DEFAULT_TIME_LIMITS[currentDay];
}
function calculateRemainingTime() {
const timeLimitInSeconds = getDailyTimeLimit() * 60; //convert to seconds
return(Math.max(0, timeLimitInSeconds - timePlayedToday));
}
function isTimeLimitExceeded() {
return(calculateRemainingTime() <= 0);
}
function isRootRepulsIo(url) {
return(url === `https://${LINK_GAME}` || url === `http://${LINK_GAME}`);
}
function closeRepulsIoTab() {
browser.tabs.query({url: `*://${LINK_GAME}`}).then((tabs) => {
tabs.forEach((tab) => {
browser.tabs.remove(tab.id);
});
browser.tabs.create({ url: "blocked/blocked.html" });
});
}
// ***************************************************************
// timer functions
// ***************************************************************
function startTimer() {
if(!timerInterval) {
timerInterval = setInterval(() => {
timePlayedToday++;
browser.storage.local.set({ timeRemaining: calculateRemainingTime(), lastDate: currentDate });
if(isTimeLimitExceeded()) {
stopTimer();
closeRepulsIoTab();
}
}, 1000);
}
}
function stopTimer() {
if(timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}
}
function initStorage() {
// init > retrieve saved play time and limits
browser.storage.local.get(["contentVisible", "lastDate", "timeLimits", "timeRemaining"]).then((result) => {
if(result.lastDate === currentDate) {
timePlayedToday = getDailyTimeLimit() * 60 - result.timeRemaining || 0;
}
else { // new day
timePlayedToday = 0;
browser.storage.local.set({ lastDate: currentDate });
}
if(!result.timeLimits) {
browser.storage.local.set({ timeLimits: DEFAULT_TIME_LIMITS });
timeLimits = DEFAULT_TIME_LIMITS;
}
else timeLimits = result.timeLimits;
if (result.contentVisible === undefined) {
browser.storage.local.set({ contentVisible: DEFAULT_STATE_DISPLAYING_USELESS_ELEMENTS });
}
});
}
// ***************************************************************
// listeners and init
// ***************************************************************
initStorage();
// listerner for blocking after time limit
browser.webRequest.onBeforeRequest.addListener(
(details) => {
if(isTimeLimitExceeded() && isRootRepulsIo(details.url)) {
browser.tabs.update(details.tabId, {url: browser.runtime.getURL("blocked/blocked.html")});
return {cancel: true};
}
if(details.url == `https://${LINK_GAME_BETA}` || details.url == `http://${LINK_GAME_BETA}`) {
browser.tabs.update(details.tabId, {url: `https://${LINK_GAME}`});
return {cancel: true};
}
},
{urls: [`*://${LINK_GAME}*`, `*://${LINK_GAME_BETA}*`]},
["blocking"]
);
// https://developer.mozilla.org/en/docs/Mozilla/Add-ons/WebExtensions/API/tabs/onActivated
// listener for tab changes & tab updates
browser.tabs.onActivated.addListener((activeInfo) => {
browser.tabs.get(activeInfo.tabId).then((tab) => {
handleURLChange(tab);
});
});
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if(changeInfo.status === "complete") {
handleURLChange(tab);
}
});
browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
if(activeTab === tabId) {
stopTimer();
activeTab = null;
}
});
browser.windows.onFocusChanged.addListener(handleFocusChange);
// listener for time limits update
browser.runtime.onMessage.addListener((message) => {
if (message.action === "timeLimitsUpdated") {
initStorage();
}
});