If a javascript function is declared anonymously is there any way to override it or portions of it?
I am attempting to stop google'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;
}
If a javascript function is declared anonymously is there any way to override it or portions of it?
I am attempting to stop google.'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;
}
Share
Improve this question
edited May 23, 2017 at 12:05
CommunityBot
11 silver badge
asked Nov 17, 2010 at 3:27
mrtshermanmrtsherman
39.9k23 gold badges89 silver badges111 bronze badges
5
- What exactly is the current behavior? and which is the desired one? could you link to an example?? – jcane86 Commented Apr 28, 2011 at 4: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 Commented Apr 28, 2011 at 14:02
- why can't you use pageup/pagedown to get desired behavior, instead of overriding arrow key event listeners? – ampersand Commented May 2, 2011 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 Commented May 2, 2011 at 17:40
- You don't need cancelBubble since it's IE only and depricated. You also don't need return false. – Sindre Sorhus Commented May 23, 2011 at 22:57
4 Answers
Reset to default 3 +75You 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.
- Add an event listener for the input text box at the capture phase, and cancel the event if keyCode is UP or DOWN
- 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.
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.
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
"
).
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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745400458a4626076.html
评论列表(0条)