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 #503
  • Loading branch information
eoghanmurray committed Aug 9, 2021
1 parent 44111c1 commit 08bf3ff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 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 @@ -23,6 +23,8 @@ import {
mousemoveCallBack,
mousePosition,
mouseInteractionCallBack,
mouseInteractionParam,
clickParam,
MouseInteractions,
listenerHandler,
scrollCallback,
Expand Down Expand Up @@ -286,12 +288,30 @@ function initMouseInteractionObserver(
}
const id = mirror.getId(target as INode);
const { clientX, clientY } = e;
cb({
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,
};
}
}
cb(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 @@ -365,14 +365,18 @@ export enum MouseInteractions {
TouchEnd,
}

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 @@ -2992,6 +2992,7 @@ exports[`link 1`] = `
\\"source\\": 2,
\\"type\\": 2,
\\"id\\": 21,
\\"href\\": \\"about:blank#clicked\\"
}
}
]"
Expand Down

0 comments on commit 08bf3ff

Please sign in to comment.