Skip to content

Instantly share code, notes, and snippets.

@chapmanjacobd
Last active February 14, 2025 01:11
Show Gist options
  • Save chapmanjacobd/d1dda15aefa92eccf0d51f00cf574678 to your computer and use it in GitHub Desktop.
Save chapmanjacobd/d1dda15aefa92eccf0d51f00cf574678 to your computer and use it in GitHub Desktop.
Auto-Activate Text Field on Hover
// ==UserScript==
// @name Auto-Activate Text Field on Hover
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Automatically activates text fields when the mouse hovers over them, and only removes focus if not clicked.
// @author You
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const textFields = document.querySelectorAll('input[type="text"], textarea, input[type="url"], input[type="email"], input[type="password"], input[type="search"], input[type="tel"]');
textFields.forEach(textField => {
let wasClicked = false;
textField.addEventListener('mouseover', function(event) {
if (event.target === textField || textField.contains(event.target)) {
if (document.activeElement !== textField) {
textField.focus();
// textField.select(); // Select all text
}
}
});
textField.addEventListener('mousedown', function() {
wasClicked = true;
});
textField.addEventListener('mouseout', function(event) {
if (event.target === textField || textField.contains(event.target)) {
if (document.activeElement === textField && !wasClicked) {
textField.blur();
wasClicked = false;
}
}
});
textField.addEventListener('blur', function() {
wasClicked = false; // Reset the flag on blur, in case the user clicks elsewhere to remove focus
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment