Skip to content

Commit b4f7b21

Browse files
committed
added netflix video tracking
1 parent 69419f1 commit b4f7b21

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
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).
@@ -97,6 +98,22 @@
9798
</div>
9899
</div>
99100
</div>
101+
<!-- Netflix -->
102+
<div class="margin-top-10">
103+
<label class="block">Web Activity Time Tracker tracks your activity, if you active in browser (mouse
104+
or keyboard).
105+
In order for the extension to also track when you watch a video on Netflix.com, you need grant
106+
permission to
107+
access the sites.
108+
It is necessary that extension will get access to DOM (Document Object Model).</label>
109+
<input class="margin-top-10" type="button" value="Grant permission" id='grantPermissionForNetflix'>
110+
<div class="inline-block">
111+
<div id='permissionSuccessedBlockForNetflix' hidden>
112+
<img src="/icons/success.png" height="18" />
113+
<label>Permissions have been granted</label>
114+
</div>
115+
</div>
116+
</div>
100117
<div class="margin-top-10">
101118
<label>Data in memory use </label><label id='memoryUse'></label>
102119
</div>

src/scripts/background.js

Lines changed: 40 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,47 @@ 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+
console.log("netglix is playing")
182+
callback(activeUrl, tab, activeTab);
183+
} else {
184+
console.log("netflix paused")
185+
activity.closeIntervalForCurrentTab();
186+
}
187+
});
188+
}
189+
167190
function backgroundUpdateStorage() {
168191
if (tabs != undefined && tabs.length > 0)
169192
storage.saveTabs(tabs);
@@ -324,6 +347,7 @@ function loadAddDataFromStorage() {
324347

325348
function loadPermissions() {
326349
checkPermissionsForYT();
350+
checkPermissionsForNetflix();
327351
checkPermissionsForNotifications();
328352
}
329353

@@ -340,6 +364,19 @@ function checkPermissionsForYT(callbackIfTrue, callbackIfFalse, ...props) {
340364
});
341365
}
342366

367+
function checkPermissionsForNetflix(callbackIfTrue, callbackIfFalse, ...props) {
368+
chrome.permissions.contains({
369+
permissions: ['tabs'],
370+
origins: ["https://www.netflix.com/*"]
371+
}, function(result) {
372+
if (callbackIfTrue != undefined && result)
373+
callbackIfTrue(...props);
374+
if (callbackIfFalse != undefined && !result)
375+
callbackIfFalse();
376+
isHasPermissioForNetflix = result;
377+
});
378+
}
379+
343380
function checkPermissionsForNotifications(callback, ...props) {
344381
chrome.permissions.contains({
345382
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)