9

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;
}
Community
  • 1
  • 1
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • What exactly is the current behavior? and which is the desired one? could you link to an example?? – jcane86 Apr 28 '11 at 04:02
  • @jcane86 - basically google instant search overrides the up and down arrow key presses. These typically scroll the page. Instead google has chosen to make these arrows jump down the results list. I don't use a mouse, so quickly scanning the page by pressing the down arrow works best. Now I have to press the down arrow eight times just to see the results on the bottom of the screen. – mrtsherman Apr 28 '11 at 14:02
  • why can't you use pageup/pagedown to get desired behavior, instead of overriding arrow key event listeners? – ampersand May 02 '11 at 17:29
  • 1
    @ampersand - because page up/page down is jerky. I like to tap the arrow key and scan with my eyes. If you google the problem you will find that lots of people dislike the new behavior. – mrtsherman May 02 '11 at 17:40
  • You don't need cancelBubble since it's IE only and depricated. You also don't need return false. – Sindre Sorhus May 23 '11 at 22:57

4 Answers4

3

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.
Raze
  • 2,175
  • 14
  • 30
  • @Raze - I will try these out Raze. Great ideas. – mrtsherman Apr 28 '11 at 14:03
  • How come you didn't get my full 150 bounty? I marked you as the right answer. – mrtsherman May 05 '11 at 17:14
  • I don't know. Gotta ask the admins. – Raze May 05 '11 at 17:22
  • Got this from the FAQ: "If you do not award your bounty within 7 days, the highest voted answer created after the bounty started with at least 2 upvotes will be awarded half the bounty amount." – Raze May 05 '11 at 17:26
  • I requested that they award you the other 75. Hopefully they do so. – mrtsherman May 06 '11 at 02:17
  • Still seems like a hacky way of doing things - a part of the question was "is there any way to override it or *portions of it*?". Now I know that it was an OR question, but you can't avoid the fact that if there were any other pieces of functionality depending on event propagation of those two key events, you'd break those as well. – gogogadgetinternet Jun 15 '13 at 19:56
2

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

Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
0

Remove all event listeners from the element by replacing it with a clone of itself (node.parentNode.replaceChild(node.cloneNode(true), node)) and then apply a manually modified version of Google's JavaScript code (document.documentElement.appendChild(document.createElement("script")).src="location of your modified script").

Eli Grey
  • 35,104
  • 14
  • 75
  • 93
  • Thank-you for the idea, but as google modifies their js I don't want to have to constantly update my manually modified version. – mrtsherman May 04 '11 at 19:06
-1

Try using a hard core replace before the function is executed.

document.body.innerHTML = document.body.innerHTML.replace(/if (b == 40) aa(f);/,'').replace(/else if (b == 38) aa(j);/,'').replace(/else if (b == 37 || b == 39) if (/a.preventDefault && a.preventDefault();/,'').replace(/return a.returnValue = j/,'')

Probably it will work

brunoais
  • 6,258
  • 8
  • 39
  • 59