0

Hi all,

I want to know if it is possible and how to intercept an existing javascript keybind and block it.

For example, a website has some javascript that, when the user hits the letter "H" on the keyboard, the website automatically runs a function, e.g. redirecting to another web page.

I want to block this functionality after it has been loaded into the page.

Is this possible? Perhaps the stoppropagation function? Or maybe simply re-assign the key event to just run an empty function?

Thanks,
Max.

4
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by Airshow
0

If your page contains a frame, and THAT frame contains and EXTERNAL/REMOTE page that contains the javascript that is "listening" for the "H" keypress, then you will NOT be able to rebind the event. In other words, what you are trying to do is not possible. The security restrictions imposed by the browser will not give you access to the iframe content/objects if it is from a different domain.

0

Thanks for your reply,

The process does not involve any iFrames. I am going to use this in a browser add-on and so the javascript will be inserted directly into the page as if I was the developer of that website and just inserting code as normal.

Max

0

then you will need to do so AFTER the page has loaded. To clarify, basically this is what needs to be done:

//assuming this is what is there initially
document.body.onkeypress=function(){
 //code that "listens" for "H"
 ...
};

then you need to override that listener after page load:

onload=function(){
 //assuming this is what is there initially
 document.body.onkeypress=function(){
  //YOUR function definition here.
  ...
 };
}
0

Iv had a go at that, the key I need to override is backspace but entering the backspace code (8) does not seem to work. How should that work?

0
Iv had a go at that, the key I need to override is backspace but entering the backspace code (8) does not seem to work. How should that work?

it will be better if you post what you have

@Airshow: I suspect he's developing something similar to a Greasemonkey script/"extension"

0

Hielo,

@Airshow: I suspect he's developing something similar to a Greasemonkey script/"extension"

Aha! Thanks, this topic makes sense now.

Airshow

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.