11import Browser from 'webextension-polyfill' ;
22import { isValidPage } from './compositions/valid-page' ;
33import { extractHostname } from './compositions/extract-hostname' ;
4+ import { injectTabsRepository } from './repository/inject-tabs-repository' ;
5+ import { isInBlackList } from './compositions/black-list' ;
6+ import { useBadge } from './compositions/set-badge' ;
7+ import { BadgeColor } from './compositions/types' ;
48
59export async function initTracker ( ) {
610 const window = await Browser . windows . getLastFocused ( { populate : true } ) ;
711 if ( window . focused ) {
812 const activeTab = window . tabs ?. find ( t => t . active === true ) ;
913 if ( isValidPage ( activeTab ) ) {
10- var activeUrl = extractHostname ( activeTab ?. url ) ;
11- var tab = activity . getTab ( activeUrl ) ;
12- if ( tab === undefined ) {
13- activity . addTab ( activeTab ) ;
14- }
14+ const activeUrl = extractHostname ( activeTab ! . url ) ;
15+
16+ if ( await isInBlackList ( activeUrl ) ) {
17+ useBadge ( { tabId : activeTab ! . id ! , text : 'n/a' , color : BadgeColor . green } ) ;
18+ } else {
19+ const repo = await injectTabsRepository ( ) ;
20+ let tab = repo . getTab ( activeUrl ) ;
21+ if ( tab === undefined )
22+ tab = await repo . addTab ( activeTab ! ) ;
1523
16- if ( activity . isInBlackList ( activeUrl ) ) {
17- chrome . action . setBadgeBackgroundColor ( { color : '#fdb8b8' } )
18- chrome . action . setBadgeText ( {
19- tabId : activeTab . id ,
20- text : 'n/a'
21- } ) ;
22- } else {
23- if ( tab !== undefined ) {
2424 if ( currentTab !== tab . url ) {
2525 activity . setCurrentActiveTab ( tab . url ) ;
2626 }
27- chrome . idle . queryState ( parseInt ( setting_interval_inactivity ) , function ( state ) {
27+ const state = await Browser . idle . queryState ( parseInt ( setting_interval_inactivity ) ) ;
2828 if ( state === 'active' ) {
2929 mainTRacker ( activeUrl , tab , activeTab ) ;
3030 } else checkDOM ( state , activeUrl , tab , activeTab ) ;
31- } ) ;
32- }
3331 }
34- }
32+ }
3533 } else activity . closeIntervalForCurrentTab ( ) ;
3634}
0 commit comments