11import { openLog } from '../common/logger'
2- import IconUrlDatabase from '../database/icon-url-database '
3- import timerService from '../service/timer-service '
4- import { IS_CHROME } from '../util/constant/environment '
5- import { extractHostname , isBrowserUrl } from '../util/pattern '
6- import versionManager from './version-manager'
2+ import { t2Chrome } from '../util/i18n/chrome/t '
3+ import ContextMenusManager from './context-menus-manager '
4+ import IconUrlCollector from './icon-url-collector '
5+ import Timer from './timer '
6+ import VersionManager from './version-manager'
77
8+ // Open the log of console
89openLog ( )
910
10- const iconUrlDatabase = new IconUrlDatabase ( chrome . storage . local )
11+ // Start the timer
12+ new Timer ( ) . start ( )
1113
12- let lastLoopTime = Date . now ( )
14+ // Collect the icon url
15+ new IconUrlCollector ( ) . listen ( )
1316
14- let timeMap : { [ host : string ] : { focus : number , run : number } } = { }
17+ // Process version
18+ new VersionManager ( ) . init ( )
1519
16- setInterval ( ( ) => {
17- const now = Date . now ( )
18- const realInterval = now - lastLoopTime
19- lastLoopTime = now
20- chrome . windows . getAll ( windows => {
21- const hostSet : Set < string > = new Set ( )
22- let focusHost = ''
23- Promise . all (
24- windows . map ( w => {
25- const isFocusWindow = ! ! w . focused
26- return new Promise < void > ( resolve => {
27- chrome . tabs . query ( { windowId : w . id } , tabs => {
28- tabs . forEach ( tab => {
29- const url = tab . url
30- if ( ! url ) return
31- if ( isBrowserUrl ( url ) ) return
32- const host = extractHostname ( url ) . host
33- if ( host ) {
34- hostSet . add ( host )
35- isFocusWindow && tab . active && ( focusHost = host )
36- } else {
37- console . log ( 'Detect blank host:' , url )
38- }
39- } )
40- resolve ( )
41- } )
42- } )
43- } )
44- ) . then ( ( ) => {
45- hostSet . forEach ( host => {
46- let data = timeMap [ host ]
47- ! data && ( timeMap [ host ] = data = { focus : 0 , run : 0 } )
48- focusHost === host && ( data . focus += realInterval )
49- data . run += realInterval
50- } )
51- } )
52- } )
53- } , 555 )
54-
55- setInterval ( ( ) => {
56- timerService . addFocusAndTotal ( timeMap )
57- timeMap = { }
58- } , 2048 )
59-
60- chrome . runtime . onInstalled . addListener ( detail => versionManager . onChromeInstalled ( detail . reason ) )
61-
62- chrome . webNavigation . onCompleted . addListener ( ( detail ) => {
63- if ( detail . frameId > 0 ) {
64- // we don't care about activity occurring within a subframe of a tab
65- return
66- }
67- chrome . tabs . get ( detail . tabId , tab => {
68- if ( ! tab ) return
69- const url = tab . url
70- if ( ! url ) return
71- const hostInfo = extractHostname ( url )
72- const domain = hostInfo . host
73- const protocol = hostInfo . protocol
74- if ( ! domain ) return
75- let favIconUrl = tab . favIconUrl
76- // localhost hosts with Chrome use cache, so keep the favIconurl undefined
77- IS_CHROME && / ^ l o c a l h o s t ( : .+ ) ? / . test ( domain ) && ( favIconUrl = undefined )
78- const iconUrl = favIconUrl
79- || ( IS_CHROME ? `chrome://favicon/${ protocol ? protocol + '://' : '' } ${ domain } ` : '' )
80- iconUrlDatabase . put ( domain , iconUrl )
81- } )
82- } )
20+ // Mange the context menus
21+ new ContextMenusManager ( ) . init ( )
0 commit comments