@@ -18,8 +18,6 @@ var setting_dark_mode;
1818var setting_notification_list ;
1919var setting_notification_message ;
2020
21- var isHasPermissioForYouTube ;
22- var isHasPermissioForNetflix ;
2321var isHasPermissioForNotification ;
2422
2523function 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
6765function 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-
190153function backgroundUpdateStorage ( ) {
191154 if ( tabs != undefined && tabs . length > 0 )
192155 storage . saveTabs ( tabs ) ;
@@ -371,37 +334,9 @@ function loadAddDataFromStorage() {
371334}
372335
373336function 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-
405340function checkPermissionsForNotifications ( callback , ...props ) {
406341 chrome . permissions . contains ( {
407342 permissions : [ "notifications" ]
0 commit comments