1- 'use strict' ; var tabs ; var currentTab ; var activity = new Activity ( ) ; var storage = new LocalStorage ( ) ; var setting_interval_save ; var setting_interval_inactivity ; var setting_view_in_badge ; function updateSummaryTime ( ) { setInterval ( backgroundCheck , SETTINGS_INTERVAL_CHECK_DEFAULT ) }
2- function updateStorage ( ) { setInterval ( backgroundUpdateStorage , SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT ) }
3- function backgroundCheck ( ) { storage . getSettings ( SETTINGS_INTERVAL_INACTIVITY , function ( item ) { setting_interval_inactivity = item } ) ; storage . getSettings ( SETTINGS_VIEW_TIME_IN_BADGE , function ( item ) { setting_view_in_badge = item } ) ; chrome . windows . getLastFocused ( { populate :! 0 } , function ( currentWindow ) { if ( currentWindow . focused ) { var activeTab = currentWindow . tabs . find ( t => t . active === ! 0 ) ; if ( activeTab !== undefined && activity . isValidPage ( activeTab ) ) { var activeUrl = activity . extractHostname ( activeTab . url ) ; var tab = activity . getTab ( activeUrl ) ; if ( tab === undefined ) { activity . addTab ( activeTab ) }
4- if ( tab !== undefined ) { activity . setCurrentActiveTab ( tab . url ) ; chrome . idle . queryState ( parseInt ( setting_interval_inactivity ) , function ( state ) { if ( state === 'active' ) { tab . incSummaryTime ( ) ; if ( setting_view_in_badge === ! 0 ) { var today = new Date ( ) . toLocaleDateString ( ) ; var summary = tab . days . find ( s => s . date === today ) . summary ; chrome . browserAction . setBadgeText ( { tabId :activeTab . id , text :String ( convertSummaryTimeToBadgeString ( summary ) ) } ) } else { chrome . browserAction . setBadgeText ( { tabId :activeTab . id , text :'' } ) } } } ) } } } } ) }
5- function backgroundUpdateStorage ( ) { if ( tabs != undefined && tabs . length > 0 )
6- storage . saveTabs ( tabs ) }
7- function setDefaultSettings ( ) { storage . saveSettings ( SETTINGS_INTERVAL_INACTIVITY , SETTINGS_INTERVAL_INACTIVITY_DEFAULT ) ; storage . saveSettings ( SETTINGS_INTERVAL_RANGE , SETTINGS_INTERVAL_RANGE_DEFAULT ) ; storage . saveSettings ( SETTINGS_VIEW_TIME_IN_BADGE , SETTINGS_VIEW_TIME_IN_BADGE_DEFAULT ) ; storage . saveSettings ( SETTINGS_INTERVAL_SAVE_STORAGE , SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT ) }
8- function addListener ( ) { chrome . tabs . onActivated . addListener ( function ( info ) { chrome . tabs . get ( info . tabId , function ( tab ) { activity . addTab ( tab ) } ) } ) ; chrome . webNavigation . onCompleted . addListener ( function ( details ) { chrome . tabs . get ( details . tabId , function ( tab ) { activity . updateFavicon ( tab ) } ) } ) ; chrome . runtime . onInstalled . addListener ( function ( details ) { if ( details . reason == "install" ) { setDefaultSettings ( ) } } ) }
9- function loadTabs ( ) { storage . loadTabs ( STORAGE_TABS , function ( items )
10- { tabs = [ ] ; for ( var i = 0 ; i < items . length ; i ++ ) { tabs . push ( new Tab ( items [ i ] . url , items [ i ] . favicon , items [ i ] . days , items [ i ] . summaryTime ) ) } } ) }
11- loadTabs ( ) ; addListener ( ) ; updateSummaryTime ( ) ; updateStorage ( )
1+ 'use strict' ;
2+
3+ var tabs ;
4+ var currentTab ;
5+ var activity = new Activity ( ) ;
6+ var storage = new LocalStorage ( ) ;
7+
8+ var setting_interval_save ;
9+ var setting_interval_inactivity ;
10+ var setting_view_in_badge ;
11+
12+ function updateSummaryTime ( ) {
13+ setInterval ( backgroundCheck , SETTINGS_INTERVAL_CHECK_DEFAULT ) ;
14+ }
15+
16+ function updateStorage ( ) {
17+ setInterval ( backgroundUpdateStorage , SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT ) ;
18+ }
19+
20+ function backgroundCheck ( ) {
21+ storage . getSettings ( SETTINGS_INTERVAL_INACTIVITY , function ( item ) { setting_interval_inactivity = item ; } ) ;
22+ storage . getSettings ( SETTINGS_VIEW_TIME_IN_BADGE , function ( item ) { setting_view_in_badge = item ; } ) ;
23+ chrome . windows . getLastFocused ( { populate : true } , function ( currentWindow ) {
24+ if ( currentWindow . focused ) {
25+ var activeTab = currentWindow . tabs . find ( t => t . active === true ) ;
26+ if ( activeTab !== undefined && activity . isValidPage ( activeTab ) ) {
27+ var activeUrl = activity . extractHostname ( activeTab . url ) ;
28+ var tab = activity . getTab ( activeUrl ) ;
29+ if ( tab === undefined ) {
30+ activity . addTab ( activeTab ) ;
31+ }
32+
33+ if ( tab !== undefined ) {
34+ activity . setCurrentActiveTab ( tab . url ) ;
35+ chrome . idle . queryState ( parseInt ( setting_interval_inactivity ) , function ( state ) {
36+ if ( state === 'active' ) {
37+ tab . incSummaryTime ( ) ;
38+ if ( setting_view_in_badge === true ) {
39+ var today = new Date ( ) . toLocaleDateString ( ) ;
40+ var summary = tab . days . find ( s => s . date === today ) . summary ;
41+ chrome . browserAction . setBadgeText ( {
42+ tabId : activeTab . id ,
43+ text : String ( convertSummaryTimeToBadgeString ( summary ) )
44+ } ) ;
45+ } else {
46+ chrome . browserAction . setBadgeText ( {
47+ tabId : activeTab . id ,
48+ text : ''
49+ } ) ;
50+ }
51+ }
52+ } ) ;
53+ }
54+ }
55+ }
56+ } ) ;
57+ }
58+
59+ function backgroundUpdateStorage ( ) {
60+ if ( tabs != undefined && tabs . length > 0 )
61+ storage . saveTabs ( tabs ) ;
62+ }
63+
64+ function setDefaultSettings ( ) {
65+ storage . saveSettings ( SETTINGS_INTERVAL_INACTIVITY , SETTINGS_INTERVAL_INACTIVITY_DEFAULT ) ;
66+ storage . saveSettings ( SETTINGS_INTERVAL_RANGE , SETTINGS_INTERVAL_RANGE_DEFAULT ) ;
67+ storage . saveSettings ( SETTINGS_VIEW_TIME_IN_BADGE , SETTINGS_VIEW_TIME_IN_BADGE_DEFAULT ) ;
68+ storage . saveSettings ( SETTINGS_INTERVAL_SAVE_STORAGE , SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT ) ;
69+ }
70+
71+ function addListener ( ) {
72+ chrome . tabs . onActivated . addListener ( function ( info ) {
73+ chrome . tabs . get ( info . tabId , function ( tab ) {
74+ activity . addTab ( tab ) ;
75+ } ) ;
76+ } ) ;
77+
78+ chrome . webNavigation . onCompleted . addListener ( function ( details ) {
79+ chrome . tabs . get ( details . tabId , function ( tab ) {
80+ activity . updateFavicon ( tab ) ;
81+ } ) ;
82+ } ) ;
83+ chrome . runtime . onInstalled . addListener ( function ( details ) {
84+ if ( details . reason == "install" ) {
85+ setDefaultSettings ( ) ;
86+ }
87+ } ) ;
88+ }
89+
90+ function loadTabs ( ) {
91+ storage . loadTabs ( STORAGE_TABS , function ( items )
92+ {
93+ tabs = [ ] ;
94+ for ( var i = 0 ; i < items . length ; i ++ ) {
95+ tabs . push ( new Tab ( items [ i ] . url , items [ i ] . favicon , items [ i ] . days , items [ i ] . summaryTime ) ) ;
96+ }
97+ } ) ;
98+ }
99+
100+ loadTabs ( ) ;
101+ addListener ( ) ;
102+ updateSummaryTime ( ) ;
103+ updateStorage ( ) ;
0 commit comments