Skip to content

Commit c009a89

Browse files
authored
Merge pull request Stigmatoz#24 from danieltaub96/master
added support for Netflix tracking
2 parents 69419f1 + c886a64 commit c009a89

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<p><span class="arrow"></span> set notifications after a certain time has elapsed</p>
4040
<p><span class="arrow"></span> set a list of sites with no tracking</p>
4141
<p><span class="arrow"></span> enable time tracking on youtube.com</p>
42+
<p><span class="arrow"></span> enable time tracking on Netflix.com</p>
4243
</div>
4344
</div>
4445
<div id="stats" class="hide">

src/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"name": "Web Activity Time Tracker",
55
"short_name": "Web Time Tracker",
6-
"version": "1.3.4",
6+
"version": "1.5.0",
77
"minimum_chrome_version": "26",
88

99
"description": "Track and limit time your activity in the browser every day.",
@@ -27,6 +27,7 @@
2727
],
2828
"optional_permissions": [
2929
"https://www.youtube.com/*",
30+
"https://www.netflix.com/*",
3031
"notifications"
3132
],
3233
"offline_enabled": true,

src/options.html

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,17 @@
8282
<div class="margin-top-10">
8383
<input type="button" value="Export to CSV" id='exportToCsv'>
8484
</div>
85+
<!-- YouTube -->
8586
<div class="margin-top-10">
8687
<label class="block">Web Activity Time Tracker tracks your activity, if you active in browser (mouse
8788
or keyboard).
88-
In order for the extension to also track when you watch a video on YouTube.com, you need grant
89+
In order for the extension to also track when you watch a video on YouTube / Netflix, you need grant
8990
permission to
9091
access the sites.
9192
It is necessary that extension will get access to DOM (Document Object Model).</label>
93+
</div>
94+
<div class="margin-top-10">
95+
<label class="block">YouTube</label>
9296
<input class="margin-top-10" type="button" value="Grant permission" id='grantPermissionForYT'>
9397
<div class="inline-block">
9498
<div id='permissionSuccessedBlockForYT' hidden>
@@ -97,6 +101,17 @@
97101
</div>
98102
</div>
99103
</div>
104+
<!-- Netflix -->
105+
<div class="margin-top-10">
106+
<label class="block">Netflix</label>
107+
<input class="margin-top-10" type="button" value="Grant permission" id='grantPermissionForNetflix'>
108+
<div class="inline-block">
109+
<div id='permissionSuccessedBlockForNetflix' hidden>
110+
<img src="/icons/success.png" height="18" />
111+
<label>Permissions have been granted</label>
112+
</div>
113+
</div>
114+
</div>
100115
<div class="margin-top-10">
101116
<label>Data in memory use </label><label id='memoryUse'></label>
102117
</div>

src/scripts/background.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var setting_notification_list;
1616
var setting_notification_message;
1717

1818
var isHasPermissioForYouTube;
19+
var isHasPermissioForNetflix;
1920
var isHasPermissioForNotification;
2021

