覆盖 google.com 匿名函数的一部分
如果一个 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 = function (a) {} 的函数的一部分,该函数位于大约三千行匿名函数内。 SO 上发布了类似的问题 作者最终以一种不适合我的方式进行锻炼。我意识到我可以关闭即时搜索,但我喜欢它,我只是无法忍受我的箭头键不再起作用。
解决方案:
我最终编写了一个 chrome 扩展,将向上/向下箭头键功能恢复为滚动。我使用了以下代码。感谢 Raze 和 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.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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能在另一个匿名函数中覆盖另一个匿名函数。您无法更改任何现有函数的部分,您必须创建一个新函数并将其附加到需要的位置,尽管您可以获取函数的源代码,对其进行操作并从源代码中创建一个新函数。
我在这里对这个问题有两个建议,其中不涉及重新定义函数。
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.
您可以通过捕获箭头键事件并防止它们冒泡来轻松做到这一点。
已在 google.com 上测试并正常运行
You can easily do that by capturing arrow key events and preventing them from bubbling.
Tested and works on google.com
通过将其替换为自身的克隆 (
node.parentNode.replaceChild(node.cloneNode(true), node)
),从元素中删除所有事件侦听器,然后应用手动修改的 Google JavaScript 代码版本(document.documentElement.appendChild(document.createElement("script")).src="
修改脚本的位置
" )。
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.
Probably it will work