donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
97 lines • 3.69 kB
JavaScript
;
/**
* Donobu script for tracking interactions with the current page. This scripts assumes the
* following:
* 1. There is an exposed binding to call back to NodeJS named "__donobuTrackInteraction".
* 2. The smart-selector-generator.js script has defined the "__donobu.generateSmartSelectors"
* method on the window object.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.installPageInteractionsTracker = installPageInteractionsTracker;
function installPageInteractionsTracker() {
const dnb = window.__donobu;
if (!dnb) {
throw new Error('[Donobu] __donobu namespace missing; page-interactions-tracker cannot initialize.');
}
else if (dnb.interactionListenersRegistered) {
return;
}
if (!dnb.startDragPosition) {
Object.defineProperty(dnb, 'startDragPosition', {
value: {
x: 0,
y: 0,
},
enumerable: false,
writable: true,
configurable: false,
});
}
const processedEvents = dnb.processedEvents ?? new WeakSet();
if (!dnb.processedEvents) {
Object.defineProperty(dnb, 'processedEvents', {
value: processedEvents,
enumerable: false,
writable: true,
configurable: false,
});
}
// Mouse events
['click', 'dblclick', 'contextmenu', 'mousedown', 'mouseup'].forEach((eventType) => {
document.addEventListener(eventType, function (e) {
if (processedEvents.has(e)) {
return;
}
processedEvents.add(e);
const mouseEvent = e;
const target = mouseEvent.target instanceof Element ? mouseEvent.target : null;
const eventData = {
type: eventType,
x: mouseEvent.clientX,
y: mouseEvent.clientY,
selectors: dnb.generateSmartSelectors(target),
timestamp: new Date().getTime(),
};
if (eventType === 'mousedown') {
dnb.startDragPosition = {
x: mouseEvent.clientX,
y: mouseEvent.clientY,
};
}
else if (eventType === 'mouseup') {
eventData.dragDistance = Math.sqrt(Math.pow(mouseEvent.clientX - dnb.startDragPosition.x, 2) +
Math.pow(mouseEvent.clientY - dnb.startDragPosition.y, 2));
}
__donobuTrackInteraction(eventData);
}, true);
});
// Keyboard events
['keydown', 'keyup'].forEach((eventType) => {
document.addEventListener(eventType, function (e) {
if (processedEvents.has(e)) {
return;
}
processedEvents.add(e);
const keyboardEvent = e;
const target = keyboardEvent.target instanceof Element ? keyboardEvent.target : null;
__donobuTrackInteraction({
type: eventType,
key: keyboardEvent.key,
keyCode: keyboardEvent.keyCode,
altKey: keyboardEvent.altKey,
ctrlKey: keyboardEvent.ctrlKey,
metaKey: keyboardEvent.metaKey,
shiftKey: keyboardEvent.shiftKey,
selectors: dnb.generateSmartSelectors(target),
timestamp: new Date().getTime(),
});
}, true);
});
Object.defineProperty(dnb, 'interactionListenersRegistered', {
value: true,
enumerable: false,
writable: false,
configurable: false,
});
}
//# sourceMappingURL=page-interactions-tracker.js.map