forked from ChrisSmith2/YouTube-Time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
261 lines (227 loc) · 6.57 KB
/
background.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
console.log("background running")
chrome.runtime.onInstalled.addListener(function (object) {
if (object.reason === chrome.runtime.OnInstalledReason.INSTALL) {
chrome.tabs.create({url: "/pages/options.html"});
}
});
var override = false;
var onYoutube = false;
var timeLeft = 1800;
var currentTab;
// chrome.storage.local.set({"lastDate":null}); //for debugging
checkReset();
chrome.storage.local.get({"override":override}, function(data) {
override = data.override;
});
chrome.storage.local.get({"timeLeft":timeLeft}, function(data) {
var time = data.timeLeft;
if (!Number.isNaN(time))
timeLeft = time;
else {
chrome.storage.local.get({"timeLimit":30}, function(data) {
timeLeft = data.timeLimit*60;
});
}
});
chrome.tabs.onActivated.addListener(function(activeInfo) {
checkReset();
chrome.tabs.get(activeInfo.tabId, function(tab){
currentTab = tab;
checkTab4YouTube(tab.url)
});
});
chrome.tabs.onUpdated.addListener(function(tabId, changedInfo, tab) {
checkReset();
if(tab.active && changedInfo.url){
currentTab = tab;
checkTab4YouTube(changedInfo.url)
}
});
window.setInterval(checkBrowserFocus, 1000);
function checkBrowserFocus(){
if(typeof timer != 'undefined') {
chrome.windows.getCurrent(function(browser){
if(browser.focused) {
if(!onYoutube) {
var getInfo = {populate: true};
chrome.windows.getCurrent(getInfo, function(window) {
for(var i = 0; i < window.tabs.length; i++) {
if(window.tabs[i].active) {
checkTab4YouTube(window.tabs[i].url)
}
}
});
}
} else if(onYoutube) {
console.log("bow stop")
onYoutube = false;
stopTime();
}
})
}
}
chrome.windows.onFocusChanged.addListener(function(windowId) {
checkReset();
if(windowId == chrome.windows.WINDOW_ID_NONE && typeof timer != 'undefined') {
onYoutube = false;
stopTime();
} else if(windowId != chrome.windows.WINDOW_ID_NONE) {
var getInfo = {populate: true};
chrome.windows.getCurrent(getInfo, function(window) {
for(var i = 0; i < window.tabs.length; i++) {
if(window.tabs[i].active) {
checkTab4YouTube(window.tabs[i].url)
}
}
});
}
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.msg == "override") {
override = request.value;
// console.log("override")
chrome.storage.local.set({"override":request.value, "tempOverride":true}, function() {
chrome.runtime.sendMessage({
msg: "goToSavedVideo"
});
});
} else if (request.msg == "checkReset") {
checkReset();
} else if (request.msg == "timeLimitUpdated") {
chrome.storage.local.get({"timeLeft":timeLeft}, function(data) {
timeLeft = data.timeLeft;
});
}
});
function isYoutube(url) {
// regex from https://stackoverflow.com/a/32730577
return url.match(/(https?:\/\/(.+?\.)?youtube\.com(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?)/)
}
function isYoutubeVideo(url) {
return url.match(/(https?:\/\/(.+?\.)?youtube\.com\/watch([A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?)/)
}
function updateTime() {
if (timeLeft > 0) {
timeLeft--;
} else {
clearInterval(timer);
blockRedirect();
}
chrome.browserAction.setBadgeText({"text": formatTime(timeLeft)});
chrome.storage.local.set({"timeLeft":timeLeft});
var views = chrome.extension.getViews({ type: "popup" });
if (views.length != 0) {
chrome.runtime.sendMessage({
msg: "updateTime",
time: timeLeft
});
}
}
function startTime() {
// console.log("start", timeLeft)
chrome.browserAction.setBadgeText({"text": formatTime(timeLeft)});
timer = setInterval(updateTime, 1000);
}
function stopTime() {
// console.log("stopped", timeLeft)
clearInterval(timer);
chrome.browserAction.setBadgeText({"text": ""});
}
function formatTime(totalSeconds) {
var hours = Math.floor(totalSeconds / 3600);
totalSeconds -= hours*3600;
var minutes = Math.floor(totalSeconds / 60);
totalSeconds -= minutes*60;
var seconds = Math.floor(totalSeconds);
var result = "";
if (hours > 0) {
result += hours + ":";
}
if (minutes > 0) {
result += minutes + ":";
} else {
result += "0:";
}
if (seconds < 10) {
result += "0";
}
result += seconds;
return result;
}
function blockRedirect() {
// console.log("blockRedirect")
// console.log("tabs[0].url: " + currentTab)
// console.log(isYoutubeVideo(tabs[0].url))
if (isYoutubeVideo(currentTab.url)) {
// request videoURL with time
chrome.tabs.sendMessage(currentTab.id, {msg:"saveVideoURL"}, function(response){
// console.log("response: " + response)
chrome.storage.local.set({"savedVideoURL": response}, function() {
chrome.tabs.update(currentTab.id, {url: "/pages/blocked.html"});
});
});
} else {
// if not on a youtube video
chrome.storage.local.set({"savedVideoURL": currentTab.url}, function() {
chrome.tabs.update(currentTab.id, {url: "/pages/blocked.html"});
});
}
}
function checkReset() {
chrome.storage.local.get("lastDate", function(data) {
var today = new Date();
if(!data.lastDate || (today.getDate().toString() != data.lastDate && today.getHours() >= 6)) {
chrome.storage.local.get({"timeLimit":30}, function(data) {
chrome.storage.local.set({"lastDate":today.getDate().toString(), "override":false, "timeLeft":data.timeLimit*60}, function () {
chrome.runtime.sendMessage({
msg: "checkDone"
});
});
override = false;
timeLeft = data.timeLimit*60;
});
} else {
chrome.runtime.sendMessage({
msg: "checkDone"
});
}
});
}
function checkOverride(url) {
// console.log("check override")
// checks if youtube page navigated to has been allowed
// if not, user will be redirected back to block page
// allows user to override and go back to most recent video
// but not go to any other videos
chrome.storage.local.get({savedVideoURL:"", tempOverride:false}, function(data) {
// console.log("current url: " + url);
// console.log("allowed url: " + data.savedVideoURL);
if (urlNoTime(url) != urlNoTime(data.savedVideoURL) || !data.tempOverride) {
// if url doesn't match one that was saved or tempoverride isn't true
chrome.storage.local.set({savedVideoURL: currentTab.url, tempOverride: false}, function() {
chrome.tabs.update(currentTab.id, {url: "/pages/blocked.html"});
});
}
});
}
function urlNoTime(url) {
let arr = url.split("&t=");
if (arr.length >= 1) {
return arr[0];
}
return null
}
function checkTab4YouTube(url) {
// console.log("checkTab4YouTube")
if (isYoutube(url) && !onYoutube) {
if (!override) {
onYoutube = true;
startTime();
} else {
checkOverride(url);
}
} else if (!isYoutube(url) && onYoutube && !override) {
onYoutube = false;
stopTime();
}
}