Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding a portion of a google.com anonymous function

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;
}
like image 383
mrtsherman Avatar asked Nov 17 '10 03:11

mrtsherman


2 Answers

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.

  1. Add an event listener for the input text box at the capture phase, and cancel the event if keyCode is UP or DOWN
  2. Create an overlayed text box where you actually enter text, and pass on all events except UP key and DOWN key to the actual text box underneath.
like image 176
Raze Avatar answered Sep 28 '22 18:09

Raze


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

like image 20
Sindre Sorhus Avatar answered Sep 28 '22 18:09

Sindre Sorhus