Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<p><span class="arrow"></span> set notifications after a certain time has elapsed</p>
<p><span class="arrow"></span> set a list of sites with no tracking</p>
<p><span class="arrow"></span> enable time tracking on youtube.com</p>
<p><span class="arrow"></span> enable time tracking on Netflix.com</p>
</div>
</div>
<div id="stats" class="hide">
Expand Down
3 changes: 2 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "Web Activity Time Tracker",
"short_name": "Web Time Tracker",
"version": "1.3.4",
"version": "1.5.0",
"minimum_chrome_version": "26",

"description": "Track and limit time your activity in the browser every day.",
Expand All @@ -27,6 +27,7 @@
],
"optional_permissions": [
"https://www.youtube.com/*",
"https://www.netflix.com/*",
"notifications"
],
"offline_enabled": true,
Expand Down
17 changes: 16 additions & 1 deletion src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@
<div class="margin-top-10">
<input type="button" value="Export to CSV" id='exportToCsv'>
</div>
<!-- YouTube -->
<div class="margin-top-10">
<label class="block">Web Activity Time Tracker tracks your activity, if you active in browser (mouse
or keyboard).
In order for the extension to also track when you watch a video on YouTube.com, you need grant
In order for the extension to also track when you watch a video on YouTube / Netflix, you need grant
permission to
access the sites.
It is necessary that extension will get access to DOM (Document Object Model).</label>
</div>
<div class="margin-top-10">
<label class="block">YouTube</label>
<input class="margin-top-10" type="button" value="Grant permission" id='grantPermissionForYT'>
<div class="inline-block">
<div id='permissionSuccessedBlockForYT' hidden>
Expand All @@ -97,6 +101,17 @@
</div>
</div>
</div>
<!-- Netflix -->
<div class="margin-top-10">
<label class="block">Netflix</label>
<input class="margin-top-10" type="button" value="Grant permission" id='grantPermissionForNetflix'>
<div class="inline-block">
<div id='permissionSuccessedBlockForNetflix' hidden>
<img src="/icons/success.png" height="18" />
<label>Permissions have been granted</label>
</div>
</div>
</div>
<div class="margin-top-10">
<label>Data in memory use </label><label id='memoryUse'></label>
</div>
Expand Down
41 changes: 38 additions & 3 deletions src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var setting_notification_list;
var setting_notification_message;

var isHasPermissioForYouTube;
var isHasPermissioForNetflix;
var isHasPermissioForNotification;

