Skip to content

Commit cd1f2ba

Browse files
authored
Add typing method to urlChanged method (#1869)
1 parent 6389a7e commit cd1f2ba

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

injected/src/features/web-telemetry.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import ContentFeature from '../content-feature.js';
22

3+
/**
4+
* @typedef {import('../url-change.js').NavigationType} NavigationType
5+
*/
6+
37
const MSG_VIDEO_PLAYBACK = 'video-playback';
48
const MSG_URL_CHANGED = 'url-changed';
59

@@ -18,7 +22,10 @@ export class WebTelemetry extends ContentFeature {
1822
}
1923
}
2024

21-
urlChanged(navigationType = 'unknown') {
25+
/**
26+
* @param {NavigationType} navigationType
27+
*/
28+
urlChanged(navigationType) {
2229
if (this.getFeatureSettingEnabled('urlChanged')) {
2330
this.fireTelemetryForUrlChanged(navigationType);
2431
}
@@ -40,6 +47,9 @@ export class WebTelemetry extends ContentFeature {
4047
return null;
4148
}
4249

50+
/**
51+
* @param {NavigationType} navigationType
52+
*/
4353
fireTelemetryForUrlChanged(navigationType) {
4454
this.messaging.notify(MSG_URL_CHANGED, {
4555
url: window.location.href,

injected/src/url-change.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import { DDGProxy, DDGReflect, isBeingFramed } from './utils.js';
22
import ContentFeature from './content-feature.js';
33

4+
/**
5+
* @typedef {'push' | 'replace' | 'reload' | 'traverse' | 'unknown'} NavigationType
6+
* An enumerated value representing the type of navigation.
7+
*
8+
* Possible values:
9+
* - `'push'` - A new location is navigated to, causing a new entry to be pushed onto the history list.
10+
* - `'replace'` - The Navigation.currentEntry is replaced with a new history entry.
11+
* - `'reload'` - The Navigation.currentEntry is reloaded.
12+
* - `'traverse'` - The browser navigates from one existing history entry to another existing history entry.
13+
* - `'unknown'` - Fallback but highly unlikely. If the WeakMap lookup fails that means the navigate event wasn't captured.
14+
*
15+
* @see https://developer.mozilla.org/en-US/docs/Web/API/NavigateEvent/navigationType
16+
*/
17+
18+
/**
19+
* @typedef {(navigationType: NavigationType) => void} URLChangeListener
20+
*/
21+
422
const urlChangeListeners = new Set();
23+
524
/**
625
* Register a listener to be called when the URL changes.
7-
* @param {function} listener
26+
* @param {URLChangeListener} listener - Callback function that receives the navigation type
827
*/
928
export function registerForURLChanges(listener) {
1029
if (urlChangeListeners.size === 0) {
@@ -13,6 +32,9 @@ export function registerForURLChanges(listener) {
1332
urlChangeListeners.add(listener);
1433
}
1534

35+
/**
36+
* @param {NavigationType} navigationType - The type of navigation that occurred
37+
*/
1638
function handleURLChange(navigationType = 'unknown') {
1739
for (const listener of urlChangeListeners) {
1840
listener(navigationType);
@@ -32,7 +54,7 @@ function listenForURLChanges() {
3254
navigations.set(event.target, event.navigationType);
3355
});
3456
globalThis.navigation.addEventListener('navigatesuccess', (event) => {
35-
const navigationType = navigations.get(event.target) || 'unknown';
57+
const navigationType = navigations.get(event.target);
3658
handleURLChange(navigationType);
3759
navigations.delete(event.target);
3860
});
@@ -56,6 +78,6 @@ function listenForURLChanges() {
5678
historyMethodProxy.overload();
5779
// listen for popstate events in order to run on back/forward navigations
5880
window.addEventListener('popstate', () => {
59-
handleURLChange('popState');
81+
handleURLChange('traverse');
6082
});
6183
}

0 commit comments

Comments
 (0)