skip to Main Content

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


  1. you can simulate a native mouseenter event since React’s synthetic event system still listens for these:

    const element = document.querySelector('your-selector-here');
    const hoverEvent = new MouseEvent('mouseenter', {
      bubbles: true,
      cancelable: true,
      view: window
    });
    
    element.dispatchEvent(hoverEvent);
    

    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.

    Login or Signup to reply.
  2. 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:

    const element = document.querySelector('your-element-selector');
    const mouseOverEvent = new MouseEvent('mouseover', { bubbles: true });
    element.dispatchEvent(mouseOverEvent);
    

    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:

    const element = document.querySelector('your-element-selector');
    Object.keys(element).forEach(key => {
      if (key.startsWith('__reactEventHandlers')) {
        element[key].onMouseOver(); // Simulate React onMouseOver event
      }
    });
    

    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):

    element.classList.add('your-hover-class');
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search