Skip to content

Commit 9d37278

Browse files
committed
Add optionally permission for notification
1 parent c1ab382 commit 9d37278

File tree

4 files changed

+134
-52
lines changed

4 files changed

+134
-52
lines changed

src/manifest.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

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

9-
"description": "Track and limit time your activity in the browser.",
9+
"description": "Track and limit time your activity in the browser every day.",
1010

1111
"options_page": "options.html",
1212

@@ -23,11 +23,11 @@
2323
"idle",
2424
"chrome://favicon/*",
2525
"webNavigation",
26-
"unlimitedStorage",
27-
"notifications"
26+
"unlimitedStorage"
2827
],
2928
"optional_permissions": [
30-
"https://www.youtube.com/*"
29+
"https://www.youtube.com/*",
30+
"notifications"
3131
],
3232
"offline_enabled": true,
3333
"background": {

src/options.html

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@
8585
<div class="margin-top-10">
8686
<label class="block">Web Activity Time Tracker tracks your activity, if you active in browser (mouse
8787
or keyboard).
88-
In order for the extension to also track when you watch a video on YouTube.com, you must grant
89-
permissions to
88+
In order for the extension to also track when you watch a video on YouTube.com, you need grant
89+
permission to
9090
access the sites.
9191
It is necessary that extension will get access to DOM (Document Object Model).</label>
92-
<input class="margin-top-10" type="button" value="Grant permission" id='grantPermission'>
92+
<input class="margin-top-10" type="button" value="Grant permission" id='grantPermissionForYT'>
9393
<div class="inline-block">
94-
<div id='permissionSuccessedBlock' hidden>
94+
<div id='permissionSuccessedBlockForYT' hidden>
9595
<img src="/icons/success.png" height="18" />
9696
<label>Permissions have been granted</label>
9797
</div>
@@ -152,6 +152,17 @@
152152
</div>
153153
</div>
154154
</div>
155+
<div class="margin-top-10">
156+
<label class="block">In order to Web Activity Time Tracker showed notifications every time you spend a specified time interval on the site, you need grant permission to access for notifications.
157+
</label>
158+
<input class="margin-top-10" type="button" value="Grant permission" id='grantPermissionForNotifications'>
159+
<div class="inline-block">
160+
<div id='permissionSuccessedBlockForNotifications' hidden>
161+
<img src="/icons/success.png" height="18" />
162+
<label>Permissions have been granted</label>
163+
</div>
164+
</div>
165+
</div>
155166
<div class="notify warning notify-width-300" id='notifyForNotifyList' hidden>
156167
The site is already in the notify list
157168
</div>

src/scripts/background.js

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ var setting_view_in_badge;
1515
var setting_notification_list;
1616
var setting_notification_message;
1717

18+
var isHasPermissioForYouTube;
19+
var isHasPermissioForNotification;
20+
1821
function updateSummaryTime() {
1922
setInterval(backgroundCheck, SETTINGS_INTERVAL_CHECK_DEFAULT);
2023
}
@@ -64,15 +67,11 @@ function mainTRacker(activeUrl, tab, activeTab) {
6467
}
6568
if (!activity.isInBlackList(activeUrl)) {
6669
if (activity.isNeedNotifyView(activeUrl, tab)) {
67-
chrome.notifications.clear('watt-site-notification');
68-
chrome.notifications.create(
69-
'watt-site-notification', {
70-
type: 'basic',
71-
iconUrl: 'icons/128x128.png',
72-
title: "Web Activity Time Tracker",
73-
contextMessage: activeUrl + ' ' + convertShortSummaryTimeToString(tab.getTodayTime()),
74-
message: setting_notification_message
75-
});
70+
if (isHasPermissioForNotification) {
71+
showNotification(activeUrl, tab);
72+
} else {
73+
checkPermissionsForNotifications(showNotification, activeUrl, tab);
74+
}
7675
}
7776
tab.incSummaryTime();
7877
}
@@ -93,6 +92,18 @@ function mainTRacker(activeUrl, tab, activeTab) {
9392
}
9493
}
9594

