Im building a bookmarklet to automatically perform actions on my browser. The site im trying to make this work with (i dont control the site’s source code) has some elements that change when hovering with the mouse (or pointer) and im trying to trigger that state change.
The main problem (i think) is that the site is based on reactjs, so it has its own set of handlers.
I have tried multiple approaches to programmatically trigger the hover event , but nothing has worked so far:
- Forcing changing the state of the browser (with
Force state -> hover
) - Creating a custom Event, then dispatching it.
When monitoring the events on the site, even filtering I cant seem to be able to find which element is actually triggering the event (the site has a heavily nested react DOM as all react sites do).
2
Answers
you can simulate a native mouseenter event since React’s synthetic event system still listens for these:
React handles native events like
mouseenter
alongside its synthetic events. By dispatching this event, you should trigger any hover-related changes on the site. Use the browser’s dev tools to find the exact element and modify the selector as needed.If you do not have access to the site’s source code, here are best options to trigger hover or similar events:
1. Simulate Mouse Event (Vanilla JavaScript)
This method can trigger DOM events, even on React sites:
This simulates the actual
mouseover
event, which is often what triggers hover states.2. Trigger React’s Internal Event Handler
Although you can’t directly interact with React’s internal state, you can attempt to trigger its event handlers:
This manually invokes React’s event handlers if they are available on the element.
3. Simulate CSS Hover with Forced Class
If you’re only trying to mimic the hover effect visually (without triggering an event):
This adds the class that normally gets added on hover, forcing the visual change.
4. Use DevTools to Force Hover
If your goal is purely visual (for testing), you can open Chrome DevTools, right-click the element, select "Force state", and check
:hover
. This will apply the hover state.