|
| 1 | +import { sendMsg2Runtime } from '@api/chrome/runtime' |
| 2 | + |
| 3 | +type Session = { |
| 4 | + startTime: number |
| 5 | + domain: string |
| 6 | +} |
| 7 | + |
| 8 | +export default class TimelineCollector { |
| 9 | + private currentSession: Session | null = null; |
| 10 | + |
| 11 | + private isActive: boolean = false; |
| 12 | + |
| 13 | + /** |
| 14 | + * Bind page visibility and focus events |
| 15 | + */ |
| 16 | + init(): void { |
| 17 | + document.addEventListener('visibilitychange', () => { |
| 18 | + if (document.hidden) { |
| 19 | + this.pauseTracking() |
| 20 | + } else { |
| 21 | + this.startTracking() |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + window.addEventListener('focus', () => this.startTracking()) |
| 26 | + window.addEventListener('blur', () => this.pauseTracking()) |
| 27 | + window.addEventListener('beforeunload', () => this.endCurrentSession()) |
| 28 | + |
| 29 | + if (document.readyState === 'complete') { |
| 30 | + this.startTracking() |
| 31 | + } else { |
| 32 | + window.addEventListener('load', () => this.startTracking()) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Start tracking current page |
| 38 | + */ |
| 39 | + public startTracking(): void { |
| 40 | + if (this.isActive) return |
| 41 | + |
| 42 | + this.isActive = true |
| 43 | + const now: number = Date.now() |
| 44 | + const domain: string = this.getCurrentDomain() |
| 45 | + |
| 46 | + this.currentSession = { |
| 47 | + startTime: now, |
| 48 | + domain: domain, |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Pause tracking |
| 54 | + */ |
| 55 | + public pauseTracking(): void { |
| 56 | + if (!this.isActive || !this.currentSession) return |
| 57 | + |
| 58 | + this.endCurrentSession() |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * End current session and generate event |
| 63 | + */ |
| 64 | + private endCurrentSession(): void { |
| 65 | + if (!this.currentSession) return |
| 66 | + |
| 67 | + const now: number = Date.now() |
| 68 | + const duration: number = now - this.currentSession.startTime |
| 69 | + |
| 70 | + const event = this.createEvent( |
| 71 | + this.currentSession.startTime, |
| 72 | + this.currentSession.domain, |
| 73 | + duration, |
| 74 | + ) |
| 75 | + sendMsg2Runtime('cs.timelineEv', event) |
| 76 | + |
| 77 | + this.currentSession = null |
| 78 | + this.isActive = false |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Create event object |
| 83 | + */ |
| 84 | + private createEvent(startTime: number, domain: string, duration: number): timer.timeline.Event { |
| 85 | + return { |
| 86 | + startTime: startTime, |
| 87 | + domain: domain, |
| 88 | + duration: duration, |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get current domain |
| 94 | + */ |
| 95 | + private getCurrentDomain(): string { |
| 96 | + try { |
| 97 | + return new URL(window.location.href).hostname |
| 98 | + } catch (error) { |
| 99 | + return window.location.hostname || 'unknown' |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Destroy collector and remove event listeners |
| 105 | + */ |
| 106 | + public destroy(): void { |
| 107 | + this.endCurrentSession() |
| 108 | + } |
| 109 | +} |
0 commit comments