<commit_before>/* global chrome:false */
var loaded;

function getElementXPath(element) {
	var path = [];

	do {
		if (element.id) {
			path.unshift('id("' + element.id + '")');

			// No need to continue to ascend since we found a unique root
			break;
		}
		else if (element.parentNode) {
			var nodeName = element.nodeName;
			var hasNamedSiblings = Boolean(element.previousElementSibling || element.nextElementSibling);
			// XPath is 1-indexed
			var index = 1;
			var sibling = element;

			if (hasNamedSiblings) {
				while ((sibling = sibling.previousElementSibling)) {
					if (sibling.nodeName === nodeName) {
						++index;
					}
				}

				path.unshift(nodeName + '[' + index + ']');
			}
			else {
				path.unshift(nodeName);
			}
		}
		// The root node
		else {
			path.unshift('');
		}
	} while ((element = element.parentNode));

	return path.join('/');
}

(function () {
	if (loaded) {
		return;
	}

	var EVENT_TYPES = 'click dblclick mousedown mouseup mousemove keydown keyup'.split(' ');
	var lastMouseDown = {};

	var port = chrome.runtime.connect(chrome.runtime.id, { name: 'eventProxy' });
	port.onDisconnect.addListener(function disconnect() {
		port.onDisconnect.removeListener(disconnect);
		EVENT_TYPES.forEach(function (eventType) {
			document.removeEventListener(eventType, sendEvent, true);
			loaded = false;
			port = null;
		});
		window.removeEventListener('message', passEvent, false);
	});

	function send(detail) {
		if (window !== window.top) {
			window.parent.postMessage({
				method: 'recordEvent',
				detail: detail
			}, '*');
		}
		else {
			port.postMessage({
				method: 'recordEvent',
				args: [ detail ]
			});
		}
	}

	function sendEvent(event) {
		if (event.type === 'mousedown') {
			lastMouseDown[event.button] = event;
		}

		if (
			event.type === 'click' && (
				Math.abs(event.clientX - lastMouseDown[event.button].clientX) > 5 ||
				Math.abs(event.clientY - lastMouseDown[event.button].clientY > 5)
			)
		) {
			return;
		}

		var rect = event.target.getBoundingClientRect();

		send({
			altKey: event.altKey,
			button: event.button,
			buttons: event.buttons,
			ctrlKey: event.ctrlKey,
			clientX: event.clientX,
			clientY: event.clientY,
			elementX: event.clientX - rect.left,
			elementY: event.clientY - rect.top,
			keyIdentifier: event.keyIdentifier,
			location: event.location,
			metaKey: event.metaKey,
			shiftKey: event.shiftKey,
			target: getElementXPath(event.target),
			targetFrame: [],
			type: event.type
		});
	}

	function passEvent(event) {
		if (!event.data || event.data.method !== 'recordEvent' || !event.data.detail) {<commit_msg>Fix drag and drop<commit_after>/* global chrome:false */
var loaded;

function getElementXPath(element) {
	var path = [];

	do {
		if (element.id) {
			path.unshift('id("' + element.id + '")');

			// No need to continue to ascend since we found a unique root
			break;
		}
		else if (element.parentNode) {
			var nodeName = element.nodeName;
			var hasNamedSiblings = Boolean(element.previousElementSibling || element.nextElementSibling);
			// XPath is 1-indexed
			var index = 1;
			var sibling = element;

			if (hasNamedSiblings) {
				while ((sibling = sibling.previousElementSibling)) {
					if (sibling.nodeName === nodeName) {
						++index;
					}
				}

				path.unshift(nodeName + '[' + index + ']');
			}
			else {
				path.unshift(nodeName);
			}
		}
		// The root node
		else {
			path.unshift('');
		}
	} while ((element = element.parentNode));

	return path.join('/');
}

(function () {
	if (loaded) {
		return;
	}

	var EVENT_TYPES = 'click dblclick mousedown mouseup mousemove keydown keyup'.split(' ');
	var lastMouseDown = {};

	var port = chrome.runtime.connect(chrome.runtime.id, { name: 'eventProxy' });
	port.onDisconnect.addListener(function disconnect() {
		port.onDisconnect.removeListener(disconnect);
		EVENT_TYPES.forEach(function (eventType) {
			document.removeEventListener(eventType, sendEvent, true);
			loaded = false;
			port = null;
		});
		window.removeEventListener('message', passEvent, false);
	});

	function send(detail) {
		if (window !== window.top) {
			window.parent.postMessage({
				method: 'recordEvent',
				detail: detail
			}, '*');
		}
		else {
			port.postMessage({
				method: 'recordEvent',
				args: [ detail ]
			});
		}
	}

	function sendEvent(event) {
		if (event.type === 'mousedown') {
			lastMouseDown[event.button] = event;
		}

		if (
			event.type === 'click' && (
				Math.abs(event.clientX - lastMouseDown[event.button].clientX) > 5 ||
				Math.abs(event.clientY - lastMouseDown[event.button].clientY > 5)
			)
		) {
			return;
		}

		// When a user drags an element that moves with the mouse, the element will not be dragged in the recorded
		// output unless the final position of the mouse is recorded relative to an element that did not move
		var target;

		if (event.type === 'mouseup') {
			target = document.documentElement;
		}
		else {
			target = event.target;
		}

		var rect = target.getBoundingClientRect();

		send({
			altKey: event.altKey,
			button: event.button,
			buttons: event.buttons,
			ctrlKey: event.ctrlKey,
			clientX: event.clientX,
			clientY: event.clientY,
			elementX: event.clientX - rect.left,
			elementY: event.clientY - rect.top,
			keyIdentifier: event.keyIdentifier,
			location: event.location,
			metaKey: event.metaKey,
			shiftKey: event.shiftKey,
			target: getElementXPath(target),
			targetFrame: [],
			type: event.type
		});
	}

	function passEvent(event) {
		if (!event.data || event.data.method !== 'recordEvent' || !event.data.detail) {
{'fraction_non_alphanumeric': 0.14362464183381088, 'fraction_numerical': 0.0014326647564469914, 'mean_word_length': 3.2829754601226995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1630404', 'n_tokens_mistral': 2198, 'n_tokens_neox': 1860, 'n_words': 409}