6

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>

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?

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.

4

1 回答 1

4

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

使用 jQuery,代码将仅仅是:

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

或者,对于旧版本的 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)您正在从页面中删除杂物,这使它不那么笨拙。
(3) 我怀疑您可能还需要解包或修改该链接——在这种情况下,使用 jQuery 的目标重写方法是可行的方法。


如果您真的只想PostMenu.click()用自己的空函数替换该函数,那么代码就是:

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

至于让 Greasemonkey 拦截所有onclick事件……要可靠地做到这一点并不容易。除非出于某种原因(在这种情况下似乎没有),否则请忘记这种方法。

于 2012-04-27T11:23:29.730 回答