如何用GreaseMonkey替换onclick事件? [英] How to replace onclick event with GreaseMonkey?

查看:82
本文介绍了如何用GreaseMonkey替换onclick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个网站有一个图像库。每次我点击缩略图时,它都会在新标签页中打开URL(不是因为我设置了firefox以在新标签页中打开链接)。我想在同一个窗口中打开URL。缩略图图像的示例是这样的。

This website has a gallery of images. Each time I click on a thumbnail image it opens the URL in a new tab (not because I set firefox to open links in new tabs). I want to just open the URL in the same window. An example of what the thumbnail images looks like is this.

<span class="thumb" id="789">
<a href="/post/image/12345" onclick="return PostMenu.click(12345)">
<img  class="preview" src="http://abc.com/image.jpg" title="title" alt="">
</a>
</span>

我相信 onclick =return PostMenu.click(12345)正在这样做。如何在GreaseMonkey中用我自己的空函数替换 PostMenu.click()函数?有没有办法让GreaseMonkey脚本拦截所有onclick事件?

I believe that onclick="return PostMenu.click(12345)" is doing this. How can I replace the PostMenu.click() function with my own empty function in GreaseMonkey? Is there a way to make a GreaseMonkey script intercept all onclick events?

我唯一的另一个选择是遍历所有 span 类并从链接标记中删除 onclick =return PostMenu.click(12345)。但是因为在一个页面上可能有超过一百个,我宁愿不这样做。

My only other option is to go through all the span classes and remove the onclick="return PostMenu.click(12345)" from the link tags. But since there can be over a hundred of these on a single page, I'd rather not do that.

推荐答案

实际上,删除 onclick s根本不是一项繁重的任务。

Actually, deleting the onclicks is not at all an onerous task.

使用jQuery,代码只会是:

With jQuery, the code would merely be:

$("span.thumb a").prop ("onclick", null);

或者,对于旧版本的jQuery:

Or, for older versions of jQuery:

$("span.thumb a").removeAttr ("onclick");

一个完整的脚本:

// ==UserScript==
// @name     _Kill select onClicks
// @include  http://YOUR_SERVER/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript==

$("span.thumb a").prop ("onclick", null);



这种方法的优点是:

(1)你保留 PostMenu.click()以防你想要它,或者它需要,在其他地方,

(2)你要删除crud来自页面,这使得它稍微不那么笨拙。

(3)我怀疑你可能还需要打开或修改该链接 - 在这种情况下,使用jQuery的目标重写方法是要走的路。


The advantage of this approach is that:
(1) You preserve PostMenu.click() in case you want it, or it's needed, elsewhere,
(2) You are removing crud from the page, which makes it just a little bit less unwieldy.
(3) I suspect that you might also need to unwrap or modify that link -- in which case, the targeted rewrite approach using jQuery is the way to go.

如果你真的只想更换 PostMenu.click() 具有您自己的空函数的函数,其代码只是:

If you really just want to replace the PostMenu.click() function with your own empty function, the code for that is just:

unsafeWindow.PostMenu.click = function () {};

至于让Greasemonkey拦截所有 onclick 事件......这并不容易可靠。忘记这种方法,除非出于某种原因需要压倒性的(在这种情况下似乎没有)。

As for having Greasemonkey intercept all onclick events... That is not easy to do reliably. Forget that approach unless there is an overwhelming need for some reason (which there doesn't seem to be, in this case).

这篇关于如何用GreaseMonkey替换onclick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