-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathnavigation.ts
More file actions
134 lines (113 loc) · 3.13 KB
/
navigation.ts
File metadata and controls
134 lines (113 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { nanoid } from 'nanoid'
import { tabs, WebNavigation, webNavigation } from 'webextension-polyfill'
import { BrowserEvent } from '@/schemas/recording'
function isReload({ transitionType }: WebNavigation.OnCommittedDetailsType) {
return transitionType === 'reload'
}
function isAddressBarNavigation({
transitionType,
}: WebNavigation.OnCommittedDetailsType) {
return transitionType === 'typed' || transitionType === 'start_page'
}
function isHistoryNavigation({
transitionType,
transitionQualifiers,
}: WebNavigation.OnCommittedDetailsType) {
return (
transitionType === 'link' && transitionQualifiers.includes('forward_back')
)
}
function isImplicitNavigation({
transitionType,
transitionQualifiers,
}: WebNavigation.OnCommittedDetailsType) {
return (
(transitionType === 'link' &&
!transitionQualifiers.includes('forward_back')) ||
transitionType === 'form_submit'
)
}
function getNavigationSource(details: WebNavigation.OnCommittedDetailsType) {
if (isHistoryNavigation(details)) {
return 'history'
}
if (isAddressBarNavigation(details)) {
return 'address-bar'
}
if (isImplicitNavigation(details)) {
return 'implicit'
}
return null
}
export function captureNavigationEvents(
onCaptured: (events: BrowserEvent[] | BrowserEvent) => void
) {
webNavigation.onCommitted.addListener((details) => {
console.log('onCommitted', details)
if (isReload(details)) {
onCaptured({
type: 'reload-page',
eventId: nanoid(),
timestamp: details.timeStamp,
tab: details.tabId.toString(),
url: details.url ?? '',
})
return
}
const source = getNavigationSource(details)
if (source === null) {
return
}
onCaptured({
type: 'navigate-to-page',
eventId: nanoid(),
timestamp: details.timeStamp,
tab: details.tabId.toString(),
url: details.url ?? '',
source,
})
})
webNavigation.onHistoryStateUpdated.addListener((details) => {
console.log('onHistoryStateUpdated', details)
if (isHistoryNavigation(details)) {
onCaptured({
type: 'navigate-to-page',
eventId: nanoid(),
timestamp: details.timeStamp,
tab: details.tabId.toString(),
url: details.url ?? '',
source: 'history',
})
return
}
})
tabs
.query({ active: true, currentWindow: true })
.then((tabs) => {
for (const tab of tabs) {
// If the pendingUrl is set, it means that the navigation hasn't been
// comitted yet so the onCommited handler will record the navigation.
if (tab.pendingUrl !== undefined) {
continue
}
if (
tab.id === undefined ||
tab.url === undefined ||
tab.url.startsWith('chrome://')
) {
continue
}
onCaptured({
type: 'navigate-to-page',
eventId: nanoid(),
timestamp: Date.now(),
tab: tab.id.toString(),
url: tab.url,
source: 'address-bar',
})
}
})
.catch((error) => {
console.error('Error getting active tab:', error)
})
}