function updateSummaryTime() {
Expand Down Expand Up @@ -145,25 +146,45 @@ function isVideoPlayedOnPage() {
function checkDOM(state, activeUrl, tab, activeTab) {
if (state === 'idle' && isDomainEquals(activeUrl, "youtube.com")) {
trackForYT(mainTRacker, activeUrl, tab, activeTab);
} else if (state === 'idle' && isDomainEquals(activeUrl, "netflix.com")) {
trackForNetflix(mainTRacker, activeUrl, tab, activeTab);
} else activity.closeIntervalForCurrentTab();
}

function trackForYT(callback, activeUrl, tab, activeTab) {
if (isHasPermissioForYouTube) {
executeScript(callback, activeUrl, tab, activeTab);
executeScriptYoutube(callback, activeUrl, tab, activeTab);
} else {
checkPermissionsForYT(executeScript, activity.closeIntervalForCurrentTab, callback, activeUrl, tab, activeTab);
checkPermissionsForYT(executeScriptYoutube, activity.closeIntervalForCurrentTab, callback, activeUrl, tab, activeTab);
}
}

function executeScript(callback, activeUrl, tab, activeTab) {
function trackForNetflix(callback, activeUrl, tab, activeTab) {
if (isHasPermissioForNetflix) {
executeScriptNetflix(callback, activeUrl, tab, activeTab);
} else {
checkPermissionsForNetflix(executeScriptNetflix, activity.closeIntervalForCurrentTab, callback, activeUrl, tab, activeTab);
}
}

function executeScriptYoutube(callback, activeUrl, tab, activeTab) {
chrome.tabs.executeScript({ code: "var videoElement = document.getElementsByTagName('video')[0]; (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);" }, (results) => {
if (results !== undefined && results[0] !== undefined && results[0] === true)
callback(activeUrl, tab, activeTab);
else activity.closeIntervalForCurrentTab();
});
}

function executeScriptNetflix(callback, activeUrl, tab, activeTab) {
chrome.tabs.executeScript({ code: "var videoElement = document.getElementsByTagName('video')[0]; (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);" }, (results) => {
if (results !== undefined && results[0] !== undefined && results[0] === true) {
callback(activeUrl, tab, activeTab);
} else {
activity.closeIntervalForCurrentTab();
}
});
}

function backgroundUpdateStorage() {
if (tabs != undefined && tabs.length > 0)
storage.saveTabs(tabs);
Expand Down Expand Up @@ -324,6 +345,7 @@ function loadAddDataFromStorage() {

function loadPermissions() {
checkPermissionsForYT();
checkPermissionsForNetflix();
checkPermissionsForNotifications();
}

Expand All @@ -340,6 +362,19 @@ function checkPermissionsForYT(callbackIfTrue, callbackIfFalse, ...props) {
});
}

function checkPermissionsForNetflix(callbackIfTrue, callbackIfFalse, ...props) {
chrome.permissions.contains({
permissions: ['tabs'],
origins: ["https://www.netflix.com/*"]
}, function(result) {
if (callbackIfTrue != undefined && result)
callbackIfTrue(...props);
if (callbackIfFalse != undefined && !result)
callbackIfFalse();
isHasPermissioForNetflix = result;
});
}

function checkPermissionsForNotifications(callback, ...props) {
chrome.permissions.contains({
permissions: ["notifications"]
Expand Down
32 changes: 32 additions & 0 deletions src/scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ document.addEventListener('DOMContentLoaded', function () {
document.getElementById('grantPermissionForYT').addEventListener('click', function () {
grantPermissionForYT();
});
document.getElementById('grantPermissionForNetflix').addEventListener('click', function () {
grantPermissionForNetflix();
});
document.getElementById('grantPermissionForNotifications').addEventListener('click', function () {
grantPermissionForNotifications();
});
Expand Down Expand Up @@ -107,6 +110,7 @@ function loadSettings() {
document.getElementById('notifyMessage').value = mess;
});
checkPermissionsForYT();
checkPermissionsForNetflix();
checkPermissionsForNotifications();
}

Expand All @@ -121,6 +125,17 @@ function checkPermissionsForYT() {
});
}

function checkPermissionsForNetflix() {
chrome.permissions.contains({
permissions: ['tabs'],
origins: ["https://www.netflix.com/*"]
}, function (result) {
if (result) {
setUIForAnyPermissionForNetflix();
}
});
}

function checkPermissionsForNotifications() {
chrome.permissions.contains({
permissions: ["notifications"]
Expand Down Expand Up @@ -156,6 +171,18 @@ function grantPermissionForYT() {
});
}

function grantPermissionForNetflix() {
chrome.permissions.request({
permissions: ['tabs'],
origins: ["https://www.netflix.com/*"]
}, function (granted) {
// The callback argument will be true if the user granted the permissions.
if (granted) {
setUIForAnyPermissionForNetflix();
}
});
}

function grantPermissionForNotifications() {
chrome.permissions.request({
permissions: ["notifications"]
Expand All @@ -172,6 +199,11 @@ function setUIForAnyPermissionForYT() {
document.getElementById('grantPermissionForYT').setAttribute('disabled', 'true');
}

function setUIForAnyPermissionForNetflix() {
document.getElementById('permissionSuccessedBlockForNetflix').hidden = false;
document.getElementById('grantPermissionForNetflix').setAttribute('disabled', 'true');
}

function setUIForAnyPermissionForNotifications() {
document.getElementById('permissionSuccessedBlockForNotifications').hidden = false;
document.getElementById('grantPermissionForNotifications').setAttribute('disabled', 'true');
Expand Down