If a javascript function is declared anonymously is there any way to override it or portions of it?
I am attempting to stop google.com's instant search from hijacking the up and down arrow keys to move through your search rankings. I have identified what I believe is the problematic section of code. Keycodes 38 and 40 are for the down and up keys.
if (b == 40) aa(f);
else if (b == 38) aa(j);
else if (b == 37 || b == 39) if (!ca(b == 39)) return f;
a.preventDefault && a.preventDefault();
return a.returnValue = j
The problem is that this is part of a function called Sb = function (a) {} that lies inside of about a three thousand line anonymous function. There is a similar question posted on SO here that the author ended up working out in a hacky way that doesn't apply to me. I realize I can turn off instant search, but I like it, I just can't stand that my arrow keys don't work anymore.
SOLUTION:
I ended up writing a chrome extension to revert up/down arrow key functionality to scrolling. I used the following code. Thank you to Raze and Mofle.
if (event.keyCode == 40 || event.keyCode == 38) {
event.cancelBubble = true;
event.stopPropagation();
return false;
}
You can't override an anonymous function inside another anonymous function. You can't change portions of any existing function, you'll have to create a new function and attach it where required, though you could get the source of a function, manipulate it and create a new function out of the source.
I have two suggestions for this problem here, which doesn't involve redefining the functions.
You can easily do that by capturing arrow key events and preventing them from bubbling.
window.addEventListener('keydown', function(e) {
e.stopPropagation();
}, true);
Tested and works on google.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With