覆盖google.com匿名函数的一部分 [英] Overriding a portion of a google.com anonymous function
问题描述
如果javascript函数是匿名声明的,有什么方法可以覆盖它或它的一部分吗?
我试图阻止google.com的即时搜索劫持向上和向下箭头键移动您的搜索排名。我已经确定了我认为是代码有问题的部分。键盘38和40用于上下键。
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
问题在于这是被调用函数的一部分Sb =函数(a){}位于大约三千行匿名函数中。在SO上发布了类似的问题在这里,作者最终以一种不适用于我的黑客方式工作。我意识到我可以关闭即时搜索功能,但我喜欢它,但我无法忍受我的箭头键不再有效。
解决方案:
>我最终编写了 Chrome扩展程序将上/下箭头键功能恢复为滚动。我使用了下面的代码。感谢您的Raze和Mofle。
if(event.keyCode == 40 || event.keyCode == 38){
event.cancelBubble = true;
event.stopPropagation();
返回false;
匿名函数里面的另一个匿名函数。您不能更改任何现有函数的部分,您必须创建一个新函数并将其附加到需要的位置,尽管您可以获取函数的来源,操作它并从源创建一个新函数。 p>
这里有两个关于这个问题的建议,其中不涉及重新定义函数。
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.
- 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.
这篇关于覆盖google.com匿名函数的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!