Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept (and perhaps cancel) page's mouse/keyboard event handlers

I'm trying to create a Chrome Extension that deals with selected text. Some website pages' otherwise selectable text content has click/mouse-up/down event handlers that navigate to a new page.

Is there a way from the background or content script to temporarily disable (and restore) the page's arbitrary event handlers without interfering with the native text selection?

Worst case I'm thinking of is to detach, clone the body html, allow the selection, and then restore the bound original. Seems like trouble.

Thanks!

like image 311
Jason Kleban Avatar asked Sep 12 '14 19:09

Jason Kleban


People also ask

How to handle keyboard and mouse events using the API?

Handling special keyboard and mouse events are done using the Advanced User Interactions API. It contains the Actions and the Action classes that are needed when executing these events. The following are the most commonly used keyboard and mouse events provided by the Actions class. Clicks (without releasing) at the current mouse location.

How to handle a keyboard event in JavaScript?

To handle a keyboard event, you follow these steps: First, select the element on which the keyboard event will fire. Typically, it is a text box. Then, use the element.addEventListener () to register an event handler. Suppose that you have the following text box with the id message:

What is a keyboard event object?

Jump to: KeyboardEvent objects describe a user interaction with the keyboard; each event describes a single interaction between the user and a key (or combination of a key with modifier keys) on the keyboard. The event type (keydown, keypress, or keyup) identifies what kind of keyboard activity occurred.

Why can't a keyboard-only user trigger a mouse event?

When you use only mouse events to trigger an action, a keyboard-only user cannot trigger the event. The solution often involves one or two steps. First, you need to determine if the element where you attaching the mouse events can also receive the keyboard focus.


1 Answers

Most HTML DOM events follow the capture-target-bubble event model. This means, for example, that if you click on a button, that the "click" event is first dispatched at the root, all the way down to the button, then back up. Event propagation can be stopped, which prevents the event listener at the next level from being notified of the event.

The earliest possibility of receiving the event is at the root, often window at the capture phase. To bind an event listener to the capture phase, use addEventListener with the third parameter set to true:

// in a content script, at run_at:document_start
window.addEventListener('click', function(event) {
    event.stopImmediatePropagation();
}, true);

Many web pages use jQuery to manage DOM events, which binds the event listeners at the bubbling phase, so the previous method will work on most sites. If the page does not use jQuery, then you have to bind your event listener at document_start to make sure that your event listener is triggered before every other event listener.

like image 136
Rob W Avatar answered Oct 23 '22 01:10

Rob W