Skip to content

Instantly share code, notes, and snippets.

@inertia186
Last active July 19, 2024 16:52
Show Gist options
  • Save inertia186/ee7458f2ef7aa338a0fc8c32bf3369f9 to your computer and use it in GitHub Desktop.
Save inertia186/ee7458f2ef7aa338a0fc8c32bf3369f9 to your computer and use it in GitHub Desktop.
unhover.user.js
// ==UserScript==
// @name unhover
// @namespace https://example.com/
// @version 2024-07-18
// @description Trap hover events so they don't interrupt you constantly and invert colors of the element causing hover.
// @author You
// @match *://*/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @updateURL https://gist.githubusercontent.com/inertia186/ee7458f2ef7aa338a0fc8c32bf3369f9/raw/unhover.user.js
// @downloadURL https://gist.githubusercontent.com/inertia186/ee7458f2ef7aa338a0fc8c32bf3369f9/raw/unhover.user.js
// ==/UserScript==
(function() {
'use strict';
// Function to stop propagation of hover events and invert colors
function handleHoverEvents(event) {
event.stopPropagation();
event.target.classList.toggle('invert-color');
}
// Add CSS class to invert colors
const style = document.createElement('style');
style.innerHTML = `
.invert-color {
filter: invert(1);
}
`;
document.head.appendChild(style);
// Attach event listeners to capture and prevent hover events
document.addEventListener('mouseover', handleHoverEvents, true);
document.addEventListener('mouseout', handleHoverEvents, true);
})();
@inertia186
Copy link
Author

@inertia186
Copy link
Author

inertia186 commented Jul 19, 2024

I recommend commenting out the event.target.classList.toggle('invert-color'); line. It's there to remind you this user script is running. But if you're fine with running it all the time, for all sites, may as well just let it do its thing without any reminder.

In my opinion, there is never any reason to have hover events, ever. Tooltips are one thing. They might be handy. If they're implemented without a hover, they won't be affected. But you don't need them. No UI element should ever need to activate with a hover. Website designers know that you might be on a mobile device with touch controls, so they aren't going to make the site unusable if you don't have hover controls.

And if they do, you can always disable the user script, if you remember it's running and you need the odd hover event, once in a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment