简体   繁体   English

从Greasemonkey禁用所有点击事件不起作用

[英]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.

相关问题 更多提交按钮禁用了Jquery的点击,不起作用 - More submit button disabling on click with Jquery, doesn't work .submit()不适用于Firefox和Greasemonkey - .submit() doesn't work with Firefox and Greasemonkey 随Greasemonkey + jQuery添加的按钮出现,但单击时不起作用 - Buttons added with Greasemonkey+jQuery appear but don't work on click 为什么 Alpine.js 不能处理他们示例中的所有事件? - Why doesn't Alpine.js work with all events from their examples? 使用jQuery通过id隐藏div在Greasemonkey中不起作用? - Using jQuery to hide a div by id doesn't work in Greasemonkey? 为什么jQuery()。remove()和unbind()在GreaseMonkey中不起作用? - Why doesn't jQuery().remove() and unbind() work in GreaseMonkey? 为什么此Greasemonkey脚本不能与此jQuery插件一起使用? - Why doesn't this Greasemonkey script work with this jQuery plugin? 通过添加事件侦听器禁用上下文菜单(右键单击中显示的选项)不起作用 - Disabling contextmenu (the options seen on a right click) by adding event listener doesn't work 即使使用引用,GreaseMonkey setTimeout也不起作用(FF4) - GreaseMonkey setTimeout doesn't work even with references (FF4) react-bootstrap js事件根本不起作用 - react-bootstrap js events doesn't work at all
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM