@@ -15,6 +15,9 @@ var setting_view_in_badge;
1515var setting_notification_list ;
1616var setting_notification_message ;
1717
18+ var isHasPermissioForYouTube ;
19+ var isHasPermissioForNotification ;
20+
1821function 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+
96107function 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
111122function 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
147160function 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
162168function 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 ( ) ;
279320addListener ( ) ;
280321loadAddDataFromStorage ( ) ;
281322updateSummaryTime ( ) ;
0 commit comments