[英]Disabling All Click Events from Greasemonkey doesn't work
I'm trying to disable all click events on page from my Greasemonkey script using JQuery like this: 我正在尝试使用JQuery从我的Greasemonkey脚本禁用页面上的所有单击事件,如下所示:
$("*").unbind("click");
$("[onclick]").removeAttr("onclick");
Jquery is loaded into greasemonkey script using 使用以下命令将jQuery加载到oilsmonkey脚本中
// @require http://code.jquery.com/jquery-1.10.1.min.js
It works when I run it manually from Firefox OR Firebug Console. 当我从Firefox或Firebug控制台手动运行它时,它可以工作。
But it doesn't work from Greasemonkey! 但这不适用于Greasemonkey!
What is the problem? 问题是什么?
How Can I do that from greasemonkey? 我该如何从滑脂猴中做到这一点?
It works when I run it manually from Firefox OR Firebug Console.
当我从Firefox或Firebug控制台手动运行它时,它可以工作。
But it doesn't work from Greasemonkey!但这不适用于Greasemonkey!
Since Greasemonkey usually runs your script in a wrapper function, the jQuery the page is using is not the same as the jQuery in your script. 由于Greasemonkey通常在包装函数中运行脚本,因此页面使用的jQuery与脚本中的jQuery不同。 Either that or you're overwriting the jQuery function with a different one.
要么就是您要用另一种方法覆盖jQuery函数。
Because of that, .unbind()
in your script cannot remove the listeners on the page because they were added with a different jQuery function. 因此,脚本中的
.unbind()
无法删除页面上的侦听器,因为它们是使用其他jQuery函数添加的。
It works when you run it in the Firebug console because the Firebug console will use the jQuery function from the page, not your Greasemonkey script. 当您在Firebug控制台中运行它时,它可以工作,因为Firebug控制台将使用页面中的jQuery函数,而不是您的Greasemonkey脚本。
To solve this, just use the jQuery function from the page to unbind the listeners. 要解决此问题,只需使用页面上的jQuery函数来取消绑定监听器。
We can use unsafeWindow
to access the page's window object. 我们可以使用
unsafeWindow
访问页面的窗口对象。
unsafeWindow.$("*")
.unbind("click")
.off("click")
.removeAttr("onclick");
Note: this will only remove event listeners added by jQuery. 注意:这只会删除jQuery添加的事件监听器。
To remove listeners added by .addEventListener()
, you will have to hi-jack that method to listen for calls to it, and then filter them so click events aren't added. 要删除由
.addEventListener()
添加的侦听.addEventListener()
,您将必须劫持该方法以侦听对其的调用,然后对其进行过滤,以便不添加click事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.