Skip to content

Commit 9ef6c13

Browse files
committed
Record the href target on anchor (and area) elements as a first step in distinguishing navigational clicks from other clicks. Part of #503
1 parent 00fd6eb commit 9ef6c13

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

packages/rrweb/src/record/observer.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323
mousemoveCallBack,
2424
mousePosition,
2525
mouseInteractionCallBack,
26+
mouseInteractionParam,
27+
clickParam,
2628
MouseInteractions,
2729
listenerHandler,
2830
scrollCallback,
@@ -286,12 +288,30 @@ function initMouseInteractionObserver(
286288
}
287289
const id = mirror.getId(target as INode);
288290
const { clientX, clientY } = e;
289-
cb({
291+
let emissionEvent: mouseInteractionParam | clickParam = {
290292
type: MouseInteractions[eventKey],
291293
id,
292294
x: clientX,
293295
y: clientY,
294-
});
296+
}
297+
const htarget = (target as Element);
298+
if (MouseInteractions[eventKey] === MouseInteractions.Click) {
299+
let href: string | null = null;
300+
if (htarget.tagName.toLowerCase() === 'a' &&
301+
(htarget as HTMLAnchorElement).href) {
302+
href = (htarget as HTMLAnchorElement).href;
303+
} else if (htarget.tagName.toLowerCase() === 'area' &&
304+
(htarget as HTMLAreaElement).href) {
305+
href = (htarget as HTMLAreaElement).href;
306+
}
307+
if (href !== null) {
308+
emissionEvent = {
309+
...emissionEvent,
310+
href: href,
311+
};
312+
}
313+
}
314+
cb(emissionEvent);
295315
};
296316
};
297317
Object.keys(MouseInteractions)

packages/rrweb/src/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,18 @@ export enum MouseInteractions {
365365
TouchEnd,
366366
}
367367

368-
type mouseInteractionParam = {
368+
export type mouseInteractionParam = {
369369
type: MouseInteractions;
370370
id: number;
371371
x: number;
372372
y: number;
373373
};
374374

375-
export type mouseInteractionCallBack = (d: mouseInteractionParam) => void;
375+
export type clickParam = mouseInteractionParam & {
376+
href?: string;
377+
};
378+
379+
export type mouseInteractionCallBack = (d: mouseInteractionParam | clickParam) => void;
376380

377381
export type scrollPosition = {
378382
id: number;

packages/rrweb/test/__snapshots__/integration.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,7 @@ exports[`link 1`] = `
29922992
\\"source\\": 2,
29932993
\\"type\\": 2,
29942994
\\"id\\": 21,
2995+
\\"href\\": \\"about:blank#clicked\\"
29952996
}
29962997
}
29972998
]"

packages/rrweb/test/html/link.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<title>Link click</title>
8+
</head>
9+
<body>
10+
<span id="not-a-link">not link</span>
11+
<a href="#clicked">link</a>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)