2122
function updateSummaryTime() {
@@ -145,25 +146,45 @@ function isVideoPlayedOnPage() {
145146
function checkDOM(state, activeUrl, tab, activeTab) {
146147
if (state === 'idle' && isDomainEquals(activeUrl, "youtube.com")) {
147148
trackForYT(mainTRacker, activeUrl, tab, activeTab);
149+
} else if (state === 'idle' && isDomainEquals(activeUrl, "netflix.com")) {
150+
trackForNetflix(mainTRacker, activeUrl, tab, activeTab);
148151
} else activity.closeIntervalForCurrentTab();
149152
}
150153

151154
function trackForYT(callback, activeUrl, tab, activeTab) {
152155
if (isHasPermissioForYouTube) {
153-
executeScript(callback, activeUrl, tab, activeTab);
156+
executeScriptYoutube(callback, activeUrl, tab, activeTab);
154157
} else {
155-
checkPermissionsForYT(executeScript, activity.closeIntervalForCurrentTab, callback, activeUrl, tab, activeTab);
158+
checkPermissionsForYT(executeScriptYoutube, activity.closeIntervalForCurrentTab, callback, activeUrl, tab, activeTab);
156159
}
157160
}
158161

159-
function executeScript(callback, activeUrl, tab, activeTab) {
162+
function trackForNetflix(callback, activeUrl, tab, activeTab) {
163+
if (isHasPermissioForNetflix) {
164+
executeScriptNetflix(callback, activeUrl, tab, activeTab);
165+
} else {
166+
checkPermissionsForNetflix(executeScriptNetflix, activity.closeIntervalForCurrentTab, callback, activeUrl, tab, activeTab);
167+
}
168+
}
169+
170+
function executeScriptYoutube(callback, activeUrl, tab, activeTab) {
160171
chrome.tabs.executeScript({ code: "var videoElement = document.getElementsByTagName('video')[0]; (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);" }, (results) => {
161172
if (results !== undefined && results[0] !== undefined && results[0] === true)
162173
callback(activeUrl, tab, activeTab);
163174
else activity.closeIntervalForCurrentTab();
164175
});
165176
}
166177

178+
function executeScriptNetflix(callback, activeUrl, tab, activeTab) {
179+
chrome.tabs.executeScript({ code: "var videoElement = document.getElementsByTagName('video')[0]; (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);" }, (results) => {
180+
if (results !== undefined && results[0] !== undefined && results[0] === true) {
181+
callback(activeUrl, tab, activeTab);
182+
} else {
183+
activity.closeIntervalForCurrentTab();
184+
}
185+
});
186+
}
187+
167188
function backgroundUpdateStorage() {
168189
if (tabs != undefined && tabs.length > 0)
169190
storage.saveTabs(tabs);
@@ -324,6 +345,7 @@ function loadAddDataFromStorage() {
324345

325346
function loadPermissions() {
326347
checkPermissionsForYT();
348+
checkPermissionsForNetflix();
327349
checkPermissionsForNotifications();
328350
}
329351

@@ -340,6 +362,19 @@ function checkPermissionsForYT(callbackIfTrue, callbackIfFalse, ...props) {
340362
});
341363
}
342364

365+
function checkPermissionsForNetflix(callbackIfTrue, callbackIfFalse, ...props) {
366+
chrome.permissions.contains({
367+
permissions: ['tabs'],
368+
origins: ["https://www.netflix.com/*"]
369+
}, function(result) {
370+
if (callbackIfTrue != undefined && result)
371+
callbackIfTrue(...props);
372+
if (callbackIfFalse != undefined && !result)
373+
callbackIfFalse();
374+
isHasPermissioForNetflix = result;
375+
});
376+
}
377+
343378
function checkPermissionsForNotifications(callback, ...props) {
344379
chrome.permissions.contains({
345380
permissions: ["notifications"]

src/scripts/settings.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ document.addEventListener('DOMContentLoaded', function () {
4646
document.getElementById('grantPermissionForYT').addEventListener('click', function () {
4747
grantPermissionForYT();
4848
});
49+
document.getElementById('grantPermissionForNetflix').addEventListener('click', function () {
50+
grantPermissionForNetflix();
51+
});
4952
document.getElementById('grantPermissionForNotifications').addEventListener('click', function () {
5053
grantPermissionForNotifications();
5154
});
@@ -107,6 +110,7 @@ function loadSettings() {
107110
document.getElementById('notifyMessage').value = mess;
108111
});
109112
checkPermissionsForYT();
113+
checkPermissionsForNetflix();
110114
checkPermissionsForNotifications();
111115
}
112116

@@ -121,6 +125,17 @@ function checkPermissionsForYT() {
121125
});
122126
}
123127

128+
function checkPermissionsForNetflix() {
129+
chrome.permissions.contains({
130+
permissions: ['tabs'],
131+
origins: ["https://www.netflix.com/*"]
132+
}, function (result) {
133+
if (result) {
134+
setUIForAnyPermissionForNetflix();
135+
}
136+
});
137+
}
138+
124139
function checkPermissionsForNotifications() {
125140
chrome.permissions.contains({
126141
permissions: ["notifications"]
@@ -156,6 +171,18 @@ function grantPermissionForYT() {
156171
});
157172
}
158173

174+
function grantPermissionForNetflix() {
175+
chrome.permissions.request({
176+
permissions: ['tabs'],
177+
origins: ["https://www.netflix.com/*"]
178+
}, function (granted) {
179+
// The callback argument will be true if the user granted the permissions.
180+
if (granted) {
181+
setUIForAnyPermissionForNetflix();
182+
}
183+
});
184+
}
185+
159186
function grantPermissionForNotifications() {
160187
chrome.permissions.request({
161188
permissions: ["notifications"]
@@ -172,6 +199,11 @@ function setUIForAnyPermissionForYT() {
172199
document.getElementById('grantPermissionForYT').setAttribute('disabled', 'true');
173200
}
174201

202+
function setUIForAnyPermissionForNetflix() {
203+
document.getElementById('permissionSuccessedBlockForNetflix').hidden = false;
204+
document.getElementById('grantPermissionForNetflix').setAttribute('disabled', 'true');
205+
}
206+
175207
function setUIForAnyPermissionForNotifications() {
176208
document.getElementById('permissionSuccessedBlockForNotifications').hidden = false;
177209
document.getElementById('grantPermissionForNotifications').setAttribute('disabled', 'true');

0 commit comments

Comments
 (0)