Skip to content

Instantly share code, notes, and snippets.

@ojczeo
Created November 27, 2025 12:00
Show Gist options
  • Select an option

  • Save ojczeo/c3b5559847d4db1dbe879548653337d5 to your computer and use it in GitHub Desktop.

Select an option

Save ojczeo/c3b5559847d4db1dbe879548653337d5 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name MacBook Touchpad Event Translator
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Translates Touch events into full MouseEvent sequences
// @author https://github.com/ojczeo
// @match *://*/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// Max movement (px) allowed for a tap to be valid
const SCROLL_THRESHOLD = 10;
let startX = 0;
let startY = 0;
function simulateMouseClick(targetNode) {
function triggerMouseEvent(targetNode, eventType) {
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent(eventType, true, true);
targetNode.dispatchEvent(clickEvent);
}
// Dispatch full sequence for compatibility
["mouseover", "mousedown", "mouseup", "click"].forEach(function(eventType) {
triggerMouseEvent(targetNode, eventType);
});
}
// 1. Record touch start position
document.addEventListener('touchstart', function(e) {
if (e.changedTouches && e.changedTouches.length > 0) {
startX = e.changedTouches[0].screenX;
startY = e.changedTouches[0].screenY;
}
}, { passive: false });
// 2. Handle release and detect tap vs. scroll
document.addEventListener('touchend', function(e) {
if (e.changedTouches && e.changedTouches.length > 0) {
let endX = e.changedTouches[0].screenX;
let endY = e.changedTouches[0].screenY;
// Calculate drift
let deltaX = Math.abs(endX - startX);
let deltaY = Math.abs(endY - startY);
// Trigger only if movement was minimal (not a scroll)
if (deltaX < SCROLL_THRESHOLD && deltaY < SCROLL_THRESHOLD) {
simulateMouseClick(e.target);
}
}
}, { passive: false });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment