1- import Browser from 'webextension-polyfill' ;
2- import { isValidPage } from './compositions/valid-page' ;
3- import { 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' ;
1+ import Browser from "webextension-polyfill" ;
2+ import { isValidPage } from "./compositions/valid-page" ;
3+ import { 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" ;
8+ import { INTERVAL_INACTIVITY_DEFAULT , StorageParams , } from "./storage/storage-params" ;
9+ import { injecStorage } from "./storage/inject-storage" ;
10+ import { closeInterval } from "./compositions/daily-intervals" ;
11+ import { ActiveTab } from "./compositions/activeTab" ;
12+ import { isLimitExceeded } from "./compositions/limit-list" ;
13+ import { Tab } from "./entity/tab" ;
14+ import { useBlockPage } from "./compositions/block-page" ;
815
9- export async function initTracker ( ) {
16+ async function trackTime ( ) {
17+ const activeTabInstance = ActiveTab . getInstance ( ) ;
1018 const window = await Browser . windows . getLastFocused ( { populate : true } ) ;
11- if ( window . focused ) {
12- const activeTab = window . tabs ?. find ( t => t . active === true ) ;
19+ if ( window . focused ) {
20+ const activeTab = window . tabs ?. find ( ( t ) => t . active === true ) ;
1321 if ( isValidPage ( activeTab ) ) {
1422 const activeUrl = extractHostname ( activeTab ! . url ) ;
23+ activeTabInstance . setActiveTab ( activeUrl ) ;
1524
1625 if ( await isInBlackList ( activeUrl ) ) {
17- useBadge ( { tabId : activeTab ! . id ! , text : 'n/a' , color : BadgeColor . green } ) ;
18- } else {
26+ useBadge ( {
27+ tabId : activeTab ! . id ! ,
28+ text : "n/a" ,
29+ color : BadgeColor . red ,
30+ } ) ;
31+ } else {
1932 const repo = await injectTabsRepository ( ) ;
20- let tab = repo . getTab ( activeUrl ) ;
21- if ( tab === undefined )
22- tab = await repo . addTab ( activeTab ! ) ;
23-
24- if ( currentTab !== tab . url ) {
25- activity . setCurrentActiveTab ( tab . url ) ;
26- }
27- const state = await Browser . idle . queryState ( parseInt ( setting_interval_inactivity ) ) ;
28- if ( state === 'active' ) {
29- mainTRacker ( activeUrl , tab , activeTab ) ;
30- } else checkDOM ( state , activeUrl , tab , activeTab ) ;
33+ const tab = repo . getTab ( activeUrl ) ;
34+ if ( tab == undefined ) {
35+ await repo . addTab ( activeTab ! ) ;
36+ } else {
37+ const storage = injecStorage ( ) ;
38+ const inactivityInterval = ( await storage . getValue (
39+ StorageParams . INTERVAL_INACTIVITY ,
40+ INTERVAL_INACTIVITY_DEFAULT
41+ ) ) as number ;
42+ const state = await Browser . idle . queryState ( inactivityInterval ) ;
43+ mainTracker ( state , activeTab ! , activeUrl , tab ) ;
44+ }
3145 }
3246 }
33- } else activity . closeIntervalForCurrentTab ( ) ;
34- }
47+ } else {
48+ await closeInterval ( activeTabInstance . getActiveTab ( ) ) ;
49+ activeTabInstance . setActiveTab ( null ) ;
50+ }
51+ }
52+
53+ async function mainTracker ( state : Browser . Idle . IdleState , activeTab : Browser . Tabs . Tab , activeUrl : string , tab : Tab ) {
54+ function isAudible ( ) {
55+ return state === 'idle' && activeTab . audible ;
56+ }
57+
58+ if ( state === 'active' || isAudible ( ) ) {
59+ if ( await isLimitExceeded ( activeUrl , tab ) ) {
60+ const summaryTime = tab . days . at ( - 1 ) ! . summary ;
61+ const summaryCounter = tab . days . at ( - 1 ) ! . counter ;
62+ await useBlockPage ( activeUrl , summaryTime , summaryCounter ) ;
63+ return ;
64+ }
65+
66+ tab . incSummaryTime ( ) ;
67+ }
68+ }
69+
70+ export async function initTracker ( ) {
71+ setInterval ( trackTime , 1000 ) ;
72+ }
0 commit comments