Skip to content

Commit 99d2973

Browse files
committed
auto detect video sites and check its status
1 parent 94bce57 commit 99d2973

File tree

6 files changed

+55
-215
lines changed

6 files changed

+55
-215
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./vscode

src/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
<p><span class="arrow"></span> set daily limits for sites</p>
4040
<p><span class="arrow"></span> set notifications after a certain time has elapsed</p>
4141
<p><span class="arrow"></span> set a list of sites with no tracking</p>
42-
<p><span class="arrow"></span> enable time tracking on youtube.com</p>
43-
<p><span class="arrow"></span> enable time tracking on Netflix.com</p>
4442
</div>
4543
</div>
4644
<div id="stats" class="hide">

src/manifest.json

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
11
{
2-
"manifest_version": 2,
3-
4-
"name": "Web Activity Time Tracker",
5-
"short_name": "Web Time Tracker",
6-
"version": "1.5.4",
7-
"minimum_chrome_version": "26",
8-
9-
"description": "Track and limit time your activity in the browser every day.",
10-
11-
"options_page": "options.html",
12-
13-
"icons": {
14-
"16": "icons/16x16.png",
15-
"32": "icons/32x32.png",
16-
"48": "icons/48x48.png",
17-
"128": "icons/128x128.png"
18-
},
19-
"permissions": [
20-
"tabs",
21-
"activeTab",
22-
"storage",
23-
"idle",
24-
"chrome://favicon/*",
25-
"webNavigation",
26-
"unlimitedStorage"
2+
"manifest_version": 2,
3+
"name": "Web Activity Time Tracker",
4+
"short_name": "Web Time Tracker",
5+
"version": "1.5.4",
6+
"minimum_chrome_version": "26",
7+
"description": "Track and limit time your activity in the browser every day.",
8+
"options_page": "options.html",
9+
"icons": {
10+
"16": "icons/16x16.png",
11+
"32": "icons/32x32.png",
12+
"48": "icons/48x48.png",
13+
"128": "icons/128x128.png"
14+
},
15+
"permissions": [
16+
"tabs",
17+
"activeTab",
18+
"storage",
19+
"idle",
20+
"chrome://favicon/*",
21+
"webNavigation",
22+
"unlimitedStorage",
23+
"<all_urls>"
24+
],
25+
"optional_permissions": [
26+
"notifications"
27+
],
28+
"offline_enabled": true,
29+
"background": {
30+
"scripts": [
31+
"scripts/common.js",
32+
"scripts/storage.js",
33+
"scripts/activity.js",
34+
"scripts/tab.js",
35+
"scripts/timeInterval.js",
36+
"scripts/background.js",
37+
"scripts/restriction.js",
38+
"scripts/url.js"
2739
],
28-
"optional_permissions": [
29-
"https://www.youtube.com/*",
30-
"https://www.netflix.com/*",
31-
"notifications"
32-
],
33-
"offline_enabled": true,
34-
"background": {
35-
"scripts": ["scripts/common.js",
36-
"scripts/storage.js",
37-
"scripts/activity.js",
38-
"scripts/tab.js",
39-
"scripts/timeInterval.js",
40-
"scripts/background.js",
41-
"scripts/restriction.js",
42-
"scripts/url.js"],
43-
"persistent": false
44-
},
45-
"browser_action": {
46-
"default_popup": "index.html",
47-
"default_title": "Web Activity Time Tracker",
48-
"default_icon": "icons/48x48.png"
49-
}
40+
"persistent": false
41+
},
42+
"browser_action": {
43+
"default_popup": "index.html",
44+
"default_title": "Web Activity Time Tracker",
45+
"default_icon": "icons/48x48.png"
46+
}
5047
}

src/options.html

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,34 +110,6 @@
110110
<div class="notify warning" id='notify-restore-failed' hidden>
111111
Backup file is not valid
112112
</div>
113-
<!-- YouTube -->
114-
<div class="margin-top-10">
115-
<label class="block">Web Activity Time Tracker tracks your activity when you're active in
116-
the browser with your mouse or keyboard.
117-
In order for the extension to also track when you watch <span class="bold">a video on
118-
YouTube / Netflix</span>, you need grant it
119-
permission to access these sites.
120-
The extension needs permission to access to the DOM (Document Object Model) to tell if a video is currently playing.</label>
121-
</div>
122-
<div class="margin-top-10">
123-
<label class="bold site-name">YouTube</label>
124-
<input class="margin-top-10 permission-btn" type="button" value="Grant permission"
125-
id='grantPermissionForYT'>
126-
<div id='permissionSuccessedBlockForYT' hidden>
127-
<img src="/icons/success.png" height="18" />
128-
<label>Permissions have been granted</label>
129-
</div>
130-
</div>
131-
<!-- Netflix -->
132-
<div class="margin-top-10">
133-
<label class="bold site-name">Netflix</label>
134-
<input class="margin-top-10 permission-btn" type="button" value="Grant permission"
135-
id='grantPermissionForNetflix'>
136-
<div id='permissionSuccessedBlockForNetflix' hidden>
137-
<img src="/icons/success.png" height="18" />
138-
<label>Permissions have been granted</label>
139-
</div>
140-
</div>
141113
<div class="margin-top-10">
142114
<label>Data in memory use </label><label id='memoryUse'></label>
143115
</div>

src/scripts/background.js

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ var setting_dark_mode;
1818
var setting_notification_list;
1919
var setting_notification_message;
2020

21-
var isHasPermissioForYouTube;
22-
var isHasPermissioForNetflix;
2321
var isHasPermissioForNotification;
2422

2523
function updateSummaryTime() {
@@ -55,7 +53,7 @@ function backgroundCheck() {
5553
chrome.idle.queryState(parseInt(setting_interval_inactivity), function(state) {
5654
if (state === 'active') {
5755
mainTRacker(activeUrl, tab, activeTab);
58-
} else checkDOM(state, activeUrl, tab, activeTab);
56+
} else checkVideoPlaying(activeUrl, tab, activeTab);
5957
});
6058
}
6159
}
@@ -65,6 +63,7 @@ function backgroundCheck() {
6563
}
6664

