// Naive rage click implementation: If mouse has not moved further than thresholdPx // over clickCount clicks with max timeoutMs between clicks, it's // counted as a rage click import { isObject } from '@posthog/core' import type { RageclickConfig } from '@posthog/types' const DEFAULT_THRESHOLD_PX = 30 const DEFAULT_TIMEOUT_MS = 1000 const DEFAULT_CLICK_COUNT = 3 // closest('label') without closest(), which IE11 lacks. The target may be a non-Element, for // example the document when a click is dispatched on it. function findLabel(target: EventTarget | null): HTMLLabelElement | null { if (typeof Node === 'undefined' || !(target instanceof Element)) { return null } // The native getter: a form exposes a control named "parentNode" as a property of that name, // which would otherwise make this walk loop forever. IE11 defines parentElement only on // HTMLElement, so parentNode is the getter available on Node.prototype everywhere. const parentNodeGetter = Object.getOwnPropertyDescriptor(Node.prototype, 'parentNode')?.get if (!parentNodeGetter) { return null } let node: Node | null = target // like closest(), stop at the document or a shadow root while (node && node.nodeType === 1) { // localName, unlike tagName, is lowercase in XHTML documents too if ((node as Element).localName === 'label') { return node as HTMLLabelElement } node = parentNodeGetter.call(node) as Node | null } return null } export default class RageClick { clicks: { x: number; y: number; timestamp: number }[] lastTarget: EventTarget | null lastTimestamp: number thresholdPx: number timeoutMs: number clickCount: number disabled: boolean constructor(rageclickConfig: RageclickConfig | boolean) { this.disabled = rageclickConfig === false const conf = isObject(rageclickConfig) ? rageclickConfig : {} this.thresholdPx = conf.threshold_px || DEFAULT_THRESHOLD_PX this.timeoutMs = conf.timeout_ms || DEFAULT_TIMEOUT_MS this.clickCount = conf.click_count || DEFAULT_CLICK_COUNT this.clicks = [] this.lastTarget = null this.lastTimestamp = 0 } isRageClick(x: number, y: number, timestamp: number, target?: EventTarget | null): boolean { if (this.disabled) { return false } // Clicking a