95+
function showNotification(activeUrl, tab) {
96+
chrome.notifications.clear('watt-site-notification');
97+
chrome.notifications.create(
98+
'watt-site-notification', {
99+
type: 'basic',
100+
iconUrl: 'icons/128x128.png',
101+
title: "Web Activity Time Tracker",
102+
contextMessage: activeUrl + ' ' + convertShortSummaryTimeToString(tab.getTodayTime()),
103+
message: setting_notification_message
104+
});
105+
}
106+
96107
function setBlockPageToCurrent(activeUrl) {
97108
var blockUrl = chrome.runtime.getURL("block.html") + '?url=' + activeUrl;
98109
chrome.tabs.query({ currentWindow: true, active: true }, function (tab) {
@@ -110,23 +121,24 @@ function isVideoPlayedOnPage() {
110121

111122
function checkDOM(state, activeUrl, tab, activeTab) {
112123
if (state === 'idle' && isDomainEquals(activeUrl, "youtube.com")) {
113-
checkPermissions(mainTRacker, activeUrl, tab, activeTab);
124+
trackForYT(mainTRacker, activeUrl, tab, activeTab);
114125
}
115126
else activity.closeIntervalForCurrentTab();
116127
}
117128

118-
function checkPermissions(callback, activeUrl, tab, activeTab) {
119-
chrome.permissions.contains({
120-
permissions: ['tabs'],
121-
origins: ["https://www.youtube.com/*"]
122-
}, function (result) {
123-
if (result) {
124-
chrome.tabs.executeScript({ code: "var videoElement = document.getElementsByTagName('video')[0]; (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);" }, (results) => {
125-
if (results !== undefined && results[0] !== undefined && results[0] === true)
126-
callback(activeUrl, tab, activeTab);
127-
else activity.closeIntervalForCurrentTab();
128-
});
129-
} else activity.closeIntervalForCurrentTab();
129+
function trackForYT(callback, activeUrl, tab, activeTab) {
130+
if (isHasPermissioForYouTube) {
131+
executeScript(callback, activeUrl, tab, activeTab);
132+
} else {
133+
checkPermissionsForYT(executeScript, activity.closeIntervalForCurrentTab, callback, activeUrl, tab, activeTab);
134+
}
135+
}
136+
137+
function executeScript(callback, activeUrl, tab, activeTab) {
138+
chrome.tabs.executeScript({ code: "var videoElement = document.getElementsByTagName('video')[0]; (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);" }, (results) => {
139+
if (results !== undefined && results[0] !== undefined && results[0] === true)
140+
callback(activeUrl, tab, activeTab);
141+
else activity.closeIntervalForCurrentTab();
130142
});
131143
}
132144

@@ -142,6 +154,7 @@ function setDefaultSettings() {
142154
storage.saveValue(SETTINGS_INTERVAL_RANGE, SETTINGS_INTERVAL_RANGE_DEFAULT);
143155
storage.saveValue(SETTINGS_VIEW_TIME_IN_BADGE, SETTINGS_VIEW_TIME_IN_BADGE_DEFAULT);
144156
storage.saveValue(SETTINGS_INTERVAL_SAVE_STORAGE, SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT);
157+
storage.saveValue(STORAGE_NOTIFICATION_MESSAGE, STORAGE_NOTIFICATION_MESSAGE_DEFAULT);
145158
}
146159

147160
function checkSettingsImEmpty() {
@@ -150,13 +163,6 @@ function checkSettingsImEmpty() {
150163
setDefaultSettings();
151164
}
152165
});
153-
154-
storage.getValue(STORAGE_NOTIFICATION_MESSAGE, function (item) {
155-
var current = item;
156-
if (current == undefined) {
157-
storage.saveValue(STORAGE_NOTIFICATION_MESSAGE, STORAGE_NOTIFICATION_MESSAGE_DEFAULT);
158-
}
159-
});
160166
}
161167

162168
function addListener() {
@@ -191,6 +197,9 @@ function addListener() {
191197
if (key === STORAGE_NOTIFICATION_LIST) {
192198
loadNotificationList();
193199
}
200+
if (key === STORAGE_NOTIFICATION_MESSAGE) {
201+
loadNotificationMessage();
202+
}
194203
if (key === SETTINGS_INTERVAL_INACTIVITY) {
195204
storage.getValue(SETTINGS_INTERVAL_INACTIVITY, function (item) { setting_interval_inactivity = item; });
196205
}
@@ -257,6 +266,9 @@ function loadNotificationList() {
257266
storage.getValue(STORAGE_NOTIFICATION_LIST, function (items) {
258267
setting_notification_list = items;
259268
});
269+
}
270+
271+
function loadNotificationMessage() {
260272
storage.getValue(STORAGE_NOTIFICATION_MESSAGE, function (item) {
261273
setting_notification_message = item;
262274
});
@@ -267,7 +279,7 @@ function loadSettings() {
267279
storage.getValue(SETTINGS_VIEW_TIME_IN_BADGE, function (item) { setting_view_in_badge = item; });
268280
}
269281

270-
function loadAddDataFromStorage(){
282+
function loadAddDataFromStorage() {
271283
loadTabs();
272284
loadTimeIntervals();
273285
loadBlackList();
@@ -276,6 +288,35 @@ function loadAddDataFromStorage(){
276288
loadSettings();
277289
}
278290

291+
function loadPermissions() {
292+
checkPermissionsForYT();
293+
checkPermissionsForNotifications();
294+
}
295+
296+
function checkPermissionsForYT(callbackIfTrue, callbackIfFalse, ...props) {
297+
chrome.permissions.contains({
298+
permissions: ['tabs'],
299+
origins: ["https://www.youtube.com/*"]
300+
}, function (result) {
301+
if (callbackIfTrue != undefined && result)
302+
callbackIfTrue(...props);
303+
if (callbackIfFalse != undefined && !result)
304+
callbackIfFalse();
305+
isHasPermissioForYouTube = result;
306+
});
307+
}
308+
309+
function checkPermissionsForNotifications(callback, ...props) {
310+
chrome.permissions.contains({
311+
permissions: ["notifications"]
312+
}, function (result) {
313+
if (callback != undefined && result)
314+
callback(...props);
315+
isHasPermissioForNotification = result;
316+
});
317+
}
318+
319+
loadPermissions();
279320
addListener();
280321
loadAddDataFromStorage();
281322
updateSummaryTime();

src/scripts/settings.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ document.addEventListener('DOMContentLoaded', function () {
4343
document.getElementById('rangeToDays').addEventListener('change', function () {
4444
storage.saveValue(SETTINGS_INTERVAL_RANGE, this.value);
4545
});
46-
document.getElementById('grantPermission').addEventListener('click', function () {
47-
grantPermission();
46+
document.getElementById('grantPermissionForYT').addEventListener('click', function () {
47+
grantPermissionForYT();
48+
});
49+
document.getElementById('grantPermissionForNotifications').addEventListener('click', function () {
50+
grantPermissionForNotifications();
4851
});
4952
document.getElementById('notifyMessage').addEventListener('change', function () {
5053
updateNotificationMessage();
@@ -97,22 +100,33 @@ function loadSettings() {
97100
storage.getValue(STORAGE_NOTIFICATION_LIST, function (items) {
98101
notifyList = items;
99102
if (notifyList === undefined)
100-
notifyList = [];
103+
notifyList = [];
101104
viewNotificationList(items);
102105
});
103106
storage.getValue(STORAGE_NOTIFICATION_MESSAGE, function (mess) {
104107
document.getElementById('notifyMessage').value = mess;
105108
});
106-
checkPermissions();
109+
checkPermissionsForYT();
110+
checkPermissionsForNotifications();
107111
}
108112

109-
function checkPermissions() {
113+
function checkPermissionsForYT() {
110114
chrome.permissions.contains({
111115
permissions: ['tabs'],
112116
origins: ["https://www.youtube.com/*"]
113117
}, function (result) {
114118
if (result) {
115-
setUIForAnyPermission();
119+
setUIForAnyPermissionForYT();
120+
}
121+
});
122+
}
123+
124+
function checkPermissionsForNotifications() {
125+
chrome.permissions.contains({
126+
permissions: ["notifications"]
127+
}, function (result) {
128+
if (result) {
129+
setUIForAnyPermissionForNotifications();
116130
}
117131
});
118132
}
@@ -130,24 +144,40 @@ function viewBlackList(items) {
130144
}
131145
}
132146

133-
function grantPermission() {
147+
function grantPermissionForYT() {
134148
chrome.permissions.request({
135149
permissions: ['tabs'],
136150
origins: ["https://www.youtube.com/*"]
137151
}, function (granted) {
138152
// The callback argument will be true if the user granted the permissions.
139153
if (granted) {
140-
setUIForAnyPermission();
154+
setUIForAnyPermissionForYT();
141155
}
142156
});
143157
}
144158

145-
function setUIForAnyPermission() {
146-
document.getElementById('permissionSuccessedBlock').hidden = false;
147-
document.getElementById('grantPermission').setAttribute('disabled', 'true');
159+
function grantPermissionForNotifications() {
160+
chrome.permissions.request({
161+
permissions: ["notifications"]
162+
}, function (granted) {
163+
// The callback argument will be true if the user granted the permissions.
164+
if (granted) {
165+
setUIForAnyPermissionForNotifications();
166+
}
167+
});
168+
}
169+
170+
function setUIForAnyPermissionForYT() {
171+
document.getElementById('permissionSuccessedBlockForYT').hidden = false;
172+
document.getElementById('grantPermissionForYT').setAttribute('disabled', 'true');
173+
}
174+
175+
function setUIForAnyPermissionForNotifications() {
176+
document.getElementById('permissionSuccessedBlockForNotifications').hidden = false;
177+
document.getElementById('grantPermissionForNotifications').setAttribute('disabled', 'true');
148178
}
149179

150-
function viewNotificationList(items){
180+
function viewNotificationList(items) {
151181
if (items !== undefined) {
152182
for (var i = 0; i < items.length; i++) {
153183
addDomainToEditableListBox(items[i], 'notifyList', actionEditSite, deleteNotificationSite, updateItemFromNotifyList, updateNotificationList);
@@ -270,7 +300,7 @@ function addDomainToListBox(domain) {
270300
document.getElementById('blackList').appendChild(li).appendChild(del);
271301
}
272302

273-
function addDomainToEditableListBox(entity, elementId, actionEdit, actionDelete, actionUpdateTimeFromList, actionUpdateList){
303+
function addDomainToEditableListBox(entity, elementId, actionEdit, actionDelete, actionUpdateTimeFromList, actionUpdateList) {
274304
var li = document.createElement('li');
275305

276306
var domainLbl = document.createElement('input');

0 commit comments

Comments
 (0)