66 */
77
88import TimerDatabase from "@db/timer-database"
9+ import whitelistHolder from "@service/components/whitelist-holder"
910import optionService from "@service/option-service"
1011import { extractHostname , isBrowserUrl } from "@util/pattern"
1112
@@ -18,9 +19,9 @@ export type BadgeLocation = {
1819 */
1920 tabId : number
2021 /**
21- * The host to gain the focus time, can be undefined or null
22+ * The url of tab
2223 */
23- host : string
24+ url : string
2425}
2526
2627function mill2Str ( milliseconds : number ) {
@@ -35,8 +36,12 @@ function mill2Str(milliseconds: number) {
3536 }
3637}
3738
38- function setBadgeText ( milliseconds : number | undefined , tabId : number | undefined ) {
39+ function setBadgeTextOfMills ( milliseconds : number | undefined , tabId : number | undefined ) {
3940 const text = milliseconds === undefined ? '' : mill2Str ( milliseconds )
41+ setBadgeText ( text , tabId )
42+ }
43+
44+ function setBadgeText ( text : string , tabId : number | undefined ) {
4045 chrome . browserAction ?. setBadgeText ?.( { text, tabId } )
4146}
4247
@@ -64,8 +69,7 @@ function findActiveTab(): Promise<BadgeLocation> {
6469 resolve ( undefined )
6570 } else {
6671 const { url, id } = tabs [ 0 ]
67- const host = extractHostname ( url ) . host
68- resolve ( { tabId : id , host } )
72+ resolve ( { tabId : id , url } )
6973 }
7074 } )
7175 } ) )
@@ -76,9 +80,17 @@ async function updateFocus(badgeLocation?: BadgeLocation) {
7680 if ( ! badgeLocation ) {
7781 return
7882 }
79- const { host, tabId } = badgeLocation
83+ const { url, tabId } = badgeLocation
84+ if ( ! url || isBrowserUrl ( url ) ) {
85+ return
86+ }
87+ const host = extractHostname ( url ) ?. host
88+ if ( whitelistHolder . contains ( host ) ) {
89+ setBadgeText ( 'W' , tabId )
90+ return
91+ }
8092 const milliseconds = host ? ( await timerDb . get ( host , new Date ( ) ) ) . focus : undefined
81- setBadgeText ( milliseconds , tabId )
93+ setBadgeTextOfMills ( milliseconds , tabId )
8294}
8395
8496const ALARM_NAME = 'timer-badge-text-manager-alarm'
@@ -102,14 +114,16 @@ class BadgeTextManager {
102114 const option : Partial < timer . option . AllOption > = await optionService . getAllOption ( )
103115 this . pauseOrResumeAccordingToOption ( ! ! option . displayBadgeText )
104116 optionService . addOptionChangeListener ( ( { displayBadgeText } ) => this . pauseOrResumeAccordingToOption ( displayBadgeText ) )
117+ whitelistHolder . addPostHandler ( updateFocus )
105118 }
106119
107120 /**
108121 * Hide the badge text
109122 */
110- pause ( ) {
123+ async pause ( ) {
111124 this . isPaused = true
112- setBadgeText ( undefined , undefined )
125+ const tab = await findActiveTab ( )
126+ setBadgeText ( 'P' , tab ?. tabId )
113127 }
114128
115129 /**
0 commit comments