Skip to content

Commit

Permalink
Record the href target on anchor (and area) elements as a first ste…
Browse files Browse the repository at this point in the history
…p in distinguishing navigational clicks from other clicks. Part of rrweb-io#503
  • Loading branch information
eoghanmurray committed Jul 26, 2022
1 parent 5e4d002 commit 3b03d60
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
24 changes: 22 additions & 2 deletions packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
mousemoveCallBack,
mousePosition,
mouseInteractionCallBack,
mouseInteractionParam,
clickParam,
MouseInteractions,
listenerHandler,
scrollCallback,
Expand Down Expand Up @@ -234,12 +236,30 @@ function initMouseInteractionObserver({
}
const id = mirror.getId(target);
const { clientX, clientY } = e;
mouseInteractionCb({
let emissionEvent: mouseInteractionParam | clickParam = {
type: MouseInteractions[eventKey],
id,
x: clientX,
y: clientY,
});
}
const htarget = (target as Element);
if (MouseInteractions[eventKey] === MouseInteractions.Click) {
let href: string | null = null;
if (htarget.tagName.toLowerCase() === 'a' &&
(htarget as HTMLAnchorElement).href) {
href = (htarget as HTMLAnchorElement).href;
} else if (htarget.tagName.toLowerCase() === 'area' &&
(htarget as HTMLAreaElement).href) {
href = (htarget as HTMLAreaElement).href;
}
if (href !== null) {
emissionEvent = {
...emissionEvent,
href: href,
};
}
}
mouseInteractionCb(emissionEvent);
};
};
Object.keys(MouseInteractions)
Expand Down
8 changes: 6 additions & 2 deletions packages/rrweb/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,18 @@ export type CanvasArg =
| null
| CanvasArg[];

type mouseInteractionParam = {
export type mouseInteractionParam = {
type: MouseInteractions;
id: number;
x: number;
y: number;
};

export type mouseInteractionCallBack = (d: mouseInteractionParam) => void;
export type clickParam = mouseInteractionParam & {
href?: string;
};

export type mouseInteractionCallBack = (d: mouseInteractionParam | clickParam) => void;

export type scrollPosition = {
id: number;
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/test/__snapshots__/integration.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,7 @@ exports[`record integration tests can record clicks 1`] = `
\\"source\\": 2,
\\"type\\": 2,
\\"id\\": 21,
\\"href\\": \\"about:blank#clicked\\"
}
}
]"
Expand Down
13 changes: 13 additions & 0 deletions packages/rrweb/test/html/link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Link click</title>
</head>
<body>
<span id="not-a-link">not link</span>
<a href="#clicked">link</a>
</body>
</html>
9 changes: 7 additions & 2 deletions packages/rrweb/typings/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,18 @@ export declare type SerializedCanvasArg = {
index: number;
};
export declare type CanvasArg = SerializedCanvasArg | string | number | boolean | null | CanvasArg[];
declare type mouseInteractionParam = {
export declare type mouseInteractionParam = {
type: MouseInteractions;
id: number;
x: number;
y: number;
};
export declare type mouseInteractionCallBack = (d: mouseInteractionParam) => void;
export declare type clickParam = mouseInteractionParam & {
href?: string;
targetId?: string;
targetText?: string;
};
export declare type mouseInteractionCallBack = (d: mouseInteractionParam | clickParam) => void;
export declare type scrollPosition = {
id: number;
x: number;
Expand Down

0 comments on commit 3b03d60

Please sign in to comment.