6765
function mainTRacker(activeUrl, tab, activeTab) {
66+
console.log(activeUrl)
6867
if (activity.isLimitExceeded(activeUrl, tab) && !activity.wasDeferred(activeUrl)) {
6968
setBlockPageToCurrent(activeTab.url);
7069
}
@@ -138,55 +137,19 @@ function setBlockPageToCurrent(currentUrl) {
138137
});
139138
}
140139

141-
function isVideoPlayedOnPage() {
142-
var videoElement = document.getElementsByTagName('video')[0];
143-
if (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2) {
144-
return true;
145-
} else return false;
146-
}
147-
148-
function checkDOM(state, activeUrl, tab, activeTab) {
149-
if (state === 'idle' && activeUrl.isMatch("youtube.com")) {
150-
trackForYT(mainTRacker, activeUrl, tab, activeTab);
151-
} else if (state === 'idle' && activeUrl.isMatch("netflix.com")) {
152-
trackForNetflix(mainTRacker, activeUrl, tab, activeTab);
153-
} else activity.closeIntervalForCurrentTab();
154-
}
155-
156-
function trackForYT(callback, activeUrl, tab, activeTab) {
157-
if (isHasPermissioForYouTube) {
158-
executeScriptYoutube(callback, activeUrl, tab, activeTab);
159-
} else {
160-
checkPermissionsForYT(executeScriptYoutube, activity.closeIntervalForCurrentTab, callback, activeUrl, tab, activeTab);
161-
}
162-
}
163-
164-
function trackForNetflix(callback, activeUrl, tab, activeTab) {
165-
if (isHasPermissioForNetflix) {
166-
executeScriptNetflix(callback, activeUrl, tab, activeTab);
167-
} else {
168-
checkPermissionsForNetflix(executeScriptNetflix, activity.closeIntervalForCurrentTab, callback, activeUrl, tab, activeTab);
169-
}
170-
}
171-
172-
function executeScriptYoutube(callback, activeUrl, tab, activeTab) {
173-
chrome.tabs.executeScript({ code: "var videoElement = document.getElementsByTagName('video')[0]; (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);" }, (results) => {
140+
function checkVideoPlaying(activeUrl, tab, activeTab) {
141+
// wasm DOM has some problems with checking inside executeScript(), so make it always true
142+
chrome.tabs.executeScript({ code: `var videoEle = document.getElementsByTagName('video'); var wasmVideoEle = document.getElementsByTagName('bwp-video');
143+
var videoPlaying = [...videoEle].map(videoElement => (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2));
144+
var wasmVideoPlaying = [...wasmVideoEle].map(videoElement => true);
145+
[...videoPlaying, ...wasmVideoPlaying].includes(true)` },
146+
(results) => {
174147
if (results !== undefined && results[0] !== undefined && results[0] === true)
175-
callback(activeUrl, tab, activeTab);
148+
mainTRacker(activeUrl, tab, activeTab);
176149
else activity.closeIntervalForCurrentTab();
177150
});
178151
}
179152

180-
function executeScriptNetflix(callback, activeUrl, tab, activeTab) {
181-
chrome.tabs.executeScript({ code: "var videoElement = document.getElementsByTagName('video')[0]; (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);" }, (results) => {
182-
if (results !== undefined && results[0] !== undefined && results[0] === true) {
183-
callback(activeUrl, tab, activeTab);
184-
} else {
185-
activity.closeIntervalForCurrentTab();
186-
}
187-
});
188-
}
189-
190153
function backgroundUpdateStorage() {
191154
if (tabs != undefined && tabs.length > 0)
192155
storage.saveTabs(tabs);
@@ -371,37 +334,9 @@ function loadAddDataFromStorage() {
371334
}
372335

373336
function loadPermissions() {
374-
checkPermissionsForYT();
375-
checkPermissionsForNetflix();
376337
checkPermissionsForNotifications();
377338
}
378339

379-
function checkPermissionsForYT(callbackIfTrue, callbackIfFalse, ...props) {
380-
chrome.permissions.contains({
381-
permissions: ['tabs'],
382-
origins: ["https://www.youtube.com/*"]
383-
}, function(result) {
384-
if (callbackIfTrue != undefined && result)
385-
callbackIfTrue(...props);
386-
if (callbackIfFalse != undefined && !result)
387-
callbackIfFalse();
388-
isHasPermissioForYouTube = result;
389-
});
390-
}
391-
392-
function checkPermissionsForNetflix(callbackIfTrue, callbackIfFalse, ...props) {
393-
chrome.permissions.contains({
394-
permissions: ['tabs'],
395-
origins: ["https://www.netflix.com/*"]
396-
}, function(result) {
397-
if (callbackIfTrue != undefined && result)
398-
callbackIfTrue(...props);
399-
if (callbackIfFalse != undefined && !result)
400-
callbackIfFalse();
401-
isHasPermissioForNetflix = result;
402-
});
403-
}
404-
405340
function checkPermissionsForNotifications(callback, ...props) {
406341
chrome.permissions.contains({
407342
permissions: ["notifications"]

src/scripts/settings.js

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ document.addEventListener('DOMContentLoaded', function () {
6161
document.getElementById('rangeToDays').addEventListener('change', function () {
6262
storage.saveValue(SETTINGS_INTERVAL_RANGE, this.value);
6363
});
64-
document.getElementById('grantPermissionForYT').addEventListener('click', function () {
65-
grantPermissionForYT();
66-
});
67-
document.getElementById('grantPermissionForNetflix').addEventListener('click', function () {
68-
grantPermissionForNetflix();
69-
});
7064
document.getElementById('grantPermissionForNotifications').addEventListener('click', function () {
7165
grantPermissionForNotifications();
7266
});
@@ -130,32 +124,9 @@ function loadSettings() {
130124
storage.getValue(STORAGE_NOTIFICATION_MESSAGE, function (mess) {
131125
document.getElementById('notifyMessage').value = mess;
132126
});
133-
checkPermissionsForYT();
134-
checkPermissionsForNetflix();
135127
checkPermissionsForNotifications();
136128
}
137129

138-
function checkPermissionsForYT() {
139-
chrome.permissions.contains({
140-
permissions: ['tabs'],
141-
origins: ["https://www.youtube.com/*"]
142-
}, function (result) {
143-
if (result) {
144-
setUIForAnyPermissionForYT();
145-
}
146-
});
147-
}
148-
149-
function checkPermissionsForNetflix() {
150-
chrome.permissions.contains({
151-
permissions: ['tabs'],
152-
origins: ["https://www.netflix.com/*"]
153-
}, function (result) {
154-
if (result) {
155-
setUIForAnyPermissionForNetflix();
156-
}
157-
});
158-
}
159130

160131
function checkPermissionsForNotifications() {
161132
chrome.permissions.contains({
@@ -180,29 +151,6 @@ function viewBlackList(items) {
180151
}
181152
}
182153

183-
function grantPermissionForYT() {
184-
chrome.permissions.request({
185-
permissions: ['tabs'],
186-
origins: ["https://www.youtube.com/*"]
187-
}, function (granted) {
188-
// The callback argument will be true if the user granted the permissions.
189-
if (granted) {
190-
setUIForAnyPermissionForYT();
191-
}
192-
});
193-
}
194-
195-
function grantPermissionForNetflix() {
196-
chrome.permissions.request({
197-
permissions: ['tabs'],
198-
origins: ["https://www.netflix.com/*"]
199-
}, function (granted) {
200-
// The callback argument will be true if the user granted the permissions.
201-
if (granted) {
202-
setUIForAnyPermissionForNetflix();
203-
}
204-
});
205-
}
206154

207155
function grantPermissionForNotifications() {
208156
chrome.permissions.request({
@@ -215,17 +163,6 @@ function grantPermissionForNotifications() {
215163
});
216164
}
217165

218-
function setUIForAnyPermissionForYT() {
219-
document.getElementById('permissionSuccessedBlockForYT').hidden = false;
220-
document.getElementById('permissionSuccessedBlockForYT').classList.add('inline-block');
221-
document.getElementById('grantPermissionForYT').hidden = true;
222-
}
223-
224-
function setUIForAnyPermissionForNetflix() {
225-
document.getElementById('permissionSuccessedBlockForNetflix').hidden = false;
226-
document.getElementById('permissionSuccessedBlockForNetflix').classList.add('inline-block');
227-
document.getElementById('grantPermissionForNetflix').hidden = true;
228-
}
229166

230167
function setUIForAnyPermissionForNotifications() {
231168
document.getElementById('permissionSuccessedBlockForNotifications').hidden = false;

0 commit comments

Comments
 (0)